Java provides a Collections Framework, a set of classes in the java.util package, that enable the programmer to store and manipulate data in various ways. This library makes it very easy for the programmer to create and manipulate collections of information. Additionally, the implementations are optimized for efficiency. Many other programming languages also support a similar library.
Though having such a library makes life easier for the programmer, it is
extremely important that students learn how such libraries are implemented.
The array is the most basic type of collection. Though other types of collections are easier to use, we'll begin with arrays. It is very important for you to understand how to use and manipulate arrays.
An array allows the programmer to store a list of elements, all of the same type, in a contiguous block of memory. The size of an array must be specified when it is instantiated and cannot change.
An array declaration creates a reference to the first element of the
array. The declaration includes the type of the elements that will be stored
in the array, a name for the array, and the square brackets () specify that
the variable will be an array.
The previous example declares a reference to the array, but does not instantiate the array. Note that the example does not specify the size of the array. To instantiate the array, the new operator must be used.
It is also possible to assign values to each position of an array at the time of instantiation. The initializer list is used for this purpose:
Each array element has a subscript that identifies its position
in the array. It is extremely important to remember that the subscripts begin
at 0. Therefore, an array of size 5 would have subscripts 0, 1, 2, 3, and 4.
To access an array element, specify the name of the array followed by the
element's subscript in square brackets. The subscript may also be represented
by an int variable or expression that results in an int value.
If your program attempts to access an array element that does not exist, you will experience a runtime ArrayIndexOutOfBoundsException. Make sure that the number you place between the square brackets is a valid index for the given array.
Arrays and for loops go hand in hand. It is quite common to use a for loop to iterate through every element of an array and perform a specific action. The example below initializes every element of the array numbers to 100.
Notice that the length of an array can be retrieved by using the dot operator to access the member length. However, it is important to note that the length of an array is the total number of elements one can store in the array. It is certainly possible that the number of valid elements in an array is smaller than the total number of elements the array can store. In this case, it is common to keep a separate integer variable to represent the number of valid elements in an array as shown below.
A single array element can be passed to a method, as can an entire array. If an entire array is passed into a method, a reference to the original array is passed. As a result, any changes made to the array by the method will be reflected in the caller.
Each element of an array may be an object, not simply a primitive type. In
this case, the array variable is a reference to a list of references. Each
element of the array is a reference to an object. When the array itself is
created, the objects are not instantiated (as shown below).
Below, we create two new objects of type Name and place their addresses in positions 0 and 1 of the array, respectively.
Often, it is necessary to maintain a sorted array of elements. In this
case, adding a new element to the array can be quite a challenge. If the
element's value dictates that it should be inserted anywhere but in the
rightmost position, the insert procedure must shift the the right all
elements that come after the element to be inserted. The following example
implements a procedure to insert an element in the first position of the
Notice that the method returns in the case that the array is full. Clearly, this solution is less than optimal. It is quite common to implement such an insertion procedure such that if the array is full, the method will allocate a new array larger than the current array and copy all of the elements from the current array into the new array. Keep in mind that the reference to the old array must then be changed to refer to the new array.
Similar to insertion, deletion of elements from a sorted array requires
that all elements to the right of the element removed be moved to the left
one position. The following example illustrates a procedure to delete the
element at position 0 of an array.
Java also supports two-dimensional arrays. All of the data in a
two-dimensional array must be of the same type. However, you can imagine the
data stored in a matrix of several rows and columns. To declare a
two-dimensional array, you simply use two sets of square brackets. Similarly,
the access and modify elements of a two-dimensional array, you must specify
the element's row and column.