创建一个2D数组并将每个元素初始化为i * j的值,其中i和j是2个索引(例如,element [5] [3]是5 * 3 = 15)

时间:2022-06-11 02:28:37

I am truly stuck here on how to do this. I got as far as creating the 10x10 array and making variables i and j - not far at all. I thought about the use of loops to initialize every element, but I just don't know how to go about doing it. Any help is appreciated, thanks.

我真的被困在这里如何做到这一点。我创建了10x10数组并创建了变量i和j - 不远处。我想过使用循环来初始化每个元素,但我只是不知道如何去做。任何帮助表示赞赏,谢谢。

public class arrays {

公共类数组{

public static void main(String[] args) {

    int[][] array = new int[10][10];
    int i = 0, j = 0;   
}

}

I was thinking of using a do while loop or for loop.

我正在考虑使用do while循环或for循环。

4 个解决方案

#1


1  

Psuedo-code:

for i = 0 to 9
   for j = 0 to 9
       array[i][j] = i*j

Converting this to Java should be a snap.

将其转换为Java应该很快。

#2


1  

Create two nested for loops, one for i, and one for j, looping over all valid indices. In the body of the inner for loop, assign the computed product to the 2D array element.

创建两个嵌套的for循环,一个用于i,一个用于j,循环遍历所有有效索引。在内部for循环的主体中,将计算产品分配给2D数组元素。

#3


0  

You will need two for loops inside each other:

你需要在彼此内部使用两个for循环:

int[][] array = new int[10][10];
for (int x = 0; x < array.length; ++x)
{
   for (int y = 0; y < array[y].length; ++y)
   {
       int product = x * y;
       // put the value at the right place
   }
}

You can read this as:

你可以这样读:

For each x value, iterate over the ten y values and do...

对于每个x值,迭代十个y值并执行...

#4


0  

 int [][] array = new int[10][10];
 for (int i = 0; i < 10; i++) {
       for (int j = 0; j < 10; j++) {
             //initialize every element
             array[i][j] = i + j; 
           }
     }  

#1


1  

Psuedo-code:

for i = 0 to 9
   for j = 0 to 9
       array[i][j] = i*j

Converting this to Java should be a snap.

将其转换为Java应该很快。

#2


1  

Create two nested for loops, one for i, and one for j, looping over all valid indices. In the body of the inner for loop, assign the computed product to the 2D array element.

创建两个嵌套的for循环,一个用于i,一个用于j,循环遍历所有有效索引。在内部for循环的主体中,将计算产品分配给2D数组元素。

#3


0  

You will need two for loops inside each other:

你需要在彼此内部使用两个for循环:

int[][] array = new int[10][10];
for (int x = 0; x < array.length; ++x)
{
   for (int y = 0; y < array[y].length; ++y)
   {
       int product = x * y;
       // put the value at the right place
   }
}

You can read this as:

你可以这样读:

For each x value, iterate over the ten y values and do...

对于每个x值,迭代十个y值并执行...

#4


0  

 int [][] array = new int[10][10];
 for (int i = 0; i < 10; i++) {
       for (int j = 0; j < 10; j++) {
             //initialize every element
             array[i][j] = i + j; 
           }
     }