1. ## Array question

Java Code:
`int[][] myArray={{1,2,3},{4,5,6}};`
Does this give me a matrix of 2 rows and 3 cols or 3 cols and 2 rows? Or to put it another way if I did:

What value would I get for myAnswer?

2. 2 rows and 3 column. myArray[1][1] would give you the same answer regardless if it's 3 column & 2 rows or 3 rows & 2 columns: 5

Let's say you did:
Java Code:
`int myAnswer = myArray[1][0]`
you would get: 4

3. Originally Posted by TaxpayersMoney

Java Code:
`int[][] myArray={{1,2,3},{4,5,6}};`
Does this give me a matrix of 2 rows and 3 cols or 3 cols and 2 rows? Or to put it another way if I did:

What value would I get for myAnswer?
Java is (just as all C-like languages) row oriented w.r.t. arrays. Try it:

Java Code:
```int[][] myArray= { { 1, 2, 3 }, { 4, 5, 6 } };
printRow(myArray[0]);
printRow(myArray[1]);
...
private static void printRow(int[] row) {
System.out.println(Arrays.toString(row));
}```
AFAIK only Fortran stores its matrixes in a column oriented way ...

kind regards,

Jos

4. Originally Posted by TaxpayersMoney
What value would I get for myAnswer?

look at the following code

Java Code:
```public class ArrayOutput {
public static void main(String[] args) {
int[][] myArray = { { 1, 2, 3 }, { 4, 5, 6 } };

for (int i = 0; i < myArray.length; i++) {
for (int j = 0; j < myArray[i].length; j++)
System.out.println("Array[" + i + "][" + j + "] = "
+ myArray[i][j]);
}
}
}```

that will output each element in the array

Array[0][0] = 1
Array[0][1] = 2
Array[0][2] = 3
Array[1][0] = 4
Array[1][1] = 5
Array[1][2] = 6

now, are you able to guess the output of this array?

int[][] myArray = { { 1 }, { 4, 5, 6 }, {2, 3}};
Last edited by j2me64; 06-09-2010 at 01:08 PM.

5. Thanks everyone.

6. Senior Member
Join Date
Feb 2010
Location
Waterford, Ireland
Posts
748
Rep Power
8
Or you can just think of a multidimensional array as an array inside of an array which is what it is.

or even better you can reduce complexity i.e int[][] myArray = { { 1 }, { 4, 5, 6 }, {2, 3}}; never making an array like this!!lol
Last edited by al_Marshy_1981; 06-11-2010 at 01:43 AM.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•