JAVA: Arrays



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 we 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 to the name "Joe Smith", the code would be as follows:
names[0] = "Joe Smith";

1D Arrays

Declaring 1D 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. On the other hand, creating an excessively large array with more spaces than you would need can be a drain on storage resources. 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 (because indices start at zero), 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...
}
Note: Java arrays do not keep track of their logical size. If you need this information, you will have to create a counter variable that starts at 0 and increments each time an element is added to the array.
The code in the loop body can be used for a variety of events such as: 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.

For Each Loop for Collections
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 temporary 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);
}
In the for each loop, there is an array named students created of the String type. The loop cycles through that array, one element at a time (without indices), assigning the current value to the variable name for that run of the loop.

2D Arrays

Coming soon...