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. I would really appreciate your help and thanks in advance.
In a time of drastic change it is the learners who inherit the future. The learned usually find themselves equipped to live in a world that no longer exists.<br />Eric Hoffer
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; 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 is an array of int intArray 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; anArray is an int anArray is an int) From our above 2-D example, intArray is an array of int intArray is an array of int. since intArray is an array of int, intArray is an int intArray is another int intArray 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; If I want to have each element in intArray be a three element int array, I would have written: int intArray = new int; If I try to write inArray, 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; 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 = new int; intArray = new int; 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 = new int; intArray = new int; Now, each element of intArray is still an array of ints, BUT each array has a different size!! I can now write: intArray sice that refers to the 16th element (indexes are zero-based) of the first array in intArray. But this: intArray 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 ]
Joined: Aug 27, 2001
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!!