Usage of Arrays

To reference an element within an array, use the [] operator. This operator takes an int operand and returns the element at that index. Remember that array indices start with zero, so the first element is referenced with index 0.

In most cases, a program will not know which elements in an array are of interest. Finding the elements that a program wants to manipulate requires the program loop through the array with the for construct and examine each element in the array.


Java Example

String[] months = {"Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"};

int monthIndex = 3; String thisMonth = months[monthIndex];
System.out.println("This month is: " + thisMonth);

//use the length attribute to get the number
//of elements in an array
for(int i = 0; i < months.length; i++ ) {
System.out.println("month: " + month[i]);
}


Continue to see the results of this snippet.