An array is a collection of variables of the same data type. Arrays can be primitive data types (int, double, boolean), reference types (String, Object), or even reference types of programmer defined classes (Student, ClassA). Arrays can be used as local variables, instance variables (a.k.a. fields), and as parameters.
A Java array variable is declared by stating the data type followed by square brackets [ ].
Example:
int[] nums; \\ this is an array declaration.
Declaring an array creates a variable to hold a pointer to the array after it is instantiated.
nums = new int[10]; \\ this statement instantiates the array.
Instantiating an array allocates the necessary memory for the specified data type and size of the array. In this example, the size of the array is 10.
Examples of different ways that an array can be instantiated.
int[] arr = new int[]{2,4,6,8}; \\ original Java initializer list
double[] values = {1.14, 2.718, 3.1415} \\ latest initializer list
An initializer list instantiates an array with predefined values. Notice the two versions shown above, both are valid, but the latter is preferred. Initializer lists are also called array literals. This technique can be used with primitive variables and Strings. Other objects and class variables would first need to be instantiated before being included in an initializer list.
String[] strArray = new String[5]; \\declare and instantiate
String[] names = {“Ben”, “Jack”, “Alice”}; \\initializer of Strings
String objects are the only reference type that can be instantiated within an array.
The following example shows the necessary step of instantiating other reference types before including them in an array initializer list.
Object obj1 = new Object();
Object obj2 = new Object();
Object[] objArray = {obj1, obj2};
However, we could just declare an array and assign objects as needed by index.
Object[] objArray = new Object[10];
Object obj1 = new Object();
objArray[0] = obj1;
Accessing Arrays by Index
Like Strings, arrays use indices where the initial location has an index equal to zero. An array with length equal to five has indices of 0 – 4. Consider the integer array below.
int[] intArr = {-1, 3, 10, 8, 12];
The image above is depicting the array intArr we can see that: intArr[2] = 10 and intArr[4] = 12. However if we tried to access intArray[5]would result in an ArrayIndexOutOfBoundsException Error. Such an error would not be picked up by the compiler, so it is considered a runtime error.
Just as values can be accessed by index, similarly, values can be assigned by index.
int[] nums = new int[3];
nums[0] = -1;
nums[1] = 2;
nums[2] = 0;
Iterations are a useful technique for accessing and populating arrays. Consider the following code:
for(int i = 0; i < arr.length; i ++)
{
arr[i] = 2*i; \\ populates array with ascending even int
}
Notice i is used to access each array location by index in order store the generated values, The use of i < arr.length, accesses the variable length which is an instance variable of the array object and retrieves the length of the array so the expression can set the max value for i such that it is less than the array length, preventing an ArrayIndexOutOfBounds Error. Alternatively, the next for-loop decrements from arr.length – 1 to zero instead of incrementing from the zero index to arr.length – 1.
for(int i = arr.length - 1; i >= 0; i--)
{
arr[i] = (2*i + 1); \\ populates array with descending odd int
}