Arrays
Arrays are objects, but have a special syntax for creating and indexing, more like C and C++.
- int states [ ]; declares the states variable to be a 1D array of ints
- states = new int [ 128 ]; creates the new 1D array of length 128
- states [ 27 ] = i + 1; assigning to an element
For an array of length n, indices run from 0 to n-1. There is run-time checking of indices.
Multi-dimensional arrays are arrays of arrays
- char states [ ] [ ] = new char [12 ] [ 24 ]; delaring and creating at the same time
- they can also be “ragged”
- Note that different rows or columns may not be assumed to be stored contiguously in memory.
Arrays can have values that are any type of object
- Color hues = new Color [ 1024 ];