如何使用二维数组存储坐标?(How to store co

编程入门 行业动态 更新时间:2024-10-25 22:25:24
如何使用二维数组存储坐标?(How to store co-ordinates using a two dimensional array?)

我正在做一个涉及二维数组的项目。 该项目涉及一个从左上角移动到右下角的点。 我现在正在做的是制作一个可能的XMoves数组和一个可能的YMoves数组。 我想知道是否有更好的方法来定义二维数组的坐标。 以下是我所拥有的。

int moveX1 = x + 1; int moveX2 = x + 1; int moveX3 = x + 1; int moveX4 = x; int moveX5 = x - 1; int moveX6 = x - 1; int moveX7 = x - 1; int moveX8 = x; int [] possibleXIndexes = {moveX1, moveX2, moveX3, moveX4, moveX5, moveX6, moveX7, moveX8}; int moveY1 = y - 1; int moveY2 = y; int moveY3 = y + 1; int moveY4 = y + 1; int moveY5 = y + 1; int moveY6 = y; int moveY7 = y - 1; int moveY8 = y - 1; int [] possibleYIndexes = {moveY1, moveY2, moveY3, moveY4, moveY5, moveY6, moveY7, moveY8};

I am doing a project involving a two dimensional array. The project involves a dot that moves from the top left corner to the bottom right corner. What I am doing right now is making an array of possibleXMoves and an array of possibleYMoves. I was wondering if there was a better way to define coordinates for a two dimensional array. Below is what I have.

int moveX1 = x + 1; int moveX2 = x + 1; int moveX3 = x + 1; int moveX4 = x; int moveX5 = x - 1; int moveX6 = x - 1; int moveX7 = x - 1; int moveX8 = x; int [] possibleXIndexes = {moveX1, moveX2, moveX3, moveX4, moveX5, moveX6, moveX7, moveX8}; int moveY1 = y - 1; int moveY2 = y; int moveY3 = y + 1; int moveY4 = y + 1; int moveY5 = y + 1; int moveY6 = y; int moveY7 = y - 1; int moveY8 = y - 1; int [] possibleYIndexes = {moveY1, moveY2, moveY3, moveY4, moveY5, moveY6, moveY7, moveY8};

最满意答案

您可以使用Point类。

不是将可能的索引存储在单独的数组中,而是将它们作为Points集合存储在一个数组中。

You could use the Point class.

Instead of storing the possible indexes in separate arrays, just store them in one as a collection of Points.

更多推荐

本文发布于:2023-07-28 23:29:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1310163.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:数组   坐标   如何使用   store

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!