We can refer to each individual element of the array using an element's index enclosed in brackets.
The index for the first element is 0, the second element 1, the third element 2, and so on.
This is called zero-based indexing. The index for the last element of an array is the length of the array minus 1.
If we have an array of length 5, the indexes of the array elements would be 0, 1, 2, 3, and 4.
It is a runtime error to refer to a non-existing element of an array.
For example, using myID[5] in the code will throw an exception, because myID has a length of 5 and myID[5] refers to the sixth element, which is non-existent.
We can assign values to elements of an array as follows:
myID[0] = 10; // Assign 10 to the first element of myID myID[1] = 20; // Assign 20 to the second element of myID myID[2] = 30; // Assign 30 to the third element of myID myID[3] = 40; // Assign 40 to the fourth element of myID myID[4] = 50; // Assign 50 to the fifth element of myID
The following statement assigns the value of the third element of the myID array to an int variable temp:
int temp = myID[2]; // Assigns 30 to temp
An array object has a public final instance variable named length, which contains the number of elements in the array.
int[] myID = new int[5]; // Create an array of length 5 int len = myID.length; // 5 will be assigned to len
length
is the property of the array object.
Until we create the array object, we cannot use its length property.
Typically, elements of arrays are accessed using loops.
for (int i = 0 ; i < myID.length; i++) {
myID[i] = (i + 1) * 10;
}
Array elements are always initialized.
Array elements of primitive data type are initialized to the default value for their data types.
For example, the numeric array elements are initialized to zero, boolean elements to false, and the reference type elements to null.
The following code illustrates the array initialization:
intArray[0], intArray[1] and intArray[2] are initialized to zero by default.
int[] intArray = new int[3];
bArray[0] and bArray[1] are initialized to false.
boolean[] bArray = new boolean[2];
An example of a reference type array. strArray[0] and strArray[1] are initialized to null.
String[] strArray = new String[2]
All 100 elements of the person array are initialized to null.
Person[] person = new Person[100];
The following illustrates the array initialization for an instance variable and some local variables.
public class Main { private boolean[] bArray = new boolean[3]; // An instance variable /* www .j a v a 2 s . c o m*/ public Main() { for (int i = 0; i < bArray.length; i++) { System.out.println("bArray[" + i + "]:" + bArray[i]); } } public static void main(String[] args) { int[] myID = new int[3]; // A local array variable for (int i = 0; i < myID.length; i++) { System.out.println("myID[" + i + "]:" + myID[i]); } String[] name = new String[3]; // A local array variable for (int i = 0; i < name.length; i++) { System.out.println("name[" + i + "]:" + name[i]); } } }
The code above generates the following result.