Weeks 13 and 14: Arrays
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.Challenge: Is it possible to solve the above problem using only the techniques from the previous notes without using arrays? How? 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.
Array Terminology The syntax for declaring an array is: type[ ] variable; This just declares a variable that can hold an array, but does not create the array itself.For example, to declare a variable, numbers that can hold an array of integers, we would use: int[] numbers; Since arrays are objects, we create arrays using new. When creating an array, you specify the number of elements in the array as follows: variable = new type[length]; For example, to create an array of 10 integers: numbers = new int[10]; We can combine the two operations of declaring and creating an array: type[ ] variable = new type[length]; Our example would become: int[ ] numbers = new int[10]; // an array of 10 ints This would assign the following array to the variable numbers.
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). 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:
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"); } When you declare an array, each element of the array is set to a default initial value. For integers, the default value is 0. What do you think the default value is for doubles? Activity: Array default initial values We can use an integer variable as the index of an array. 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]; } Notice that for arrays we do not use parentheses after the length. This is different from the way the length is done for strings. Activity: Initialize and Show Integer Array Activity: Initialize and Show Double Array
Activity: variance and standard deviation 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: and then divide the sum by temps.length. The standard deviation is the square root of the variance.Simple Array Loops 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).
Activity: an array of primes 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. 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: Array Average Method 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: Create int Array Method Simple Array Methods 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. import java.util.Arrays; 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.
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.
Activity: Array Variables and Array Objects Answer the following questions about the following program: import java.util.*; public class ArrayExample { public static void main(String[] args) { int[] array1 = {1,2,3}; int[] array2 = {3,4}; int[] array3 = array1; int[] array4 = array2; System.out.println(Arrays.toString(array1)+Arrays.toString(array2)); array3[0] = 5; array4[1] = 7; System.out.println(Arrays.toString(array1)+Arrays.toString(array2)); array1 = array4; System.out.println(Arrays.toString(array1)+Arrays.toString(array2)); } }
Activity: Array Variables and Array Objects Activity: Double Array Elements Methods
Double Array Elements Array traversal is usually carried out with the pattern: for (int index = 0; index < array.length; index++) { int[ ] list = {2, -4, 6, -8, 1, -3, 5}; on one line with commas between the numbers.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:
Fencepost Survey Activity: Print Array in Brackets Method Activity: Print Long Array in Brackets Method 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}; in brackets with no more than 10 numbers per line. 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.
// 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: Prompt for Array and Search Activity: Input Words into Array Activity: Search Array Methods Activity: Search and Replace Search and Replace 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.
Activity: Equal int Arrays Method Activity: Approximately Equal Arrays Method Array Equality 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]. Here is a trace of the 3-instruction swap:
Activity: Reverse Array Method Array and String Reversal 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++) { Activity: indexOf Method Challenge: String Replace Character 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.Array Equality, Array Reversal, and String Replace 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 Hamlet. Hello.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:
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. 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: Temperatures from File Copy the above program into Dr. Java. Create a file called temperature.txt in the same directory. The first line should contain the number of temperatures in the file and the next line should contain integer temperatures separated by blanks.Run the program. Now try the following.
Activity: Searching Hamlet 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.
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: IRISi Activity: BUPA Challenge: BUPA Histogram Page 2 Weeks 15 and 16: Final Review
Final Review Problems 1 For Java programming, review the labs, the projects, and the activities from the lecture notes. Some of these will likely be part of the exam. For general knowledge, review Chapters 1-5, Sections 7.1-7.4, Supplement 3G, your quizzes, and the lecture notes. Understanding the chapter summaries and self-check problems is a good way to start reviewing the book material. Below is a table of particular items to pay attention to:
Note: All the Self-Check Problems are good to do. The above selects a subset of them as examples of what to study. Terminology
Hover mouse for more information
Notation
Hover mouse for more information
Keywords
Hover mouse for more information
Final Review Problems 2 Final Review Problems 3 Activity: Draw Red and Blue Circles Hint: Draw blue circles centered at (0,0) and red circles centered at (300,0). Activity: Three Doubles Mehtods
Activity: Determine Data Type Activity: StringAgain Some variations on this task include:
Activty: Divisible by 2 Activty: Double Array Methods [See the Temperature2 program on p. 450.] Write and test methods for one or more of the following tasks.
Activity: Self Check 29 This is based on Self-Check Problem 29 in Chapter 7. Write a method that computes the average String length of an array of Strings. For example, the array {"Write", "a", "method", "in", "Java"} has an average length of 3.6. Write a Java program that tests your method. Page 3 (change section)
|