CS 1063  Weeks 13 and 14:  Arrays  

Objectives

Assignments


Array Basics

Consider the following interaction (input underlined):

How many days' temperatures? 7
Day 1's high temp: 45
Day 2's high temp: 44
Day 3's high temp: 39
Day 4's high temp: 48
Day 5's high temp: 37
Day 6's high temp: 46
Day 7's high temp: 53
Average temp = 44.6 
4 days were above average.

We need to be able to read each input value twice: once to compute the average (a cumulative sum) and again to count how many were above average.  We could read each value into a variable, but we don't know how many days are needed until the program runs, so we don't know how many variables to declare.

We need a way to declare many variables in one step and then be able to store and access their values.

Arrays

An array is an object that stores many values of the same type.  An array element is one value in an array.  An array index is an integer indicating a position in an array.  Like Strings, arrays use zero-based indexing, that is, array indexes start with 0.  The following displays the indexes and values in an array with 10 elements of type int.

index     0     1     2     3     4     5     6     7     8     9  
value    12  49  -2  26   5  17  -6  84  72   3

Array declaration

The syntax for declaring an array is:

type[ ] variable = new type[length];
Note that we need to specify a type (such as double or String), a variable name, and the length (the number of elements).  For example:

int[ ] numbers = new int[10];         // an array of 10 ints

would assign the following array to the variable numbers.

index     0     1     2     3     4     5     6     7     8     9  
value     0   0   0   0   0   0   0   0   0   0

Each element in an array is initialized to zero, or whatever is considered "equivalent" to zero for the data type (false for booleans and null for Strings).

Storing Values and Accessing Elements

The syntax for storing a value in an array element is:

variable[index] = expression;

For example:

numbers[0] = 27;
numbers[3] = -6;

would change the numbers array to:

index     0     1     2     3     4     5     6     7     8     9  
value    27   0   0  -6   0   0   0   0   0   0

The syntax for accessing an array element is:

variable[index]

where the index can be any expression that results in an int. For example:

System.out.println(numbers[0]);
if (numbers[3] > 0) {
    System.out.println(numbers[3] + " is positive");
} else {
    System.out.println(numbers[3] + " is not positive");
}
Activity: Write a program that declares a double or String array of length 4, prints the values, assigns a value to each element, and prints the values again.

Arrays and Loops

We can use a variable as the index in an array expression.  If we use a for loop to count from 0 to the highest index, then we can process each element of an array.  For example, the following code would sum the elements in the numbers array.

int sum = 0;
for (int i = 0; i < 10; i++) {
    sum += numbers[i];
}

We start at 0 because indexes start at 0.  We end just before 10 because 10 is the length of our numbers array, and the last index is one less than the length of the array.  [Arrays provide many opportunities for off-by-one errors because of the way indexes work.]

If we changed the numbers array to have a different number of elements, this code would no longer work.  Fortunately, Java provides a easy way to obtain the length of an array, by appending .length after the array variable, for example:

int sum = 0;
for (int i = 0; i < numbers.length; i++) {
    sum += numbers[i];
}
Activity: Write a program that inputs the length of a int array from the user and assigns 1 at index 0, assigns 2 at index 1, assigns 3 at index 2, and so on.  One loop should assign values to each element of the array.  A second loop should print the values of the array with spaces between the values.
Activity: Write a program that inputs the length of a double array from the user and a value for initializing the array.  One loop should assign the value to each element of the array.  A second loop should print the values of the array with spaces between the values.

The Temperature Program

Consider the interaction at the beginning of these notes.  Here is pseudocode that follows the sequence of interactions, using an array to manage the values that the user enters.  Note that we can't count how many elements are above the average until we have computed the average, and we can't compute the average until we have input all the elements.

  1. Input the number of days from the user.
  2. Declare an int array with the number of days as its length.
  3. For each index in the array:
    1. Input the temperature from the user.
    2. Store the temperature in the array at that index.
  4. Initialize a sum to zero.
  5. For each index in the array:
    1. Add the value at that index to the sum.
  6. Calculate and print the average.
  7. Initialize a counter to zero.
  8. For each index in the array:
    1. If the value at that index is greater than the average:
      1. Increment the counter.
  9. Print the counter.

We could have combined the first two loops into one loop, but it is cleaner to do them separately.  Here is the program that corresponds to the pseudocode.

import java.util.*;

public class Temperature {
    public static void main(String[] args) {
        Scanner console = new Scanner(System.in);

        // Input the number of days from the user.
        System.out.print("How many days' temperatures? ");
        int days = console.nextInt( );

        // Declare an array, maybe should check if days is positive
        int[ ] temps = new int[days];

        // Input and store the temperatures in the array
        for (int i = 0; i < temps.length; i++) {
            System.out.print("Day " + i + "'s high temp: ");
            temps[i] = console.nextInt( );
        }

        // Calculate and print the average
        int sum = 0;
        for (int i = 0; i < temps.length; i++) {
            sum += temps[i];
        }
        // need a cast to avoid integer division
        double average = (double) sum / temps.length;
        System.out.println("Average temp = " + average);

        // Count the number of values that were above average
        int count = 0;
        for (int i = 0; i < temps.length; i++) {
            if (temps[i] > average) {
                count++;
            }
        }
        System.out.println(count + " days were above average");
    }
}
Activity: Modify the Temperature program to also compute the variance and the standard deviation of the temperatures.  To compute the variance, you need to sum up:
(temps[i] - average)2
and then divide the sum by temps.length.  The standard deviation is the square root of the variance.

Additional Array Features

There are a variety of other features in Java for programming with arrays.  We just provide some examples and information here.  Look in the textbook for more information.

If you know in advance what the values are going to be in an array, you can specify those values when you declare the array, for example:

// how many days are in each month 
int[ ] daysInMonth = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
// what is the name for each day of the week
String[ ] weekDayNames = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};

There is a special kind of loop (the for-each loop) for processing each value in an array.  Here is an example for the Temperature program.  Of course, you should choose a better name than foo.

// Sum the temperatures
int sum = 0;
for (int foo : temps) {
    sum += foo;
}

The Arrays class provides several convenient static methods to operate on arrays.  To use them, you need to append Arrays. before the name of the method (and import the java.util package).

Method Name Description
copyOf(array, newSize)     returns a copy of the array with the new size
equals(array1, array2) returns true if every element is equal
fill(array, value) sets every element in the array to the value
sort(array) rearranges the values to go from smallest to largest
toString(array) returns a String representation of the array
Activity: Write a program to initialize an array to the first 10 prime numbers: 2, 3, 5, 7, 11, 13, 17, 19, 23, 29.  Use a for-each loop to sum the values in the array.  Print the array using the Arrays.toString method.

Arrays and Methods

Like other types, arrays can be passed as parameters to methods.  Suppose we want a method to sum the values in a double array.  This will require passing an array as a parameter and returning a double result.

public static double sumArray(double[ ] vals) {
    double sum = 0;
    for (int i = 0; i < vals.length; i++) {
        sum += vals[i];
    }
    return sum;
}

In this case, the name of the parameter is vals, but like all parameter variables, you can pretty much choose whatever name you like.  Note that the [ ] is part of the parameter type.  double[ ] vals should be read as "a double array named vals".

Activity: Write a method that returns the average value in an int array.  Include and use this method in the Temperature program.

Arrays can also be returned by a method.  Suppose we want a method that converts a String into a char array.  This will require passing a String as a parameter and returning a char array.

public static char[ ] toCharArray(String token) {
    char[ ] elts = new char[token.length( )];
    for (int i = 0; i < token.length( ); i++) {
        elts[i] = token.charAt(i);
    }
    return elts;
}

Note that the [ ] is part of the return type and that token is the name of the String parameter.  Inside the method, we need to make sure the array is the same length as the String; the code gets this value using the length method.  Note that the length method is used twice: once to specify the length of the new array, and another in the loop test.

Activity: Write a method that returns a new int array with a given size and with each element initialized to a given value.  Note that this method needs two parameters.  Include and use this method in the Temperature program.

References

When you pass an array as a parameter, Java does not create a new copy of the array.  That would be very inefficient for large arrays.  Instead, the parameter variable stores a reference (or pointer) to the same array.  What this means is that if the method modifies the array, then those modifications will be seen by the code calling the method.  Consider the following program.

public class ReferenceExample {
    public static void main(String[ ] args) {
        double value1 = 3.14;
        System.out.println("The double before the method call: " + value1);
        setToZero(value1);
        System.out.println("The double after the method call:  " + value1);

        double[ ] array1 = {1.0, 0.5, 0.25, 0.125, 0.0625};
        System.out.println("The array before the method call: " + 
                           Arrays.toString(array1));
        set2Zero(array1, 3);
        System.out.println("The array after the method call:  " + 
                           Arrays.toString(array1));
    }

    // This method fails to set value1 to zero.
    public static void setToZero(double value2) {
        value2 = 0;
    }

    // This method succeeds in changing array1.
    public static void set2Zero(double[ ] array2, int index) {
        array2[index] = 0;
    }
}
The output of this program is:
The double before the method call: 3.14
The double after the method call:  3.14
The array before the method call: [1.0, 0.5, 0.25, 0.125, 0.0625]
The array after the method call:  [1.0, 0.5, 0.25, 0.0, 0.0625]

Note that the setToZero method did not change value1 to zero.  When a parameter is a primitive type, the value is copied.  This illustrates the situation before and after the value2 = 0; statement.  The values for value1 and value2 are stored in two different locations.

Before After
 value1   3.14             value1   3.14 
 value2   3.14             value2   0.0  

However, the set2Zero method did change an element of array1 to zero.  When a parameter is an array, the reference is copied, not the array.  This illustrates the situation before and after the array2[index] = 0; statement.  Both array1 and array2 refer to the same array.

Before After
array1             array1  
 
1.0    0.5    0.25   0.125  0.0625
   
1.0    0.5    0.25   0.0    0.0625
array2             array2  
Activity: Write a method that doubles each element in a double array.  This method should have an array parameter and a void return type.  Write a program to test the method, printing the array before and after the method call.

Array Traversal Algorithms

Array traversal is usually carried out with the pattern:
for (int index = 0; index < array.length; index++) {
    // do stuff with array[index]
}

The for loop goes through all the indexes of the array.  The body of the loop can store or access values in each element in turn.  There are some uses and variations of this pattern that are useful for every programmer to know.

Printing an Array

Suppose we want to print the following array:

int[ ] list = {2, -4, 6, -8, 1, -3, 5};

on one line with commas between the numbers.

2, -4, 6, -8, 1, -3, 5

Note that this will be an example of a fencepost loop because there is not as many commas as numbers.  If the body of loop prints a number and a comma, then there is an extra number to be printed outside the loop.  The choices are as follows:

Here are all three solutions for an array named list.

// print one before the loop
System.out.print(list[0]);
for (int i = 1; i < list.length; i++) {
    System.out.print(", " + list[i]);
}
System.out.println( );

// print one after the loop
for (int i = 0; i < list.length - 1; i++) {
    System.out.print(list[i] + ", ");
}
System.out.println(list[list.length - 1]);

// use an if in the loop
for (int i = 0; i < list.length; i++) {
    if (i > 0) {
        System.out.print(", ");
    }
    System.out.print(list[i]);
}
System.out.println( );
Activity: Write and test a method to print an array like:
[2 -4 6 -8 1 -3 5]
That is, there should be spaces between the numbers and brackets at the ends.
Activity: Write a program to print a longer array like:
int[ ] list = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29,
               31, 37, 41, 43, 47, 53, 59, 61, 67,
               71, 73, 79, 83, 89, 97};
with no more than 10 numbers per line.

Searching and Replacing

Once you have an array with lots of values, one common activity is to search the array for a value, that is, to find out whether a value appears in an array.  Sometimes, this is combined with replacing the value with a different value.

To write a method to search an array for a value, two parameters are needed: the array and the value to search for.  What should be returned?  One useful piece of information that could be returned is the index of the value.  What should be returned if the value is not in the array?  The Java convention for this case is to return -1.  Note that if the value is in the array, the index can be returned without looking at the rest of the array.  If the method looks through the whole array without finding the element, then the method should return -1.  Here is pseudocode for this process.

  1. For each index in the array:
    1. If the value at that index is equal to the search value:
      1. Return the index.
  2. If the loop finishes without a return:
    1. Return -1.

Here is a method that implements this pseudocode.

// This method returns the index of the value or -1 if not found.
public static int indexOf(int[ ] array, int value) {
    for (int i = 0; i < array.length; i++) {
        if (value == array[i]) {
            return i;
        }
    }
    // To get to this point, the value must not be in the array.
    return -1;
}
Activity: Write a program using the indexOf method to search an int array.  The user should be prompted to enter the values for the array.  Then the user should be prompted to enter a value to search for.
Activity: Write a program to input words from a user into a String array.  Allow the user to enter another word, searching the array for that word.  Recall that you need to use the equals method to test equality of Strings.
Activity: Write and test a method that returns the last index of a value in an array, returning -1 if the value is not found.

A method for replacing a value in an array can be based on the same code.  All that needs to be added is another parameter and an assignment statement before the return statement.  Returning an index is still useful.  For example, if -1 is returned, that would indicate that no value was replaced.

// This method replaces one value with another.
// It returns the index that was replaced or -1 if not found.
public static int replace(int[ ] array, int value, int newValue) {
    for (int i = 0; i < array.length; i++) {
        if (value == array[i]) {
            array[i] = newValue;
            return i;
        }
    }
    // To get to this point, the value must not be in the array.
    return -1;
}
Activity: Modify the int searching program or the String searching program so the user is prompted for a replacement value.

Testing for Equality

The Arrays.equals static method can test whether two arrays are equal, but it is useful to consider how to code it.  For two arrays to be equal, they need to have the same number of elements, and each element in one array must be equal to the corresponding element in the other array.  Note that if any test fails, then the arrays are not equal, and the code can return false without checking all of the tests.  Here is pseudocode for a method for testing equality of two arrays.

  1. If two arrays have different lengths:
    1. Return false.
  2. For each index:
    1. If the corresponding elements in the two arrays are not equal:
      1. Return false.
  3. Return true.
Activity: Write and test a method for checking equality of two int arrays.
Activity: Write and test a method for checking approximate equality of two double arrays.  The method should have an additional parameter that indicates how close two doubles need to be to be considered approximately equal.

Reversing an Array

To reverse the values of an array, the values at two indexes need to be repeatedly swapped.  Consider swapping the value of two variables first.

Suppose we want to swap the values of list[0] and list[1].  The following code:

list[0] = list[1];
list[1] = list[0];
does not work because the first assignment overwrites the old value of list[0].  One way of solving this problem is to save the values of list[0] and list[1] in other variables before assigning values to the array .  Assuming list is an int array:
int temp0 = list[0];
int temp1 = list[1];
list[0] = temp1;
list[1] = temp0;
However, the traditional code uses a clever sequence with one fewer assignment.
int temp = list[0];
list[0] = list[1];
list[1] = temp;
Once the old value of list[0] is stored in temp, it is safe to assign a value to list[0].  With the old value for list[1] in the right place, temp can be assigned to list[1].

Back to the reversal problem, consider the following array.

  0     1     2     3     4  
 2  -3  5  -7  11

Swapping the first and last element should result in:

  0     1     2     3     4  
 11  -3  5  -7  2

Next, swapping the second and next to last element should result in:

  0     1     2     3     4  
 11  -7  5  -3  2

Note at this point, the array has been reversed with only a couple swaps.  In general, the number the swaps will about half of the length of the array; the values on the left half will be swapped with the values on the right half.  Here is pseudocode for this process.

  1. For each index in the left half of the array:
    1. Assign the index to be swapped to right.
    2. Assign the value at index left to temp.
    3. Assign the value at index right to the element at index left.
    4. Assign the temp to the element at index right.
Activity: Write and test a method for reversing a double array.

String Traveral

We have already seen some examples of String traversal (see Section 4.3 on Text Processing).  It is similar to array traversal with a couple of exceptions.  One is that the charAt method must be used to access a char at a particular index.  The other is that you can't assign a value to a particular index in the String because Java doesn't allow Strings to be changed once they have been created.  Here is the pattern for String traversal.
for (int index = 0; index < string.length( ); index++) {
    // do stuff with string.charAt(index)
}
Activity: Write an indexOf method for Strings that returns the index of a given char.
Challenge: Write and test a method:
public static String replace(String oldString, int index, char newChar)
that returns a new String by replacing the char at the given index with newChar.

Reading a File into an Array

If you have done some of the activities, you have probably found out that it is tedious to enter values for arrays by hand.  It would be more convenient for the program to read the values from a file.  More information about file processing can be found in Chapter 6 of the textbook.

A file is a collection of information that is stored on a computer and assigned a name.  For example, hamlet.txt might be the name of a file that contains the text of HamletHello.java might be the name of the file with your "Hello World" program.  We will be working with text files, which include files with the .txt file extension as well as .java and .html files.

Using a file in a program usually involves the following steps:

  1. Open a file for reading (or writing).
  2. Read data from the file (or write data to the file).
  3. Close the file.

For example, when you use text processing software, you tell the software to open and read the file.  After you make changes, you tell the software to write the file.  When you exit the software, the software will close the file if it has not already done so (if the file is not closed, your changes might not be saved).

In Java, we will use File objects for representing files, and Scanner objects for opening, reading, and closing files.  Suppose we have the file temperature.txt that contains the following information needed by the Temperature program.

7
45 44 39 48 37 46 53

Assuming that this file is in the same folder as the Temperature program, a File object can be constructed by this statement.

File infile = new File("temperature.txt");

To read from this file, a Scanner object can be constructed by this statement.

Scanner input = new Scanner(infile);

For most purposes, we don't need the File object except for constructing the Scanner object, so the above two statements can be coded into one statement.

Scanner input = new Scanner(new File("temperature.txt"));

We can use a Scanner object for a file just like the Scanner object for the keyboard except that we don't need to print prompts.  Here is the Temperature program that reads the information from the file.

import java.util.*;
import java.io.*;

public class Temperature {
    public static void main(String[] args) throws FileNotFoundException {
        Scanner input = new Scanner(new File("temperature.txt"));

        // Input the number of days from the file.
        int days = input.nextInt( );

        // Declare an array, maybe should check if days is positive
        int[ ] temps = new int[days];

        // Input and store the temperatures in the array
        for (int i = 0; i < temps.length; i++) {
            temps[i] = input.nextInt( );
        }

        // Close the file.  This is not really needed because
        // Java will close the file once the program ends.
        input.close( );

        // Calculate and print the average
        int sum = 0;
        for (int i = 0; i < temps.length; i++) {
            sum += temps[i];
        }
        // need a cast to avoid integer division
        double average = (double) sum / temps.length;
        System.out.println("Average temp = " + average);

        // Count the number of values that were above average
        int count = 0;
        for (int i = 0; i < temps.length; i++) {
            if (temps[i] > average) {
                count++;
            }
        }
        System.out.println(count + " days were above average");
    }
}

Note that an additional import statement is needed; this is because the File class is part of the java.io package.  Also note that the method header needs a throws clause; this is because Java wants you to pay attention to the possibility of a FileNotFoundException.  It would be better to use a try/catch statement to handle this issue, but this is outside the scope of this course.

Activity: Write a program that reads the 32230 tokens in hamlet.txt into a String array.  Prompt the user for a word to search.  Count and print out the number of times that word appears.  Two words you might try are "haue" (over 100 times) and "againe" (over 10 times).

Multidimensional Arrays

In many applications, data has multiple dimensions.  Spreadsheets, images. and video are three common examples.  Here, we will use the example of iris.txt.  If you want to know more about what these numbers mean, you can read this.

The first line of iris.txt indicates that the data has 150 rows (or lines) and 5 columns.  A multidimensional array can be declared so that we can access an element by row and column.  Assuming that these numbers are stored in variables named rows and columns, we can declare a two-dimensional array by:

double[ ][ ] iris = new double[rows][columns];

Note that there are two pairs of brackets in the declaration.  Also note that two lengths are specified.  For a three-dimensional array, we would need three pairs of brackets, and so on for higher dimensions.

The element in the tenth row and the third column would be assigned and accessed by:

iris[9][2] = 1.5;
System.out.println("iris[9][2] is " + iris[9][2]);

Zero-based indexing is used for both dimensions.  Of course, it would be very tedious to write 750 lines of code to assign a value to each element of the iris array.  Typically, nested loops are used.

  1. For each row index:
    1. For each column index:
      1. Do stuff with the element in that row and column

If the Scanner object for reading from iris.txt is stored in the variable input, then the following code would fill in the array (assuming that the first two numbers have been consumed).

for (int row = 0; row < rows; row++) {
    for (int col = 0; col < columns; col++) {
        iris[row][col] = input.nextDouble( );
    }
}

A similar double loop can be coded to do other processing of the data.  Suppose you want to compute the average value of each of the columns.  Five variables could be declared for the five columns, but it is cleaner to declare an array with five elements for the five averages.  In the following code, the additional array is used first to hold the sums of each column, then a division leads to the average.

// This assignment also initializes the new array with zeroes.
double[ ] average = new double[columns];
for (int row = 0; row < rows; row++) {
    for (int col = 0; col < columns; col++) {
        average[col] += iris[row][col];
    }
}
for (int col = 0; col < columns; col++) {
    average[col] /= rows;
}
System.out.println("The averages are " + Arrays.toString(average));

When you have a lot of data, it is often useful to see what it looks like.  As a final example, consider how to count how many times each value occurs in iris.txt.  By inspection, each value only has at most one decimal place, so if we multiply by 10 and round, then different values will correspond to different integers.  All the values in iris.txt are positive, and the largest value is 7.9, so the integers will range from 1 to 79.  This means an integer array of length 80 can be used to count each value.

For example, suppose a value is 2.3.  Multiplying by 10 results in 23.  This means we should add 1 to the integer array at index 23.  Here is the code that counts all 750 values.

// This assignment also initializes the new array with zeroes.
int[ ] counts = new int[80];
for (int row = 0; row < rows; row++) {
    for (int col = 0; col < columns; col++) {
        int timesTen = (int) Math.round(10 * iris[row][col]);
        counts[timesTen]++;
    }
}
System.out.println("The counts are\n" + Arrays.toString(counts));

Using a DrawingPanel object and additional code, a histogram of the values can be drawn.  A program that includes all the examples of processing iris.txt and draws a histogram can be found here.

Activity: Read iris.txt into a 2-dimensional array.  Determine the averages of the first four columns of iris.txt depending on the value of the last column.  That is, what are the averages when the last column is 1, what are the averages when the last column is 2, and what are the averages when the last column is 3?  Create an array with 3 rows and 4 columns to store the sums and the averages.
Activity: Read bupa.txt into a 2-dimensional array.  Determine the averages of the columns of bupa.txt depending on the value of the last column.  More information about this data can be found here.
Challenge: Read bupa.txt into a 2-dimensional array.  Using DrawingPanel objects, draw histograms for each column.  Also, draw two additional histograms for each of the first six columns: one for when the last column is 1 and another for when the last column is 2.