CS 051 Fall 2011

Lecture 23

Array Review and Application

We can get the length of an array using the array's public instance variable length

We talked in class about when we might want to use this information (that is, when we might not
know the length already). Consider a case where you want to write a method that takes an array
of doubles, and returns the largest value in the array.

// return largest value in elt array. Require the array parameter to have
// length at least 1 (so there is an element there)

public double largest(double[] a)
{
     double largest = a[0];
     for (int index=1; index < a.length; index++)
     {
         if (a[index] > largest)
             largest = elt[index];
         }
     return largest;
}

In general, the above approach only works if the array is full. That is, if every slot has a
legitimate value. We can handle the case where this is not true by requiring the user to
supply the useful length of the array.

public double largest(double[] a, int length)
{
     double largest = a[0];
     for (int index=1; index < length; index++)
     {
         if (a[index] > largest)
             largest = elt[index];
         }
     return largest;
}

Array Scribbler

We had looked at a recursive version of the Scribbler program earlier in the semester. Now
we can create a version of the program that uses arrays.

See class demo ArrayScribbler

Because there will be a single class representing all scribbles, empty and non-empty, we do
not need an interface. The new Scribble class will contain an instance variable, scribbleLine,
holding an array of lines composing the scribble, and instance variable, numScribbles, which
represents the number of lines currently held in the scribble.

An important difference between this version and the recursive version is that we must
determine the maximum number, MAX_SCRIBBLES, of lines that a scribble can contain. Once that
size has been exceeded, new line segments can no longer be added.

The Scribbler class has two methods that the recursive version did not: addLine and moreRoom.
The first is necessary because we no longer want to create a new scribble every time a new line
segment is added. The second is necessary so that the program can determine whether or not
there is room to add a new line.

Notice that rather than putting the array as an instance variable of the Scribbler class,
we made it an instance variable of a separate Scribble class and provided associated methods
for accessing it. This is the preferred way of writing programs that use arrays as we want to
think of dealing not with the array itself, but instead with what the array represents. By
including method names that make sense for the intended use of the array (e.g., contains for
a scribble), we make it easier to program the problem. Among other things, you can see that the
Scribbler class in this case looks very similar to the Scribbler class for the recursive case.

New In Java 5

Java 5 also supports a new shorter way of going through all elements of an array with a for loop.
We can write:

Line[] scribbleLine = new Line[MaxVals];
...
for (int i = 0; i < MaxVals; i++)]
     scribbleLine[i].move(xOffset,yOffset);
}

or equivalently

for (Line line:scribbleLine) {
     line.move(xOffset,yOffset);
}

Note that his only works if the array is full, otherwise you will get a NullPointerException
when you send the move message to unfilled slots. This new for construct "iterates" through all
of the elements in the array from 0 to MaxVals - 1.

Java 5 now includes the class ArrayList that holds an "expandable array". That is, it behaves
like an array (though with object syntax), but keeps growing as necessary.

See class demos Drawing Program and Array List Scribbler

Notice that in each case we do not need to keep track of the number of elements in the ArrayList
as the data structure keeps track of it itself. Notice also in the first program that we can just
send the delete message to an array list with the index and it will automatically shift
everything over to fill the gap. While we only add at the end of the array here, it will also
allow us to add something in th middle and shove all of the old elements over to make space.