• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Tim Cooke
  • Devaka Cooray
Sheriffs:
  • Liutauras Vilda
  • paul wheaton
  • Rob Spoor
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
Bartenders:
  • Carey Brown
  • Roland Mueller

2D Arrays giving me hell, please help!!

 
Ranch Hand
Posts: 216
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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!!
 
Bring me the box labeled "thinking cap" ... and then read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/wiki/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic