Java - 坐标数组

Java - Array of Coordinates

我需要写一个坐标数组。我写的坐标 class 这样:

public class Coordinate {
   int row;
   int column;

   public Coordinate (int column, int row) {
   ...
   }
}

在我的主要方法中,我想要一个数组 Coordinate positions[] = new Coordinate[64] 并用值 (0,0), (0,1), ..., (7,6), (7 ,7).

知道如何处理这个问题吗?我已经尝试过嵌套循环,但我做不对。

编辑:抱歉,伙计们,仍在尝试使用 Stack Overflow 解决问题 我在做:

  for(int i=0; i<64; i++) {
        for(int c=0; c<8; c++) {
            for(int r=0; r<8; r++) {
            positions[i] = new Coordinate(c, r);
            }
        }
      }

但它弄得一团糟,循环太多了

我也不太明白你想做什么。首先,如果你想存储不同类型的坐标,你可以使用这个

public class Coords<T> {

  private final  T x;
  private final T y;

  public Coords(T x,T y ) {
    this.y = y;
    this.x = x;
  }
}

那就看你要不要遵守规则了。据我了解你可能想要这样的东西

        int count = 0;
        for( int i = 0;i < max_first_value; i++)
        {
            for(int o=0;o<max_second_value; o++){
                if(count == 64){
                    i=max_first_value;
                    o=max_second_value;
                    break;
                }
                positions[count] = new Coord<Integer>(i,o);
                count++;
            }
        }

其中 max_fist_value 和 max_second_values 是您想要的坐标最大值,但这取决于您要使用什么规则来填充此数组。变量 count are 用于不超过数组的边界

您的第一个循环导致循环过多,一个简单的解决方案如下:

int i=0;
 for(int c=0; c<8; c++) {
        for(int r=0; r<8; r++) {
        positions[i] = new Coordinate(c, r);
        i++; 
        }
    }