0

How would I go about designing something like this, using 2D arrays in java?

    A  B  C

    15 15 200
    20 20 200
    25 25 200
    30 30 200
    35 35 200
    40 40 200
    45 45 200
    50 50 200
    55 55 200
    60 60 200

      int[] A = { 15, 20, 25, 30, 35, 40, 45, 50, 55, 60 };
      int[][] name = new int[3][10];

       for (int i = 0; i < 3; i++) {
       for (int j = 0; j < 10; j++) {

       name[i][j] = A[i]; // this prints out fine
       name[i][j] = A[i]; // this also prints out fine
       name[i][j] = 200; // but when I put this piece of code, it doesn't print the two 
        //above ones but instead it prints 200 on a 10 row by 3` column table.        



        for (int j = 0; j < 10; j++) 
        System.out.println(name[0][j] + " " + name[1][j] + " " + name[2][j]);


}
}
}

Everything works but "name[i][j] = 200;" when i put this, it only prints this and nothing else

Undo
  • 25,519
  • 37
  • 106
  • 129
  • it is meant to be 10 by 3 (10 row and 3 columns) and on the first line it should contain 15 15 200 and on the second line 20 20 200 and on the third line 25 25 200 and so on – user2977015 Nov 10 '13 at 20:24
  • http://stackoverflow.com/questions/12231453/creating-two-dimensional-array – TT_ stands with Russia Nov 10 '13 at 20:25
  • 1
    Please don't edit your questions to non-questions. Even if your problem has been solved, it may still be useful to someone else. I rolled back some of the edits. – laalto Nov 20 '13 at 07:17

2 Answers2

0
new int[][] { 
  { 15, 15, 200 },
  { 20, 20, 200 },
  { 25, 25, 200 },
  { 30, 30, 200 },
  { 35, 35, 200 },
  { 40, 40, 200 },
  { 45, 45, 200 },
  { 50, 50, 200 },
  { 55, 55, 200 },
  { 60, 60, 200 } };
sebtic
  • 198
  • 1
  • 6
0
int[][] name = new int[x][y];

You would replace name with what you would like to name the array and you would replace x and y with the x and y lengths of the array, in your case it would be 3 for x and 10 for y.

If you would like to create a 2d array for another type such as Strings, char, etc. you would replace int with that type of variable so it would be

String[][] = new String[x][y];

It'll come out how you want if you print it properly. Check out this example, it should be what you are looking for.

int[][] name = new int[3][10];

for (int i = 0; i < 3; i++) {
    for (int j = 0; j < 10; j++) {
        name[i][j] = 0;
    }
}

for (int j = 0; j < 10; j++) 
    System.out.println(name[0][j] + " " + name[1][j] + " " + name[2][j]);

Read more about arrays here in the java docs.

user2612619
  • 1,119
  • 3
  • 11
  • 28