S Personal
S Personal

Reputation: 43

How to create two dimensional grid with two-d. 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't seem to do it. I got confused and I removed the part of testing w/commenting. Just ignore that.

I am aiming to get a two dimensional array like this:

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 don't get it. How can I get that result? I'm a beginner at java.

Upvotes: 4

Views: 1297

Answers (7)

Crazy Programmer
Crazy Programmer

Reputation: 21

You have to populate the data as well:

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 to print would be:

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

Upvotes: 2

Rohit Gupta
Rohit Gupta

Reputation: 1

as others pointed out you need to print it nicely to see the pattern, i and j are indices of your array. However, I see that you have a nice pattern so just running two loops won't solve the problem. Maybe something like this would help (not giving 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");
}

Upvotes: 0

J-J
J-J

Reputation: 5871

refer 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();
    }

Upvotes: 0

Jimmy M.
Jimmy M.

Reputation: 325

The array doesn't just automatically populate with incrementing integers, rather each cell of the array will automatically initialized to 0, you have to set the values you want the array to contain. You can use the concept of your testing class to do this if you wish, just set each cell of the 2D array to a certain value. After that, you can print out the array, making sure to print a each row of the array on a new line. For instance:

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();
    }
}

The first set of nested for loops will set each of the cells of the 2D array to the incremented integers you want (note increment++ will first set the cell to the value increment currently is, then add one to the variable). The second set of nested for loops will print out the array as you desire.

Upvotes: 0

Niyoko
Niyoko

Reputation: 7662

First, you need to populate the array with data, and you forgot System.out.println for 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();
}

Upvotes: 2

Brian
Brian

Reputation: 1

You are on the right track, that for loop will print out the array like that, all you need to do is print a new line character after finishing the for(j) loop. But, at least in the snippet you posted, you aren't actually doing any assignments, so there aren't any values in your array to print, Java will initialize all ints to zero for you.

Upvotes: 0

Manuel Mejias
Manuel Mejias

Reputation: 326

You are doing fine, you just need to put line jump System.out.println(); every time the second for ends

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

Upvotes: 0

Related Questions