0

私はこのコードを持っています:

_leftArray[0] = _square[6][4];
_leftArray[1] = _square[8][4];
_leftArray[2] = _square[9][5];

I want to be able to extract the values of the array. I'd like to write a method that takes the array position as an argument and returns the coordinates. So if the method was called returnCoordinatesFromArray, I could type returnCoordinatesFromArray[1] and return 6 as a variable and 4 as a variable, so I could use them in another method.

4

3 に答える 3

1

これらが静的でハードコードされた値である場合、次のようにしてみませんか。

Map<Integer, int[]> indexToCoordinateMap = new LinkedHashMap<Integer, int[]>();
indexToCoordinateMap.put(0, new int[]{6, 4});
indexToCoordinateMap.put(1, new int[]{8, 4});
indexToCoordinateMap.put(2, new int[]{9, 5});

その場合、メソッドは必要ありません。0thインデックスが x 座標で、インデックスが y 座標である値の配列を簡単に取得できます1st。もちろん、これは慣習によるものです。より具体的にしたい場合は、Point次のようなものを使用して実行できます。

Map<Integer, Point> indexToPointMap = new LinkedHashMap<Integer, Point>();
indexToPointMap.put(0, new new Point(6, 4));
indexToPointMap.put(1, new Point(8, 4));
indexToPointMap.put(2, new Point(9, 5));

次に、次のように簡単に実行できます。

Point point = indexToPointMap.get(0);
于 2013-04-02T22:31:20.893 に答える
0

double for ループを実行し、x と y の位置を保存します。

_square value;
int posX=0;
int posY=0;
for(int i=0; i<arr.length; i++) {
   for(int j=0; j<arr.length; j++) {
      if(arr[i][j]==value) {
         posX=i;
         posY=j;
      }
   }
}
于 2013-04-02T22:31:04.543 に答える