JAVA Array Notes
JAVA Array Notes
We looked at the basics of Java's built-in arrays, starting from creating an array to iterating over
the elements of an array, as well as at a few typical array manipulations in class today. See the
link to a complete implementation for you to play with at the end of this document.
Topics, in no particular order:
1. Creating an array
2. Iterating over the elements of an array
3. Copying an array
4. Resizing an array
5. Reversing an array
6. Shifing an array left
7. Shifing an array right
8. Inserting an element into an array
9. Removing an element from an array
10. Rotating an array left
11. Rotating an array right
Creating an array
In Java, you create a new array in the following:
Now x refers to an array of 100 Foo references. Note that the array doesn't actually contain the
instances of Foo (the objects that is), but rather hold the references to the objects. We can assign
other references to this array:
Foo[] y = x;
Now y is just another reference to the same array, and any change made through y will change
the array referred to by x.
A Java array has a single attribute called length that gives you the capacity of the array;
x.length for example would produce the value 100 in this case. The capacity of an array is
fixed, and once created, cannot be changed. We "resize" and array by creating a new one with
higher capacity, and copying the elements to the new one. See resizing an array for details.
Back to Table of contents
Iterating over the elements of an array is the same as the following: for each element v in
the array x, do something with v. There are two traditional patterns for iterating over
the elements of an array: using a while or a for loop. Let's print the elements of the array x
using a while loop (here the doing something is printing the element):
int i = 0;
while (i < x.length) {
System.out.println(x[i]);
i++;
}
Java has another form of the for loop to implement this "foreach" pattern:
for (Foo v : x)
System.out.println(v);
This version of the for has one advantage: it does exactly what it says - for each element v in the
array x, it prints the element v! No indexing needed at all. It also has a disadvantage: there is no
way to iterate over a part of the array, which is important when the size is not equal to the
capacity of the array.
Back to Table of contents
Copying an array
Copying the elements of a source array to destination array is simply a matter of copying the
array element by element using an iterator.
public static Object[] copyArray(Object[] source) {
Object[] copy = new Object[source.length];
for (int i = 0; i < source.length; i++)
copy[i] = source[i];
return copy;
}
For your information, the java.util.Arrays class provides a set of methods to do just this for
you in a very efficient way. Here's how:
public static Object[] copyArray(Object[] source) {
Object[] copy = java.util.Arrays.copyOf(source, source.length);
return copy;
}
Resizing an array
There is the classic problem of arrays - once created, it cannot change its capacity! The only way
to "resize" an array is to first create a new and larger array, and copy the existing elements to the
new array. The following static method resizes oldArray to have a capacity of newCapacity,
and returns a reference to the resized array.
static Object[] resize(Object[] oldArray, int newCapacity) {
Object[] newArray = new Object[newCapacity];
for (int i = 0; i < oldArray.length; i++)
newArray[i] = oldArray[i];
return newArray;
}
Reversing an array
The simplest way of reversing an array is to first copy the elements to another array in the
reverse order, and then copy the elements back to the original array. This out-of-place method is
rather inefficient, but it's simple and it works.
public static void reverse(Object[] array) {
Object[] tmpArray = new Object[array.length];
int i = 0;
// index into array
int j = tmpArray.length - 1;
// index into the reverse copy
while (i < array.length) {
tmpArray[j] = array[i];
i++;
j--;
}
// Now copy the elements in tmpArray back into the original array.
for (int i = 0; i < array.length; i++)
array[i] = tmpArray[i];
// NOTE: the following DOES NOT work! Why?
// array = tmparray;
}
int j = array.length - 1;
// backward index into right half
while (i < j) {
// Exchange array[i] with array[j]
Object tmp = array[i];
array[i] = array[j];
array[j] = tmp;
i++;
j--;
}
}
What would happen if this were a circular or cyclic array? See rotate an array left.
Back to Table of contents
array[0] = null;
// Now empty.
What would happen if this were a circular or cyclic array? See rotate an array right.
Back to Table of contents
There is a much more elegant solution that we'll see when we discuss circular or cyclic arrays
(wait till we study the Queue ADT).
Back to Table of contents
There is a much more elegant solution that we'll see when we discuss circular or cyclic arrays
(wait till we study the Queue ADT).
You can look at the example ArrayExamples.java class to see how these can be implemented.
Run the ArrayExamples.main() to see the output.