|
|
Language Fundamentals - Arrays
Array declarations
- arrays are Java objects
- all Java arrays are technically one-dimensional. Two-dimensional arrays are arrays of arrays.
- declaring an array does not create an array object or allocate space in memory; it creates a variable with a reference to an array
- array variable declarations must indicate a dimension by using []
Examples of valid array declarations: (JJ pg84)
String[]s;
String []s;
String [] s;
String [ ] s; // extra white space ignored
String[] s;
String[ ] s; // extra white space ignored
String s[];
String s [];
String s [ ]; // extra white space ignored
String[] s[];
String[][]s;
String s [] [ ]; // extra white space ignored
- declaring the size of the array with the following notation is illegal
String[5] s; // illegal declaration
- the standard convention for declaring arrays is:
String[] s; // one-dimensional array
String[][] s; // two-dimensional array
Initializing arrays
class TestArray {
int[] arr; // member declaration, initialized to 'null'
public static void main(String[] args) {
int[] arr1; // reference variable 'arr1' not initialized
// compiles ok
System.out.println("arr:" + new TestArray().arr);
// compile error
System.out.println("arr1: " + arr1);
}
}
- as arrays are allocated at runtime, you can use a variable to set their dimension
int arrSize = 100;
String[] myArray = new String[arrSize];
- you can use curly braces {} as part of an array declaration to initialize the array
String[] oneDimArray = { "abc","def","xyz" };
| Note |
- Curly braces {} can only be used in array declaration statements.
String[] s;
// illegal initialization
s = { "abc", "def", "hij");
int[] arr = new int[] {1,2,3}; // legal
|
Initializing two-dimensional arrays
- the first dimension represents the rows, the second dimension, the columns
- curly braces {} may also be used to initialize two dimensional arrays. Again they are only valid in array declaration statements.
int[][] twoDimArray = { {1,2,3}, {4,5,6}, {7,8,9} };
- you can initialize the row dimension without initializing the columns but not vice versa
int[][] myArray = new int[5][];
// illegal
int[][] myArray = new int[][5];
- the length of the columns can vary
class TestTwoDimArrays {
// initialize # of rows
static int [][] myArray = new int[3][];
public static void main(String[] args) {
myArray[0] = new int[3]; // initialize # of cols
myArray[1] = new int[4]; // in each row
myArray[2] = new int[5];
for(int i=0; i<3; i++) // fill and print the array
fillArray(i, i+3);
System.out.println();
} // end main()
private static void fillArray(int row, int col) {
for( int i=0; i<col; i++)
myArray[row][i] = i;
for( int i=0; i<col; i++)
System.out.print(myArray[row][i]);
System.out.println();
}
}
Output of TestTwoDimArrays:
012
0123
01234
Also see
Sun Tech Tip: Manipulating Java Arrays
Code Examples
Tips
- array index operator [] has highest level of precedence
- integer variables can be used as array dimension values
Traps
- incorrect array declaration statements, particularly:
arrayType [#] varName;
- incorrect array initialization statements, particularly:
arrayType[] varName = new arrayType[2];
varName = { value, value, value };
- negative values for array index
- long value for array index
- array declaration used as an array creation statement
|