Java Arrays
Java arrays fall between a primitive data type and an object
Properties of Java arrays:
-
can contain objects, primitive types, or other arrays
-
contents are accessed via zero-based positive integers enclosed within
the brackets, [, and ]
-
declaration, creation and, value initialisation of arrays are distinct
operations
-
created with the new operator
-
uninitialised arrays have a value of null
-
have a single instance variable called 'length'
-
checks at run time, that all accesses are between 0 and length - 1
-
passed to methods by reference
byte buffer[]; // array declaration (buffer = null)
buffer = new byte[1024]; // array creation (contents initialised to zeros)
int table[] = new int[10]; // declaration and creation combined
int sqrs[] = {0, 1, 4, 9, 16, 25, 36, 49, 64, 81 }; // with an initializer
buffer[5] = sqrs[sqrs.length-1]; // array references
int triangle[][] = new int[3][]; // 2D Arrays do not have to be matrix
triangle[0] = new int[3];
triangle[1] = new int[2];
triangle[2] = new int[1];