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

3
  • 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 Commented Nov 10, 2013 at 20:24
  • stackoverflow.com/questions/12231453/… Commented Nov 10, 2013 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. Commented Nov 20, 2013 at 7:17

2 Answers 2

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 } };
Sign up to request clarification or add additional context in comments.

Comments

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.

8 Comments

Hi, thanks for the reply, would it possible to use for loops? or nested for loops.
I have this atm, pasted above
Then what's the problem?
it prints them in a line downwards and not in 10 row by 3 column
Yes you can use loops, check out my updated example.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.