eclipse 如何在二维数组中添加元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/29997216/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to add elements in a 2d array
提问by lawtonhself
I just want to know how this program works, and why the answer is 14. here is the code:
我只想知道这个程序是如何工作的,为什么答案是 14。这是代码:
public class extra {
public static void main(String[] args){
int[][] table = {{1,2,3},{4,5,6},{7,8,9}};
int sum = 0;
for( int i = 2; i > 0; i-- )
sum += table[i][3-i];
System.out.println(sum);
}
}
}
I understand the way the matrix is set up
我了解矩阵的设置方式
123
123
456
456
789
789
but what is i in this problem, because I thought it was the number of rows, but since it is in a for loop, does it mean that i is the number in the matrix? Also how do the [i][3-i] come in to affect? The answer is 14, and I just want to know how it is 14.
但是这个问题中的 i 是什么,因为我认为它是行数,但是由于它在 for 循环中,这是否意味着 i 是矩阵中的数字?另外 [i][3-i] 是如何产生影响的?答案是14,我只想知道14是怎么来的。
回答by Patricia Shanahan
It is only summing part of a diagonal, specifically table[2][1]
which is 8, and table[1][2]
which is 6.
它只是对角线的一部分求和,具体来说table[2][1]
哪个是 8,table[1][2]
哪个是 6。
The easiest way to see what is going on is to add an output statement in the loop:
查看发生了什么的最简单方法是在循环中添加一个输出语句:
for (int i = 2; i > 0; i--) {
sum += table[i][3 - i];
System.out.println(i + " " + (3 - i) + " " + table[i][3 - i]);
}
回答by dawrutowicz
your program takes table[2][1] (value of 8) and table[1][2] (value of 6) elements, sums them and prints as output (value of 14)
您的程序采用 table[2][1](值为 8)和 table[1][2](值为 6)元素,将它们相加并打印为输出(值为 14)
regarding your question in a title your main method should be more like this:
关于标题中的问题,您的主要方法应该更像这样:
public static void main(String[] args) {
int[][] table = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}};
int sum = 0;
System.out.println("Before\n");
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
sum += table[i][j];
System.out.printf("Sum after %d iteration: %d\n", i + j + 1, sum);
}
}
System.out.println("\nIn total: " + sum);
}
i + j + 1 is a sum of current iteration which is sum of both axises, and since Java has 0-based indexed tables, it is increased by 1
i + j + 1 是当前迭代的总和,它是两个轴的总和,并且由于 Java 具有基于 0 的索引表,因此增加了 1
Hope it helps!
希望能帮助到你!
回答by Rhyzomatic
i
, in itself, does not correspond directly to anything in the matrix. It is just the name of the variable that the for loop changes each time it loops.
i
本身并不直接对应于矩阵中的任何内容。for 循环每次循环时更改的只是变量的名称。
The [i][3-i]
is how the i
interacts with table
. On the first round of the for loop, the i
will be equal to 2. Thus, sum
will be increased by table[2][1]
, which is the 3rd row and the 2nd column of the matrix, which has a value of 8
.
这[i][3-i]
就是 与i
交互的方式table
。在 for 循环的第一轮中,thei
将等于 2。因此,sum
将增加table[2][1]
,这是矩阵的第 3 行第 2 列,其值为8
。
On the second round of the for loop, the for loop, the i
will be equal to 1. Thus, sum
will be increased by table[1][2]
, which is the 2nd row and the 3rd column of the matrix, which has a value of 6
.
在 for 循环的第二轮 for 循环中,thei
将等于 1。因此,sum
将增加table[1][2]
,即矩阵的第 2 行第 3 列,其值为6
。
Therefore, sum
will be equal to 8+6=14.
因此,sum
将等于 8+6=14。
回答by Martin M J
What the for loop does is as follows:
for 循环的作用如下:
i
= 2. Enter loop.- Add
table[2][3-2]
to sum. Sum is now 8, becausetable[2][1]
= 8. - Decrement
i
by 1. i
= 1. Enter loop.- Add
table[1][3-1]
to sum. Sum is now 14, becausetable[1][2]
= 6. - Decrement
i
by 1. i
= 0. 0 is not greater than 0, so we exit the loop. The sum became 14.
i
= 2. 进入循环。- 添加
table[2][3-2]
到总和。Sum 现在是 8,因为table[2][1]
= 8。 - 减
i
1。 i
= 1. 进入循环。- 添加
table[1][3-1]
到总和。Sum 现在是 14,因为table[1][2]
= 6。 - 减
i
1。 i
= 0。0不大于0,所以我们退出循环。总和变成了14。
Two-dimensional arrays like int[][] table
have two indexes. One for the "outer" array (or rows), and one for the "inner" ones (columns).
二维数组就像int[][] table
有两个索引。一个用于“外部”数组(或行),一个用于“内部”数组(列)。
Let's use int[][] table = {{1,2,3},{4,5,6},{7,8,9}};
from your code as an example:
让我们以int[][] table = {{1,2,3},{4,5,6},{7,8,9}};
您的代码为例:
table[1][2]
: 1 means we should look in the array at index 1, which is {4,5,6}
. 2 means we should look at {4,5,6}
's index 2, which is 6
. In other words table[1][2] == 6
.
table[1][2]
: 1 意味着我们应该在索引 1 处查看数组,即{4,5,6}
. 2 表示我们应该查看{4,5,6}
的索引 2,即6
。换句话说table[1][2] == 6
。
table[2][0]
: 2 means we should look in the array at index 2, which is {7,8,9}
. 0 means we should look at {7,8,9}
's index 0, which is 7
.
table[2][0]
: 2 意味着我们应该查看索引 2 处的数组,即{7,8,9}
. 0 表示我们应该查看{7,8,9}
的索引 0,即7
。
回答by btgui
for (int i = 2; i > 0; i--)
so starting at 2 it checks if i is greater than zero loops once then i-- subtracts 1 check again still greater than 0 loops again then subtracts 1 again checks if greater than 0 now 0 it is not greater than 0 so stops looping //thus loops 2 times
所以从 2 开始它检查 i 是否大于零循环一次然后 i-- 再次减去 1 检查仍然大于 0 再次循环然后再次减去 1 检查是否大于 0 现在 0 它不大于 0 所以停止循环 //因此循环2次
//[0] = 1st [1] = 2nd [2] = third ...
//counting in code starts at zero not 1 so and array of 3 counts a the spaces 0,1,2
int sum = 0;//sum starts at zero
//using the value of i translates as such
sum += table[2][3-2];//[2][1]this is 3rd group 2nd part so sum += 8
//then
sum += table[1][3-1];//[1][2]this is 2nd group 3rd part so sum += 6
0 + 8 + 6 = 14
0 + 8 + 6 = 14
回答by Naji Kadri
Here's how to add elements in a 2D array in a easy way.
Firstwhen you initialize a 2D array think of the first brackets [ ] as a column and the second bracket [ ] as column rows.
For example: int[][] num = new int[10][5]
which means 10 columns and 5 rows.
下面介绍如何以简单的方式在 2D 数组中添加元素。
首先,当您初始化二维数组时,将第一个括号 [ ] 视为列,将第二个括号 [ ] 视为列行。例如:int[][] num = new int[10][5]
这意味着 10 列和 5 行。
If you want to fill all the elements in a 2D array you must use two forloops:
如果要填充二维数组中的所有元素,则必须使用两个for循环:
int[][] num = new int[10][5];
for (int i =0; i < num.length;i++ ) {
for (int x=0; x < num[0].length;i++) { //we used num[0] because we need the length of the rows not the columns
num[i][x] = //any value you want to assign
}
}