Java Fundamentals  «Prev  Next»
Lesson 5 Storing lists of data with arrays
Objective Use arrays to store lists of data in Java.

Storing lists of Data with Arrays

A good way to understand the array creation process is to think of a newly declared array variable as a placeholder for an array. The array variable at this stage is like a label that you intend to place on a box to identify its contents. The memory for the array is not allocated until you use the
new  
operator. Using the box analogy, you do not get the box for your label until you use the new operator. It does not make sense to try and store something in a label without a box, and consequently it does not make sense to use an array prior to allocating its memory with the new operator. You might be wondering why other Java data types do not require use of the new operator. The reason you have to use the new operator to create arrays is because arrays are not simple data types. Arrays are composite data types, which really means that they are objects[1].
Java objects must always be created using the new operator. The one exception, of course, is if you initialize an array when you declare it.

Arrays are declared by stating the type of element the array will hold, which can be an object or a primitive, followed by square brackets to the left or right of the identifier.
Declaring an array of primitives:
int[] key;    // brackets before name (recommended)
int key [];   // brackets after name (legal but less readable)
// spaces between the name and [] legal, but bad
Declaring an array of object references:
Thread[] threads; // Recommended
Thread threads[]; // Legal but less readable

When declaring an array reference, you should always put the array brackets immediately after the declared type, rather than after the identifier (variable name). By using this notation, anyone reading the code can easily tell that, key is a reference to an int array object and not an int primitive. We can also declare multidimensional arrays, which are in fact arrays of arrays.
This can be done in the following manner:


String[][][] occupantName;  // recommended
String[] managerName [];   // more confusing, but legal

The first example is a three-dimensional array (an array of arrays of arrays) and the second is a two-dimensional array. Notice in the second example we have one square bracket before the variable name and one after. This is perfectly legal to the compiler, proving once again that just because it is legal does not mean it is right.
It is never legal to include the size of the array in your declaration. Yes, we know you can do that in some other languages, which is why you might see a question or two in the exam that include code similar to the following:
int[5] scores; // will NOT compile

The preceding code won't make it past the compiler. Remember, the JVM does not allocate space until you actually instantiate the array object. That's when size matters.

Definition of Java Array

An array is a Java construct used to store multiple items of data in a list.
The elements in an array are numbered, and are accessed based on their number and the number of an array element is known as the element's index. Array elements must all be of the same data type.

In Java, this:
int[] tempArray = {2,3,5,7};

means exactly the same thing as this:
int[] tempArray = new int[]{2, 3, 5, 7};
There is an implicit new in the first form. Array variables are declared with square brackets ([]). To actually create an array, you use the new operator and specify the number of elements the array is to hold, like this:

int[] players = new int[12];
char[] vowels = new char[5];

The new operator in this example is used to create an int array containing twelve elements, while the char array contains five elements. The new operator is responsible for allocating memory for an array, and must always be used when creating new arrays. You can not use an array without first allocating memory for it. After an array has been created, you access its elements using square brackets and an integer index, like this:

vowels[0] = 'A';
vowels[1] = 'E';
vowels[4] = 'U';


Array indexes range from 0 to the length of the array minus 1. This is a result of arrays being zero-based, which means their indexes always start at zero. So, in the case of the vowels variable, its indexes range from 0 to 4 since the array is 5 characters long.
You can bypass the new operator if you initialize an array when you first declare it. In this case, you create an array and fill it with values in a single line of code, like this:

char[] vowels = { 'A', 'E', 'I', 'O', 'U' };

Creating Accessing Array - Exercise Result

Create and access an array in this exercise
Creating and Accessing Array - Exercise
[1] Object: An object is a collection of data and the procedures (methods) that act on that data.