Array Basics

An array is an ordered collection of data of the same type. Arrays allow us to store multiple values in a structure that can be looped through as opposed to creating multiple variables or writing over previous information. For example, if we wanted to store three names in a program, we would write the following: String name1="Mickey Mouse"; String name2="Tom Cat"; String name3="Big Bird";
However, what if I wanted to store 30 names? 100? 1,000? That is a lot of variables to keep track of. Arrays allow us to store all of the names in one variable that is sectioned into parts.

When an array is instantiated, the computer allocates a set of consecutive spaces in memory. These spaces are then sequentially numbered for reference using indicies. The first index of an array is always 0. This means that an array with 10 elements has indicies of 0-9, inclusive. Trying to reference information in index 10 will result in an error.

The syntax of referencing an array element is arrayName[index]. This syntax can be on the left or right side of an assignment statement, compared to other values, output, or sent as a parameter to a method. In other words, it can be treated like any other variable. So, to assign the first element of a names array tot he name "Joe Smith", the code would be as follows: names[0] = "Joe Smith";

Declaring Arrays

When declaring a 1D array in Java, we need a data type, array name and maximum size. Be sure to choose a size large enough, as re-sizing an array in Java is a time and resource consuming exercise. An array of 30 names would be declared as follows: String [] students = new String[30];
If desired, the declaration (String [] students) and initialization (students=new String[30]) can be separated, as in all variables. Also, the size of the array can be set using a variable. This allows user input or calculations to determine the necessary size of the array.

A 1D array can also be declared, initialized and populated at the same time in Java. This is accomplished by creating a comma separated list of elements, placed in curly braces. int [] ages = {4, 7, 9};
It is important to note that, in Java, when an array is instantiated and given a size, that size cannot be altered during the run of the program. This means we have to choose our array size carefully so as to have enough space for all of the information, but not so much as to waste valuable program memory.

Traversing 1D Arrays
To traverse a 1D array, we need use a simple for loop. By starting at the value 0, and continuing until one less than the length, we can access all elements in the array. We can use the .length property of the array object to access the length of the array. Note that this is the physical size (actual spaces) not the logical size (filled spaces). for(int x=0; x<students.length; x++){ ...loop body... }
The code in the loop body can be used for a variety of events such as:
  1. Outputting the information (System.out.println(students[x])
  2. Assigning information (students[x] = reader.readLine();)
  3. Searching for a value (if(students[x].equals("Joe Smith")))
  4. Keep a running total (sum += studentGrades[x])

That being said, there are some things that we can't do in Java arrays. We cannot add/delete elements within the array without leaving a blank space (null or 0) or writing a method to shift all of the remaining elements. This is one of the downfalls of the array structure.

There is another way to cycle through an array of elements called a for each loop. This loop takes each element and stores it in a variable to be used for that run of the loop. The for each loop does not allow the user to alter the structure of the array (adding/deleteing elements), but does allow access to elements for alteration. A for each loop to output all of the elements of an array would look as follows: for(String name:students){ System.out.println(name); }


Parallel Arrays

Sometimes, we have multiple pieces of data that belong together, such as a student name, phone number and grade. Until we learn how to create objects in Java, we can use multiple arrays of the same size to store the information. These arrays are called parallel arrays because the data at each index is partnered with the data in the same index of the other arrays to represent all of the information about a given student. This could present a problem if we sort one of the arrays. We would have to be sure to switch the data in the others or our data would no longer correspond.