Parallel Arrays
The following arrays represent the data from a dog show. Notice that the arrays do not
all contain the same "type" of data. It is often necessary to represent data in a "table"
form, as shown below. Such data, can be stored using parallel arrays. Parallel arrays
are several arrays with the same number of elements that work in tandem to organize
data.
dogname
Wally Skeeter Corky Jessie Sadie
round1
18 22 12 17 15
round2
20 25 16 18 17
**The true beauty of parallel arrays, is that each array
may be of a different data type.
In the data represented above, the first array is the dog's name, the second array is the dog's score in
round 1 of the competition, and the third array is the dog's score in round 2 of the competition. The arrays
are parallel, in that the dog in the first element of the first array has the scores represented in the first
elements of the second and third arrays.
String[ ] dogname = {"Wally", "Skeeter", "Corky", "Jessie", "Sadie"};
int[ ] round1 = {18,22,12,17,15};
int[ ] round2 = {20,25,16,18,17};
//Printing the dog competition information:
for(int index = 0; index < dogname.length; index++)
{
System.out.print(dogname[index] + " ");
System.out.print(round1[index] + " ");
System.out.println(round2[index]);
}
Passing Arrays to Methods
(resembles "pass-by-reference")
When discussing arguments/parameters and methods, we talked about passing-by-value. Copies of
argument values are sent to the method, where the copy is manipulated and in certain cases, one
value may be returned. While the copied values may change in the method, the original values in
main did not change (unless purposely reassigned after the method).
The situation, when working with arrays, is somewhat different. If we were to make copies of arrays
to be sent to methods, we could potentially be copying very large amounts of data.
Not very efficient!
Passing an array mimics a concept called "pass-by-reference", meaning that when an array is passed
as an argument, its memory address location (its "reference") is used. In this way, the contents of
an array CAN be changed inside of a method, since we are dealing directly with the actual array and
not with a copy of the array.
Note: In Java, a pointer value to the memory address location is used, making arrays "pass-by-value"
(and not actually "pass-by-reference" as seen in C++.)
You can pass an entire array, or a single element from an array, to a method.
A method declaration can include array parameters, such as passing the entire array:
public static void testArray(int [ ] num) {
Notice that the int [ ] indicates an array parameter.
A method declaration can include array parameters, such as passing a single array element:
public static void testArray(int number) {
Notice that passing a single array element is similar to passing any single value.
Only the data stored in this single element is passed (not the entire array).
EXAMPLE Snippet:
int [ ] num = {1, 2, 3};
System.out.println("num[0] = " + num[0] + "\n num[1] = " + num[1] + "\n num[2] =" + num[2]);
System.out.println("Now, call the method.");
testArray(num); // Method call
System.out.println("num[0] = " + num[0] + "\n num[1] = " + num[1] + "\n num[2] =" + num[2]);
...
// Method for testing array
Output:
// this method will change the original array
num[0] = 1
public static void testArray(int [ ] value)
num[1] = 2
{
num[2] = 3
value[0] = 4;
Now, call the method.
value[1] = 5;
num[0] = 4
value[2] = 6;
num[1] = 5
}
num[2] = 6
(The values in the array have been changed.
Notice that nothing was "returned".)
You will need to be careful when sending an array to a method. Remember that any
changes made to the array in the method will change the data in the original array. Be
sure that your intention is to change the original data (thus losing the original data).
Example: Fill an array with 10 integer values. Pass the array to a method that will add up the values and
return the sum. (In this example, no original data in the array will be altered. The array information will
simply be used to find the sum.)
import java.util.Scanner;
public class FindSum
{
public static void main (String [ ] args)
{
Scanner reply = new Scanner(System.in);
int [ ] number = new int [ 10]; // instantiate the array
int i;
int sum=0;
for ( i = 0; i < 10; i++ ) // fill the array
{
System.out.println("Enter number: ");
number[ i ] = reply.nextInt( );
}
sum = find_sum(number); // invoke the method
System.out.println("The sum is " + sum + ".");
reply.close();
}
public static int find_sum(int [ ] value) //method to find sum
{
int i, total = 0;
for(i = 0; i < 10; i++)
{
total = total + value[ i ];
}
return (total);
}
}
Finding the Maximum or Minimum
(finding the highest (or lowest) value in an array) Definition:
An algorithm is a
There are numerous algorithms that allow you to manipulate the programming method that
information stored in an array. Consider this method which allows you to performs a specific job.
determine the maximum value in an array of integers (could be easily
adapted to find minimum value instead):
//Method to find highest (maximum) value in array
public static int maximumValue(int [ ] array)
{
int length = array.length; // establish size of array
int max = array[0]; // start with max = first element
for(int i = 1; i < length; i++)
{
if(array[i] > max)
max = array[i];
}
return max; // return highest value in array
}