2

So, basically I need to print out a 2d array as a table and put indexes "around" it.

Random rnd = new Random();

    int[][] array = new int[5][5];
    for (int row = 0; row < array.length; row++) {
        for (int col = 0; col < array[row].length; col++) {

            array[row][col] = rnd.nextInt(6);
        }
    }
    for (int row = 0; row < array.length; row++) {

        for (int col = 0; col < array[row].length; col++) {

            if (col < 1) {
                System.out.print(row+" ");
                System.out.print(" " + array[row][col] + " ");
            } else {

                System.out.print(" " + array[row][col] + " ");
            }

        }
        System.out.println();
    }

}

I get this:

0  2  4  0  2  4

1  1  2  0  2  2 

2  0  1  5  4  0 

3  4  2  1  4  1 

4  2  4  3  1  3 

So the first (left) column are indexes and I need to put another column of indexes (0,1,2,3,4) on top of the "table" with "counting" starting from the second column...something like this:

  0 1 2 3 4
0 2 4 0 2 4
1 1 2 0 2 2
2 0 1 5 4 0
3 4 2 1 4 1
4 2 4 3 1 3

Sorry for any mistakes, its my first time asking here.

Marck Kopp
  • 23
  • 4

3 Answers3

1

Make a for loop before the nested for loop:

Random rnd = new Random();

final int COLUMNS = 5;
final int ROWS = 5;

int[][] array = new int[COLUMNS][ROWS];
for (int row = 0; row < array.length; row++) {
    for (int col = 0; col < array[row].length; col++) {

        array[row][col] = rnd.nextInt(6);
    }
}
System.out.print(" ");
for (int row = 0; row < array.length; row++) {
  System.out.print("  " + row );
}
System.out.println();
for (int row = 0; row < array.length; row++) {
    for (int col = 0; col < array[row].length; col++) {
        if (col < 1) {
            System.out.print(row);
            System.out.print("  " + array[row][col]);
        } else {

            System.out.print("  " + array[row][col]);
        }
    }
    System.out.println();
}

output:

   0  1  2  3  4
0  2  3  1  0  2
1  0  1  1  5  2
2  1  0  3  3  5
3  1  4  0  5  2
4  1  0  3  3  3
fab
  • 1,189
  • 12
  • 21
  • thank you very much, it works just fine. It only needs some cosmetics to look like a table(tabulation and stuff) – Marck Kopp Mar 22 '18 at 12:32
  • It looks like your example, doesn't it? Can you edit your example, how it should look like? – fab Mar 22 '18 at 12:34
  • it should look like this: well I have an example in my question post but in Your example the top line is sort of squished (narrow) so its not in order . Sorry I dont know how to add code in comments – Marck Kopp Mar 22 '18 at 12:36
  • @MarckKopp better? – fab Mar 22 '18 at 12:49
0

You are only missing another for before the nested fors in which you print the table, where to print out first an empty character, after which the indexes needed. Otherwise, the code seems correct.

System.out.print(" ");
for(int i=0;i<cols;i++){
    System.out.print(" "+i+" ");
}
Claudiu Guja
  • 290
  • 1
  • 3
  • 12
0

Your final printed tabs has a size bigger by one than your array. So first, loop for the length of array + 1. To do this change < condition by <=.

Then you can simply identify the different cases you are going through :

  1. Top right corner is identify by row==0 and col==0. In this case just print a space
  2. First row is identify by row==0 and col!=0. In this case just print the column number in your array : col-1
  3. First column is identify by col==0 and row!=0. In this case just print the row number in your array : row-1
  4. All other values, just print them from your array : array[col-1][row-1].

You should obtain this :

for (int row = 0; row <= ROWS; row++) {
    for (int col = 0; col <= COLUMNS; col++) {
        if (col == 0 && row == 0) {  //Top right corner
            System.out.print(" ");
        } else if (row == 0) { //First row
            System.out.print(" " + (col-1));
        } else if (col == 0) { //First column
            System.out.print(" " + (row-1));
        } else { //All other cases
            System.out.print(" " + array[row-1][col-1]);
        }
    }
    System.out.println();
}
vincrichaud
  • 2,218
  • 17
  • 34
  • thank you, for some reason the line System.out.print(" " + col-1); gets me an error, so if I remove the " "+ off it, it works, but I get another error "ArrayIndexOutOfBoundsException: 5" – Marck Kopp Mar 22 '18 at 12:34
  • @MarckKopp Try by putting parenthesis around `(col-1)`. If it still doesn't work use `String.valueOf(col-1)`. – vincrichaud Mar 22 '18 at 12:39
  • @MarckKopp You have this due to the second loop condition `col <= array[row].length` it should be `col <= array[col-1].length`. I edit my answer – vincrichaud Mar 22 '18 at 12:40
  • it gives me "java.lang.ArrayIndexOutOfBoundsException: -1" error – Marck Kopp Mar 22 '18 at 12:47
  • @MarckKopp This is due to the first loop, when row=0 it can't access to array[row-1]. Hopefully in your case you have constants to define number of rows and column. So you can avoid this. See the edit – vincrichaud Mar 22 '18 at 12:50
  • In case the number of column is different for each rows, You'll have to handle the case 1 and 2 outside the second loop. – vincrichaud Mar 22 '18 at 12:52
  • Yes, it works. Thank You for your time, and yes I realize the jagged array case's – Marck Kopp Mar 22 '18 at 12:58