How to create a two-dimensional grid with two-d. an array?

This is my current code:

public static void main(String[] args) {
    int [][] twoD = new int [5][5];

    /*for(int i = 0; i<5; i++){
        for(int j = 0; j<5; j++){
            System.out.print(twoD[i][j] + "");
        }
    }*/

 }   
}

I can not do it. I got confused and I removed the testing part with comments. Just ignore it.

I am trying to get a two-dimensional array as follows:

1 2 3 4 5

2 4 6 8 10

3 6 9 12 15

4 8 12 16 20

5 10 15 20 25

However, I just do not understand. How can I get this result? I am new to java.

+4
source share
7 answers

First you need to populate the array with data, and you forgot System.out.printlnfor each row of the array.

int [][] twoD = new int [5][5];

// populate array with data
for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        twoD[i][j] = (j+1)*(i+1);
    }
}

// print result
for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        System.out.print(twoD[i][j]);
        System.out.print(" ");
    }            
    System.out.println();
}
+2
source

You must also fill in the data:

int[][] arr = new int [5][5];

for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        arr[i][j] = (j+1)*(i+1);
    }
}

And the code for printing:

for(int i = 0; i<5; i++){
    for(int j = 0; j<5; j++){
        System.out.print(arr[i][j] + " ");
    }            
    System.out.println();
}
+2
source

, System.out.println(); ,

for(int i = 0; i<5; i++){
        for(int j = 0; j<5; j++){
            System.out.print(twoD[i][j] + " ");
        }
        System.out.println();
    }
0

, , , , for (j). , , , , , Java ints .

0

, 0, , , . , , , 2D- . , . :

public static void main(String[] args) {
    int [][] twoD = new int [5][5];
    int increment = 1;

    for(int i = 0; i<5; i++){
        for(int j = 0; j<5; j++){
            twoD[i][j] = increment++;
        }
    }
    for(i = 0; i<5; i++){
        for(j = 0; j<5; j++){
            System.out.print(twoD[i][j] + " ");
        }
        System.out.println();
    }
}

2D- ( , increment++ increment, ). .

0
source

see this code

    int[][] twoD = new int[5][5];

    // add values to array
    for (int i = 0; i < 5; i++) {
        int val = 1;
        val = val + i;
        for (int j = 0; j < 5; j++) {
            twoD[i][j] = val * (j + 1);;

        }

    }

    // Print array
    for (int i = 0; i < 5; i++) {
        for (int j = 0; j < 5; j++) {

            System.out.print(twoD[i][j] + "   ");

        }

        System.out.println();
    }
0
source

since others have indicated that you need to print it well to see the pattern, i and j are the indices of your array. However, I see that you have a good template, so just running two loops will not solve the problem. Perhaps something like this will help (without giving the exact answer intentionally)

int [][] twoD = new int [5][5];
int i;
// initialize
int c = 1; int j = 0;
for(c=1; c<5; c++) {
for( i = 1; i<=5; i++){        
        twoD[i-1][c-1] = c*c*i; twoD[c-1][i-1]=c*c*i;
    }
}

for( i = 0; i<5; i++) {    
   for( j = 0; j<5; j++) {   
     System.out.print(twoD[i][j]);System.out.print(" " );
   }
   System.out.println("\n");
}
0
source

Source: https://habr.com/ru/post/1659372/


All Articles