Objectives
Assignments
Array BasicsConsider the following interaction (input in red): Show
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? ArraysAn 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 Array declaration and creationThe 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. that can hold an array of integers, we would use: int[] numbers; Since arrays are objects, we create arrays using
new. 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 ( falsefor booleans and nullfor Strings). Storing Values and Accessing ElementsThe syntax for storing a value in an array element is: variable[index] = expression; For example: numbers[0] = 27; numbers[3] = -6; would change the numbersarray to:
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"); } When you declare an array, each element of the array is set to a default initial value. Activity: Array default initial values array of length 4, prints the values, assigns a value to each element, and prints the values again. elements. Arrays and LoopsWe 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 numbersarray. 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. If we changed the numbersarray 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 .lengthafter 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. Activity: Initialize and Show Integer Array 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: Initialize and Show Double Array 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 ProgramConsider 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.
We could have combined the first two loops into one loop, but it is cleaner to do them separately. 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 and create 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: variance and standard deviation and then divide the sum by temps.length. The standard deviation is the square root of the variance. Simple Array Loops Additional Array FeaturesThere 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.utilpackage).
Activity: an array of primes method. Arrays and MethodsLike other types, arrays can be passed as parameters to methods. Suppose we want a method to sum the values in a doublearray. This will require passing an array as a parameter and returning a doubleresult. 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[ ] valsshould be read as "a double array named vals". Activity: Array Average Method 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 Stringinto a chararray. This will require passing a Stringas a parameter and returning a chararray. 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 tokenis the name of the Stringparameter. Inside the method, we need to make sure the array is the same length as the String; the code gets this value using the lengthmethod. 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 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. Simple Array Methods ReferencesWhen 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 setToZeromethod did not change value1to 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 value1and value2are stored in two different locations.
However, the set2Zeromethod did change an element of array1to 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 array1and array2refer to the same array.
Activity: Array Variables and Array Objects 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 AlgorithmsArray traversal is usually carried out with the pattern: for (int index = 0; index < array.length;
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 ArraySuppose we want to print the following array: 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:
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( ); Fencepost Survey Activity: Print Array in Brackets Method That is, there should be spaces between the numbers and brackets at the ends. 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. Searching and ReplacingOnce 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.
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: Prompt for Array and Search method to search an intarray. 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: Input Words into Array array. Allow the user to enter another word, searching the array for that word. Recall that you need to use the equalsmethod to test equality of Strings. Activity: Search Array Methods 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: Search and Replace searching program so the user is prompted for a replacement value. Search and Replace Testing for EqualityThe Arrays.equalsstatic 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 arrays. Activity: Approximately Equal Arrays Method arrays. The method should have an additional parameter that indicates how close two s need to be to be considered approximately equal. Array Equality Reversing an ArrayTo 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 listis an intarray: 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, tempcan be assigned to list[1].
Back to the reversal problem, consider the following array. Swapping the first and last element should result in: Next, swapping the second and next to last element should result in: 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.
Activity: Reverse Array Method array. Array and String Reversal String TraversalWe have already seen some examples of Stringtraversal (see Section 4.3 on Text Processing). It is similar to array traversal with a couple of exceptions. One is that the charAtmethod must be used to access a charat a particular index. The other is that you can't assign a value to a particular index in the Stringbecause Java doesn't allow Strings to be changed once they have been created. Here is the pattern for Stringtraversal. for (int index = 0; index < string.length( ); index++) { Activity: indexOf Method method for Strings that returns the index of a given char. Challenge: String Replace Character Method public static String replace(String oldString, int index, char newChar) that returns a new Stringby replacing the charat the given indexwith newChar. Array Equality, Array Reversal, and String Replace Optional Topic: Reading a File into an ArrayIf 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.txtmight be the name of a file that contains the text of Hamlet. Hello.javamight be the name of the file with your "Hello World" program. We will be working with text files, which include files with the file extension as well as .javaand .htmlfiles. Using a file in a program usually involves the following steps:
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.txtthat 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
Activity:
Searching Hamlet into a Stringarray. 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). Optional Topic: Multidimensional ArraysIn 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.txtindicates 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 rowsand 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 irisarray. Typically, nested loops are used.
If the Scanner object for reading from iris.txtis 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.txtare 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.txtand draws a histogram can be found here. Activity: IRISi into a 2-dimensional array. Determine the averages of the first four columns of iris.txtdepending 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: BUPA into a 2-dimensional array. Determine the averages of the columns of bupa.txtdepending on the value of the last column. More information about this data can be found here. Challenge: BUPA
Histogram 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. What property of the ListBox control reports the number of items stored in the ListBox if the ListBox is empty the property equals 0?Items. Clear(); ListBox controls have a(n) property that reports the number of items stored in the ListBox. If the ListBox is empty, the property equals 0.
Which type of data file access allows access to any piece of data in the file without reading the data that comes before it?Random access file in C enables us to read or write any data in our disk file without reading or writing every piece of data before it. In a random-access file, we may quickly search for data, edit it or even remove it.
What is the term used to describe a file that data is read from?What is the term used to describe a file that data is read from? Input file.
|