Consider how you might write a program to read in some test scores and average them.
- Our first attempt might be to use a variable to store each test score
The problem with this approach is that we must know ahead of time how many test scores there will be, and changing this number requires editing and recompiling the program.- The second approach is to read the scores using a loop, accumulating the sum of the scores and counting the number of the scores as we go.
This approach is promising, but if the requirements changed and we had to echo all the scores before displaying the average, we wouldn't be able to do this. Once the scores have been entered, we accumulate their value, but we cannot retrieve the individual scores.
The most natural way to solve the scores problem is to use arrays.
- This solution declares a fixed-size array inside of main, which stores all the scores as they are entered.
This allows use flexibility in how to use the scores, as they are all stored to be retrieved any way we wish. One limitation is that we must know the maximum number of scores ahead of time. (See below for the syntax for array declaration and usage.)- A second version uses a constant for the array size, and uses loops to read and process the values. Note how easy it is to change it to handle a different number of scores.
- We can pass the array to a function as a parameter, accessing the array elements from within the method. Note that the method parameter works as a synonym to the original array. Unlike what happens usually when we send parameters to methods, when we send an array as a parameter to a method, the changes in the array are automatically reflected back to the calling part of the program. In general, we can put an object into an array if we want to pass it as a parameter to a method and have the change reflected back.
Rather than having to store multiple values of the same type in different variables, we can store them in an array. This is appropriate when we have a group of values of the same type, such as a list of numbers, or a group of characters representing a playing board for a game.
To declare an array, we use:
type[ ] arrayName;
where type can be any type (e.g. char, int, float), arrayName is an identifier you make up, and size indicates how many items will be stored in the array. We then must initialize the array using:
arrayName = new type[ size];
For example
int[ ] scores;
scores = new int[ 10] ;
declares and initializes an array called theScores that can hold 10 integers. In the array shown above the first element is the 0th element, and the last is the 9th element. This element number is known as the index (a.k.a. the subscript) , which starts at 0 and goes to n-1 for an array of n elements. Note that the default value of 10 is stored in the array at this point.
(You may want to browse the directory of arrays examples given below.)
Counting Characters
We can write a program to count the number of each character on an inputline.
Similarly we can have a program count the total number of each character found in an input file (e.g. Macbeth.txt). See a solution using Scanner, and another solution using BufferedReader for input.Searching and Sorting
This program uses binary searching to find a particular integer key within an array of ordered numbers, and bubble sort to put some integers in an array into ascending order. See related class handouts for binary search (MS Word) and bubble sort (MS Word). See here for some examples of other algorithms used in sorting (You'll study some of these in subsequent CS courses).
Coin Guess Program
This program pits user against computer, guessing even or odd. It uses an array to keep track of a user's last 3 guesses, using this information to forecast what the user's next guess is most likely to be.
Command line Arguments
We can retrieve values from the command line for use in our program. To test this, go to the command line in the directory in which the code resides. First compile the program (stored in the file named CommandLineArgs.java) by typing in:
javac CommandLineArgs.java
and then run the program from the command line by typing in:
java CommandLineArgsDynamic Arrays
We can write our own version of a dynamic array (with a driver and the array class), that automatically allocates more space as needed.
Java already has built-in the ArrayList class to dynamically allocate space for arrays. See this program that will store as many scores as we would like in an ArrayList.
Why do we need 2-D arrays? Some information is easier to manage that way (tables, text, matrices, etc.)
Examples: Using a 2-d array to represent a character for character recognition.
[CS Dept] [UIC] [Prof. Reed]