Johnny Peterson wrote:That sounds very interesting Winston... Where can I read more about that? 
The JLS I guess; it's a basic part of the language.
To set up a rectanglar matrix:
int [][] matrix = new int[3][4];
To access a cell:
int cell = matrix[1][2];
To get the number of rows:
int rows = matrix.length;
To get the number of columns:
int cols = matrix[0].length;
but obviously the latter is only going to work once 'matrix' is initialized.
The main problem for beginners is that an array set up as above
looks and acts like a 2D array, but it's actually an array of arrays.
And that last statement really only works if you've set up the array as above, because it guarantees that every row is the same length.
However, there's nothing to stop you from initializing (or even
changing) each row to have different lengths if you want, so when you're traversing a "2D" array,
you should generally use the
row length (
matrix[r].length).
Luckily there's a simpler alternative for basic traversals: the for-each loop, viz:
and it's guaranteed to work, providing the array is fully initialized, even if rows aren't the same length.
Winston