Hi,
I'm new to Java programming, and trying to thoroughly understand multidimensional arrays and how they work.
If I entered the following, it appears the following multi array (table) would be created:
int[][] table = {{44,22,43},
{55,39,88},
{92,40,99}};
----------------
| 44 | 22 | 43 |
---------------
| 55 | 39 | 88 |
---------------
| 92 | 40 | 99 |
---------------
Then if I later called:
System.out.println(table[0][2]);
This would print out row 1, column 3: 43.
Another option (to do the same thing):
int[][] table = new int[3][3];
Then, I could assign an array to a variable, like this:
int[] t0 = { 44, 22, 43 };
int[] t1 = { 55, 39, 88 };
int[] t2 = { 92, 40, 99 };
...and I could then assign each variable to my original array like this:
table[0] = t0;
table[1] = t1;
table[2] = t2;
I have 2 questions:
1. Is there an advantage to either approach?
2. With the latter approach I've tried assigning the arrays directly to the original variable without success. For example:
table[0] = { 44, 22, 43 };
This produces an error though (i.e. Illegal start of expression). Is there a reason I need to create temp variables for each array before adding it to the main array (i.e. table)?
Thanks