Array operations

In this lab, we will work with arrays.

All about Arrays

An array is a data structure. It is an ordered collection of objects or primitive data elements. You access a specific element of an array by stating the collection name followed by the element's index number within that collection.

Creating an Array

In Java, arrays are objects. To create an array you create an object. The syntax for creating an array object is slightly different from that used for other objects. To create an array of 5 int's called tom, you would type:
    int[] tom = new int[5];
On the left-hand side of the assignment operator, int[] is the data type array of int and tom is the name of the array object. On the right-hand side of the assignment operator, the operator new is being used to create an array object containing a collection of five integer variables.

Each element in the array has an index number associated with it. The index number of the first element is 0. Each element after that is numbered sequentially. This means that the index number of the last element of the array is one less than the size of the array. You can almost think of each element in an array as a variable. Because you access the "variables" of an array via an index number, there are some pretty powerful tricks you can do with arrays.

Arrays and Loops

If you wanted to set 5 int variables equal to 0, how would you do it? Perhaps like this:

  june = 0;
  tom = 0;
  may = 0;
  scarlet = 0;
  apple = 0;

This is a tedious way of doing things, and you run the risk of making a mistake. To do the same task using an array, you could do the following:

  int[] stuff = new int[5];
  for (int i = 0; i < 5; i++) {
     stuff[i] = 0;

This is a much more efficient way of doing things. Notice that each time the loop variable is incremented, a different element of the array is set equal to zero. Using a for-loop in this manner, you can easily perform a complex task on all of the (or specific) elements of an array. The following Stack N' Heap demonstrates how to create and initialize an array of integers storing the numbers from one to five. For simplicity, we will show the elements of the array as existing on the stack; but, because they are contained in an object, they are actually stored on the heap.

Stack N' Heap What's This? The Stack N' Heap simulates a trace program. It shows the code being executed, the contents of the stack, and the contents of the heap. To move through the code, use the >> button. This will take you one step through the code. To go back one step, use the << button.

Primitive variables are shown in the stack with their names on the left side and values on the right. Reference variables do not have any value on the right, but instead have a yellow line drawn to show which object in the heap it points to. Reference variables with a NULL value don't show anything on the right.
int[] myArray = new int[5];
for (int i = 0; i < myArray.length; i ++) {
myArray[i] = i + 1;

As you see, loops and arrays are often used together. By using a loop variable as an index into an array, you can perform tasks on a large number of variables which just a few lines of code.

Downloading the project framework

Download the Java program and store it in your csci/201 directory.

Examining the present code

Operating on Arrays

Compile and run the Array project. Notice that the first output line in the Interactions panel.

Operator getSum returns 2522

If you look through, you'll find the following method.

  // Write a method that returns the sum of the array elements
  private static int getSum(int[] v) {
    int sum = 0 ;
    for (int x : v) {
      sum = sum + x ;
    return sum ;

The main method of, through the magic of the Java reflect package, calls getSum with an array v filled with sound sample values taken from the audio file named in the variable fileBraes. (If haven't changed anything, this will correspond to the first four bars of Braes of Tullimet.)

After getSum, there are four other very incomplete methods.

  1. getMostPositive
  2. getMostNegative
  3. getAverage
  4. getExtremes

Each of these is preceded by a comment that explains what the method should do. Your job is make these methods complete their assigned tasks. Each will require five to seven lines of Java for this.

Modifying Arrays

You should also see a sound explorer window labeled setA440. Again, if you look through the code you will find a method called setA440.

  private static void setA440(int[] v) {
    for (int i=0; i<v.length; ++i) {
      double realSlotTime = (i + 0.5)/samplingRate ;
      double angleRadians = 2 * Math.PI * frequency * realSlotTime  ;
      double sineValue    = Math.sin(angleRadians) ;
      int    sampleValue  = (int)Math.round(sineValue*amplitude) ;
      v[i] = sampleValue ;

Like getSum, this method is passed an array v. However, setA440 doesn't return a value. Instead it modifies v to hold a new set of values.

Before preceding to your next assignment, you might want to play with the setA440. Right now it is demonstrating the dangers of aliasing. If you press the Zoom In windows, you'll see the wave.

Now take a look before setA440. This time, you'll see five incomplete methods for modifying the array of sample values.

  1. divideBy3
  2. repeatFirstHalf
  3. doubleTime
  4. quadrupleTime
  5. reverse

Each of these methods is proceded by a comment describing what it is supposed to do. Make them do it!

When you are done, show your lab instructor your completed methods.