Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!

ernest fakudze
Ranch Hand
Posts: 216
Hi folks! Ok, I've read two books on 2D arrays and I searched this forum too but I'm still in the dark. It appears to me that many authors just gloss over 2D arrays because they think that they are obvious. Well, they are not to me. Please refer me to your best tutorial on 2D arrays or spare some of your precious time to show me some code which is well explained.

Rob Ross
Bartender
Posts: 2205
Do you have specific questions? That would be easier to answer.
A 2D array is first of all, just an Object, like all arrays. It has type of array of array of Foo, where Foo is the underlying type.
For example
int[][] my2DIntArray; //has type of "array of array of int."
List[][] my2DListArray; //has type of "array of array of List."
etc.
(contrast this to a one-dimensional array like
int[] myIntArray; //type of "array of int")
The type is also a neumonic device for conceptualizing what this data structure looks like. A 2-D array is just an array of arrays.
Each element of the array is an array.
int[][] intArray = new int[2][2];
I've just created a two dimensional array. This is an array of int arrays. Say it with me again:
Array of int arrays.
So each element intArray[x] is itself an array of int.
intArray[0] is an array of int
intArray[1] is an array of int.
(Contrast this with one-dimensional arrays. Each element of a one-dimensional array is an object of the array type
int[] anArray = new int[2];
anArray[0] is an int
anArray[1] is an int)
From our above 2-D example,
intArray[0] is an array of int
intArray[1] is an array of int.
since intArray[0] is an array of int,
intArray[0][0] is an int
intArray[0][1] is another int
intArray[0][2] is: well, nothing, this throws an ArrayIndexOutOfBoundsException. I only allocated 2 elements when I declared this. In fact, EVERY element of inArray is a two-item int array
recall : int[][] intArray = new int[2][2];
If I want to have each element in intArray be a three element int array, I would have written:
int[][] intArray = new int[2][3];
If I try to write
inArray[2], I also get an ArrayIndexOutOfBoundsException, because my intArray is exactly 2 elements long (and each element is an array of int).
Now, a little wrinkle...
when you delcare two dimensional arrays, the compiler is doing a lot of work for you. BUT, it also does the simple case, where each element of your array (intArray) has the exact same size. If you define your own arrays however, you're not limited to this.
For example:
int[][] intArray = new int[3][];
I have just created an array of int arrays, as before. However, each element of intArray is now a null reference! I haven't actually created the arrays for each element in intArray as I did earlier. But I can do so after the fact.
intArray[0] = new int[2];
intArray[1] = new int[2];
Since each element of intArray is an array of int, I am creating a new array of int and assigning to an alement of intArray, and all is well.
But, I can also do this:
intArray[0] = new int[23];
intArray[1] = new int[5];
Now, each element of intArray is still an array of ints, BUT each array has a different size!!
I can now write:
intArray[0][15]
sice that refers to the 16th element (indexes are zero-based) of the first array in intArray.
But this:
intArray[1][15]
produces another ArrayIndexOutOfBoundsException, because the second array in intArray is only a 5 element array.
Hopefully this helps a bit.
Now, do you have any other specific questions?
[ April 02, 2002: Message edited by: Rob Ross ]

ernest fakudze
Ranch Hand
Posts: 216
Thank you very much Rob. Actually, my main problem was that I didn't know how to reference each element in the array. Now I know. I have cut+pasted your explanation to a word file so that I can read, re-read it later. You have said a lot there and I'm sure that now things should be clear. Again, I really appreciate buddy!!