CS 051 | Fall 2012 |
See class demos DrawingProgram and ArrayListScribbler
Notice that in each case we do not need to keep track of the number of elements in the ArrayList
as the data structure keeps track of it itself. Notice also in the first program that we can just
send the delete message to an array list with the index and it will automatically shift
everything over to fill the gap. While we only add at the end of the array here, it will also
allow us to add something in th middle and shove all of the old elements over to make space.
So, can we create an array of arrays? The answer is yes. As an example, we'll look at the syntax
to declare and create a 2-dimensional array.
     int [][] a = new int[2][3];
We can think of a 2-dimensional array as a table, where the cells in the table can be referenced
for our above example as follows:
column 0 1 2 row 0 a[0][0] a[0][1] a[0][2] 1 a[1][0] a[1][1] a[1][2]
Typically (though not always) we use the first number in our instantiation
to represent the number of rows, and the second number to represent the number
of columns in the array.
To demonstrate 2-dimensional arrays, we looked at the class example
Tic-Tac-Toe and started
to examine another example
MagicSquares.