简体   繁体   English

Java中2个数字的二维数组

[英]2d array of 2 numbers in Java

Edit: Maybe it's a 3d array?编辑:也许它是一个 3d 阵列? I don't want to use ArrayList.我不想使用 ArrayList。

I know this is basic but I'm still having trouble wrapping my head around arrays.我知道这是基本的,但我仍然无法将头环绕在数组上。 So I want to create a 2d array with 7 rows and 4 columns, like this:所以我想创建一个 7 行 4 列的二维数组,如下所示:

  0 1 2 3
0        
1        
2        
3 
4
5
6

And in each spot I want to put two numbers.在每个位置我想放两个数字。 For example,例如,

   0        1        2        3
0 (1, 8)   (2, 7)   (3, 6)   (4, 5)      
1        
2        
3 
4
5
6

Just as an example, if I was trying to fill (2,5) in it all in via for loops, I would do:举个例子,如果我试图通过 for 循环将 (2,5) 全部填充进去,我会这样做:

int[][][2] table = new int[7][4][2];
for (int i = 0; i < 7; i++) {
    for (int j = 0; i < 4; j++) {
    table[i][j][1] = 2;
    table[i][j][2] = 5;
    }
}

This isn't right.这是不对的。 I can't figure out how to do it.我不知道该怎么做。 Also, how would I reference a specific cell when it is correct?另外,如果正确,我将如何引用特定单元格? Like in the first example, if I wanted (1,8), would I put: table[1][1]?就像在第一个例子中一样,如果我想要 (1,8),我会放:table[1][1]吗? Or if I wanted just the 1 would I put: table[1][1][1]?或者,如果我只想要 1,我会放:table[1][1][1]?

You have 3 errors in your code:您的代码中有 3 个错误:

int[][][2] table = new int[7][4][2]; // <== Remove 2
for (int i = 0; i < 7; i++) {
    for (int j = 0; i < 4; j++) {    // <== Change i to j
        table[i][j][1] = 2;          // <== Arrays are zero-based
        table[i][j][2] = 5;          // <== -
    }
}

Corrected code is:更正后的代码是:

int[][][] table = new int[7][4][2];
for (int i = 0; i < 7; i++) {
    for (int j = 0; j < 4; j++) {
        table[i][j][0] = 2;
        table[i][j][1] = 5;
    }
}

Testing with System.out.println(Arrays.deepToString(table)) produces:使用System.out.println(Arrays.deepToString(table))产生:

[[[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]], [[2, 5], [2, 5], [2, 5], [2, 5]]]

To update with the values given, you can replace the 3rd array, or update the values directly.要使用给定的值进行更新,您可以替换第三个数组,或直接更新值。 Here I show both ways to update the first two:在这里,我展示了更新前两个的两种方法:

int[][][] table = new int[7][4][2];

table[0][0] = new int[] { 1, 8 };

table[0][1][0] = 2;
table[0][1][1] = 7;
[[[1, 8], [2, 7], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]]]

You can even replace the entire first row in one operation:您甚至可以在一次操作中替换整个第一行:

int[][][] table = new int[7][4][2];
table[0] = new int[][] { {1, 8}, {2, 7}, {3, 6}, {4, 5} };
[[[1, 8], [2, 7], [3, 6], [4, 5]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]], [[0, 0], [0, 0], [0, 0], [0, 0]]]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM