# Array question

• 06-09-2010, 01:43 PM
TaxpayersMoney
Array question

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?
• 06-09-2010, 01:57 PM
Lil_Aziz1
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:
Code:

`int myAnswer = myArray[1][0]`
you would get: 4
• 06-09-2010, 02:05 PM
JosAH
Quote:

Originally Posted by TaxpayersMoney

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:

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
• 06-09-2010, 02:05 PM
j2me64
Quote:

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

look at the following code

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

Quote:

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}};
• 06-10-2010, 10:41 PM
TaxpayersMoney
Thanks everyone.
• 06-11-2010, 02:41 AM
al_Marshy_1981
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