CSCI 201 -- 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 specific element's index number in 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 ten integer variables.

Each element in the array has a index number associated with it. The index number of the first element is 0. Each element after that is numbered sequentially, up to the last element. 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.

Primative 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, a ZIP file containing a NetBeans project named ArrayOp and unZIP this project into your csci/201 directory. Try to make your Projects panel look something like the following picture before continuing.
ArrayOp Projects

Operating on Arrays

Examing the present code

Compile and run the application class ArrayOp. Now take a look at the internal class MyClass1 of ArrayOp.

private static class MyClass1 implements SpecArrayOp {
    public int TestFunc(int[] V) {
        int N = V.length ;
        return V[N/2] ;

As you see, MyClass1 consists of just one method, TestFunc, and that TestFunc has a single parameter, V of type int[]. TestFunc's purpose is to return an integer value. Right now, it returns the middle element of the array that it receives as input.

The ArrayOp class works as follows. Its main method calls the TestArrayOperation method of TestArrayOp with a single variable, a class which implements the SpecArrayOp interface. TestArrayOperation creates a array, passes it to the TestFunc method of the class it is testing and then prints the result.

Your Assignment

You are to modify the main method of ArrayOp so that it tests six different classes. These six classes have a TestFunc method which implements the six array operations shown below. Use the solution set generator on the right to check your answers.

Class Name TestFunc Return Value
MyClass1 Adds all elements of the array and returns the total
MyClass2 Returns the largest element of the array
MyClass3 Returns the smallest element of the array
MyClass4 Adds all the even elements of the array and returns the total
MyClass5 Returns an integer average of all elements in the array
MyClass6 Adds the smallest and largest elements of the array and returns their sum

When you are done, show your lab instructor the output of your program.