Parallel Arrays

From CompSciWiki
Revision as of 17:23, 28 November 2007 by Shawn (Talk | contribs)

Jump to: navigation, search

COMP 1010 Home > 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.

   

{{{Body}}}

Parallel arrays are an advanced use of arrays. Creation and use 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 whenever you need to create a list of items, but all the information you need to store will not fit into a single Primitive Data Type. For example, you need to to keep a list of classroom of people's names and addresses. A single array of Strings seperating the addresses from names with a comma might not practical, and difficult to implement. Using one array for names, and another array of equal size for addresses would be easier and more practical.

    
    int sizeOfClassroom = 30;
    String []names = new String[sizeOfClassroom];
    String []addresses = new String[sizeOfClassroom];

As you can see, setting up a set of parallel arrays is simply as easy as simply declaring two different arrays. Ensure that the arrays you wish to use in parallel are are created using the same length.

Why Parallel Arrays

So, you may ask, why bother with parallel arrays? Simple variables have covered everything I've needed to do. That is, until now. Complex programs require keeping track of more data than is easily fathomable. 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 shoehorned into a single data type. You wouldn't keep track of someone'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 instead make parallel arrays.

    int sizeOfClassroom = 30;
    String []names = new String[sizeOfClassroom];
    String []addresses = new String[sizeOfClassroom];
    String []ages = new int[sizeOfClassroom];

Filling up the Arrays

Typically you want to fill up all the data in a set of parallel arrays at the same time, using something like this structure, by asking the program's user.

    int sizeOfClassroom = 30;
    String []names = new String[sizeOfClassroom];
    String []addresses = new String[sizeOfClassroom];
    String []ages = new int[sizeOfClassroom];

    for(int i=0; i < sizeOfClassroom; i++)
    {
        names[i] = "Joe Smith";
        addresses[i] = "123 Fake Ave.";
        ages[i] = 20;
    }

Of course in the above example all of the students data is being set to the same thing, but when putting parallel arrays to practical use will require you to get information from a file, or from the user while your program is running.

Once you have run the above code, you'll essentially have a table of names, addresses and ages

NameJoe SmithJoe SmithJoe Smith
Address123 Fake Ave.123 Fake Ave.123 Fake Ave.
Age202020