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 and creation of arrays are distinct operations
- created with the
new operator
- uninitialized 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 initialized 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][];
triangle[0] = new int[3];
triangle[1] = new int[2];
triangle[2] = new int[1];
|