Parallel Arrays

From CompSciWiki
Jump to: navigation, search

COMP 1010 Home > More With Arrays > Parallel Arrays


Introduction

What do you do if you want to hold onto data for not just one thing at a time? We can combine the data storage of arrays with the different variable types to create parallel lists of data to keep track of data for not just a single item at time, but hundreds or thousands of them. Parallel arrays are an advanced use of arrays. The creation and use of arrays is fairly simple, and a natural step from creating and using simple arrays. Parallel arrays should be used wherever you want to make a complex structure of similar or related data, but do not have the means.

Parallel Array Creation

Parallel arrays should be used when you need to create more than one list of items for related data. For example, you need to to keep a list of students names and a list of their addresses. A single array of Strings separating the addresses from names with a comma might not be practical and be difficult to implement. Using one array for the students names, and another array of equal size for their addresses would be easier and more practical.


Example 1:

 int sizeOfClassroom = 3; //Size of the classroom. Used to initialize parallel arrays 
String []studentNames = new String[sizeOfClassroom]; //Array of student names
String []studentAddresses = new String[sizeOfClassroom]; //Array of student Addresses 


As you can see above, setting up parallel arrays is simple as declaring two different arrays. Ensure that the arrays you wish to use in parallel are are created using the same length. This is where the variable sizeOfClassroom comes in handy. Initializing the parallel arrays using this variable will ensure the arrays are of the same size.


Why Parallel Arrays

So, you may ask, why use parallel arrays? Simple variables have done everything I've needed to do. That is, until now. Complex programs require keeping track of more data than can be placed into simple variables. Therefore arrays will need to be used to keep track of all that data. Not only is there much data to keep track of, but it is complex as well, and cannot be stored in a single data type. You wouldn't keep track of a person's age and name in a single int variable, so imagine how impractical it would be to make an entire list of them.


So, we use parallel arrays.


Example 2:

 int sizeOfClassroom = 3; //Size of the classroom. Used to initialize parallel arrays 
String []studentNames = new String[sizeOfClassroom]; //Array of student names
String []studentAddresses = new String[sizeOfClassroom]; //Array of student addresses
int []studentAges = new int[sizeOfClassroom]; //Array of student ages 


Adding Data to Parallel Arrays

Typically you want to fill up all the data in a set of parallel arrays at the same time. This will help ensure that the same index in each parallel array contains information for one item.


Example 3a - Using JOptionPane

 int sizeOfClassroom = 3; //Size of the classroom. Used to initialize parallel arrays
String [] studentNames = new String[sizeOfClassroom]; //Array of student names
String [] studentAddresses = new String[sizeOfClassroom]; //Array of student addresses
int [] studentAges = new int[sizeOfClassroom]; //Array of student ages



/*
This for loop will start at index 0 and run till the size of the classroom is reached. It will ask the user for a student name, student     
address and student age. This input will be saved into the three arrays. After all three values are entered for each student, 
the data will be printed to the screen.
*/

for(int i=0; i < sizeOfClassroom; i++)
{
        
   studentNames[i] = JOptionPane.showInputDialog("Enter student name");
   studentAddresses[i] = JOptionPane.showInputDialog("Enter student address");
   studentAges[i] = Integer.parseInt(JOptionPane.showInputDialog("Enter student age"));
	
   System.out.println(studentNames[i]+” “+studentAddresses[i]+” "+studentAges[i]);

} 


Example 3b - Using Scanner

 Scanner userInput=new Scanner(System.in);
int sizeOfClassroom = 3; //Size of the classroom. Used to initialize parallel arrays
String [] studentNames = new String[sizeOfClassroom]; //Array of student names
String [] studentAddresses = new String[sizeOfClassroom]; //Array of student addresses
int [] studentAges = new int[sizeOfClassroom]; //Array of student ages

/* 
This for loop will start at index 0 and run till the size of the clasroom is reached. It will ask the user for a student name, student
address, and student age. This input will be saved into the three arrays. After all three values are entered for each student, the data will
be printed to the screen.
*/

for(int i=0; i < sizeOfClassroom; i++)
{

   System.out.print("Enter Student Name: ");
   studentNames[i] = userInput.nextLine();

   System.out.print("Enter Student Address: ");
   studentAddresses[i] = userInput.nextLine();

   System.out.print("Enter Student Age: ");
   studentAges[i] = Integer.parseInt(userInput.nextLine());

   System.out.println(studentNames[i]+” “+studentAddresses[i]+” "+studentAges[i]);

} 


Reminder:

To use scanner you have to import it using this line of code:

 import java.util.Scanner; 

To use JOptionPane you have to import it using this line of code:

 import javax.swing.JOptionPane; 


For Example, say the user typed in Ryan Jones for the student name, 123 Fake Street for the student address, and 10 for the student age. Index 0 of the studentNames array would contain Ryan Jones, index 0 of the studentAddresses array would contain 123 Fake Street, and index 0 of the studentAges array would contain 10. As is seen below, index 0 of all three arrays contains information for Ryan Jones.


 Name      Ryan Jones       etc.
Address   123 Fake Street  etc.
Age       10               etc.
(Index)   0                etc. 


In the above two examples (3a and 3b), data is provided by the user via the JOptionPane input dialog boxes or using Scanner, and the data is stored in the parallel arrays. A more practical use would require the data to be read in from a file.


Once you have run the above code, you'll essentially have a table of student names, student addresses and student ages. Below, the 0,1 and 2 indicate the index of the array in which the item is stored. These 0's, 1's and 2’s will not appear when the data is printed. The reason it is shown here is to make it clear of the placement in the parallel arrays.


 Student Name        Student0 Name       Student1 Name       Student2 Name
Student Address     Student0 Address    Student1 Address    Student2 Address
Student Age         Student0 Age        Student1 Age        Student2 Age 


For example, if the below values were the values typed in by the user they would be printed as follows:


 Student Name      Ryan Jones        Steven Jones       Jordan Michaels   
Student Address   123 Fake Street   6262 Home Street   45 New Street     
Student Age       10                11                 10 


Watch Out For

Parallel arrays present two major difficulties to the code writer:

  1. Sorting
  2. Searching


Sorting a set or parallel arrays comes with a few hazards. You must be able to sort all the arrays at once, and you must be able to sort them all in some predefined fashion. However if you know which array is the most important, the one you actually want to sort, then that cures half the problem.


The next step is to create your sorting method. You cannot just put each array into a predefined sorting method, as that may destroy the parallelism of your arrays, putting Bob's addresses, which was in slot i, into where Joe's address would go in slot j. Therefore, you must build your own sorting method that keeps all the data in parallel, moving not just Joe's name, but his address and his age as well to its new spot.


Searching is a little easier, but you must know what you are looking for. You must know if you are looking for the name, address or age, or some other piece of data, and then only search on the appropriate array.


As a final note, remember to initialize, insert into, delete and perform operations on parallel arrays at the same time. Failure to do so can cause errors or incorrect data being retrieved and displayed.


Section Summary

This section showed you how to work with parallel arrays. Working with parallel arrays is very similar to working with single arrays except that, instead of keeping track of one piece of data per item, you keep track of greater than one piece of data per item. You should now know what parallel arrays look like and how to deal with them.

Previous Page: Sorting Arrays Next Page: More With Arrays Review Questions and Exercises