Java: Summary: Arrays 1

Array Summary - main points
Purpose To save large numbers of data elements, either primitives or objects.
UniversalAlmost all programming languages have arrays.
Array Declaration
Same type All elements in an array must be the same type, either primitive or object.
DeclarationPrecede variable with element type and square brackets. Eg,
int[] scores;
AllocationMemory to store array elements must be allocated with new. Eg,
int[] scores;                // Declares that scores is an array of integers.
scores = new int[12];        // Allocate memory to hold up to 12 score values.
Combined It's very common to combine the declaration with the allocation.
int[] scores = new int[12];  // Declare and allocate memory in one statement.
Fixed size When memory is allocated for an array, it is a fixed size block. It does not expand.
Plural namesThe most common naming advice is to use plural names or collective nouns.
References Array variables are references to array objects. Two variables can reference the same array.
Array initial values
zero/null/false If no initial values are specified for array elements (see below), array elements are initialized to zero for numbers, null for object references, and false for booleans. This is the same as for all object fields.
Initialization A curly brace surrounded list of values can be specified on the declaration. This allocates exactly enough space to hold the array. The following are equivalent.
String[] names = {"Mickey", "Minnie", "Donald"};
  
String[] names = new String[3];
names[0] = "Mickey";
names[1] = "Minnie";
names[2] = "Donald";
Subscripts - Accessing elements of the array
Integers Selecting a particular position in an array is done by specifying an integer value.
enums Enum values may also be used in index an array.
Square bracketsSubscripts are written after an array variable, enclosed in square brackets.
scores[5] = 86;   // Assigns the value 86 as the 5th value.
scores[ss]++;     // Increments the score of element whose number is in ss.
Start at 0 The first subscript value is zero, not one.
Bounds check ArrayIndexOutOfBoundsException is thrown if subscripts are out of the legal range.
Names When a subscript has no meaning, it's common to use i, j, k. Otherwise use a descriptive name.
Iterating over an array
Length/sizeThe size of an array can be found by referencing the length field, eg, scores.length .
For loopsFor loops are the most common way to iterate over array elements. The normal for loop is good for going forwards, backwards, subranges, comparing two elements, or on every nth element. If you are just going forward over the entire array, the enhanced for loop is more readable.
//... Using standard for loop.
int[] scores = new int[12];
. . . Set values in the scores array.
int total = 0;
for (int i = 0; i < scores.length; i++) {
    total += scores[i];
}
  
//... Using enhanced for loop.
int[] scores = new int[12];
. . . Set values in the scores array.
int total = 0;
for (int scr : scores) {
    total += scr;
}

See Summary: Arrays 2 for additional topics such as multi-dimensional arrays, predefined array methods, and other more advanced topics.