Using Java Arrays
In computer terms, an array is a type of data structure consisting of a group of elements that are accessed by indexing.
Each element has the same data type and the array occupies a contiguous area of storage.
Java is among several programming languages that support arrays, generalizing operations and functions to work transparently over arrays instead of requiring looping over array members.
Program data can be stored in variables, each of which has an identifier, a type, and a scope. When you have closely-related data of the same type and scope, it is often convenient to store it together in arrays instead of in individual variables.
An array implicitly extends "java.lang.Object" so an array is an instance of Object, but they are directly supported language features. This means that their performance would also work well in earlier versions of Java and that they have a unique syntax that is completely different from Objects.
How Java arrays are structured - The Java array has seven elements, each holding a distinct value. The first element is always indexed as 0, this means that the index of the last element is always the array's length minus one.
How Java array is declared - An array variable is declared just like any Java variable: it has a type and a valid identifier. The type is the element or elements contained in the array. Take note that the [] notation denotes that the variable is an array. For instance:
int[] counts;
String[] names;
int[][]matrix; //this is an array of arrays
How Java is initialized - Once an array has been declared, memory can be allocated to it through a new operator. This new operator is followed by the type, and the number of elements to allocate, which is placed within the [] operator. For example:
counts = new int[5];
names = new String[100];
matrix = new int[5][];
Another syntax is available for declaring and initializing an array, wherein the length of the array is implicitly defined by the number of elements included within the brackets.
String[] colors = {"red", "orange", "yellow", "green", "blue", "indigo", "violet"};
How arrays are used - To refer an element with an array, the [] operator should be used. This takes an "int" operand and returns the element at that index. Take note that array indices start with zero.
int month = months[5]; //get the sixth month (June)
A program may not know which elements in an array are of interest. To find the element that a program wants to manipulate, the program loop through with the "for" keyword is required to construct and examine each element in the array.
String months[] =
{"January", "February", "March", "April", "May", "June",
{"July", "August", "September", "October", "November", "December"}
//use the length attribute to get the number
//of elements in an array
for(int i = 0; i
System.out.println("month: "+ month[i]);
}
In Java 5.0, the enhanced "for" loop make this even easier:
String months[] =
{"January", "February", "March", "April", "May", "June",
{"July", "August", "September", "October", "November", "December"};
// Shortcut syntax loops through array months
// and assigns the next element to variable month
// for each pass through the loop
for(String month: months){
System.out.println("month: " + month);
}
