Incremental Java
Why Arrays? An Introduction to Arrays

Why Arrays?

We've looked at a Java class called ArrayList. Think about the name, ArrayList. It has the word array in it, so this suggests arrays are important to ArrayList. That's true. What is an array, and why do we need arrays if ArrayList already exists?

In many languages, including C, C++, Pascal, FORTRAN, etc., arrays are an important structure to hold data. It allows us to hold many objects of the same type, and more importantly, to use a for loop to access the elements by their index.

So far that sounds just like an ArrayList, but arrays came first. Before there was object oriented programming and classes and objects, programmers were using arrays to do what we now do with ArrayLists.

Differences Between Arrays and ArrayList

What are the differences between an array and an ArrayList? You should understand these differences when we see some examples of how arrays work.

Declaring an Array

Let's begin by declaring an array. Below, we declare a single dimensional array. (We can declare higher dimensional arrays, but will do so in a future section).
  int [] arr ;
The type of arr is int []. The brackets indicate it's an int array, instead of merely an int.

Arrays are objects. This means that arr is an object handle, which is initially null (holding no balloons). You need to construct an array, just as you need to construct any object. The syntax for constructing an array is a little different.

  int [] arr = new int[ 10 ] ;
To construct an array, In this example, the array has size 10. Once you've decided that the array has 10 elements, you can't make it any bigger or smaller. If you want to change the size, you need to create a new array of the desired size, and then copy elements from the old array to the new array, and use the new array.

In our example, arr can only hold int values. Arrays can hold primitive values, unlike ArrayList, which can only hold object values. Because arr's type is int [], we can't put in boolean values into elements of arr, nor objects. Arrays give us finer control over the type of values that go into each element. We don't have this control with an ArrayList, which can accept any object type.

One Dimensional Arrays

You can declare one-dimensional (1D) arrays with any non-negative size.
  int [] arr = new int[ 10 ];   // Array of size 10
  int [] arr2 = new int[ 100 ]; // Array of size 100
  int [] arr3 = new int[ 1 ];   // Array of size 1
  int [] arr4 = new int[ 0 ];   // Array of size 0!
For example, you can create an array of size 1 (an array of size 1 is not the same as an int variable), which is an array with 1 element. You can even create an array of size 0, which is not nearly as useless as you might expect. All of these arrays have type int []. Notice the type doesn't care about size. Thus, arr can hold any 1D int array of any non-negative size.

It Doesn't Have To Be Called arr

The names of our arrays have been called arr, arr2, and so forth. However, these are just variable names, so you can name them anything you want. Even so, you should follow good Java style rules. We use arr to make it more obvious that we're declaring array variables.

Arrays of Other Types

Of course, we can make String arrays or Object arrays, instead of int types.
   String [] arr5 = new String[ 10 ] ;
   Object [] arr6 = new Object[ 10 ] ;

Accessing Array Elements

You can access array elements using the index operator.
   int x = arr[ 2 ] ;
This accesses (i.e., reads) element 2 of the array. Each element in the array has type int, so, in particular, arr[ 2 ], has type int.

When you are constructing array, (as in new int[ 10 ]), then the value in the bracket, e.g., 10, refers to the size of the array. Anywhere else that you see an int expression in brackets (as in arr[ 2 ]), this number refers to the index of the array (just like the index in an ArrayList). arr[ 2 ] accesses (reads) element 2 of the array (recall there is an element 0, too).

Array elements can also appear on the LHS of an assignment operator. This means array elements are also lvalues (which is short for left values), which means they can be assigned to.

   arr[ 2 ] = -1 ;

Initializing Arrays

Arrays are initialized when constructed. For primitive type arrays, every element is initialized with 0 if int, 0.0 if double, false if boolean. For object type arrays, all elements are initialized to null.

All elements of a constructed array are initialized to the same value, most of the times.

However, Java does have a mechanism that allows you to initialize arrays to values you pick. This is somewhat useful for small sized arrays.

Here's an example:

   int [] arr = { 1, 2, 3, 4, 10, 11, 12 } ;
{ 1, 2, 3, 4, 10, 11, 12 } is an array initializer. What does this array initializer do?

In this example,

Array initializers can only be used in a declaration.

For example, this is wrong:

   int [] arr ;
   // NO! Initializers can only be used in declarations
   arr = { 1, 2, 3, 4, 10, 11, 12 } ;

Equivalent Code

Writing
  int [] arr = { 1, 2, 3, 4, 10, 11, 12 } ;
is equivalent to writing
   int [] arr = new int[ 7 ];
   arr[ 0 ] = 1 ;
   arr[ 1 ] = 2 ;
   arr[ 2 ] = 3 ;
   arr[ 3 ] = 4 ;
   arr[ 4 ] = 10 ;
   arr[ 5 ] = 11 ;
   arr[ 6 ] = 12 ;
As you can see, it's much shorter to use an initializer. However, once you get past 100 elements or so, it might be easier to write code that reads in values from a file.

Alternate Way to Write Arrays

I prefer to write an array as:
   int [] arr ;
You can easily declare several arrays this way:
   int [] arr, arr2, arr3 ;
All three variables, arr, arr2, and arr3, are one dimensional (or 1D) int arrays.

We wrote int [] as the type of the variables. You can also put brackets after the variable name. Here's an example.

   int arr[], arr2, arr3 ;
In this declaration, we start off with a type of int, instead of int []. However, by putting [] after arr, we make the type of arr into int []. Putting brackets after a variable name makes it into an array of the type of the declaration (in this case, int).

However, arr2 and arr3 are not arrays. They are int variables (don't let the names fool you). They do not have brackets after their names, so their type is the type of the declaration, which is int.

The type of the declaration is the type that starts the declaration (on the leftmost part of the declaration).

   // Type of this declaration is int
   int x, y, myArr[] ;          

   // Type of this declaration is int []
   int [] arr, arr2 ;

Personal Preferences

I don't like to write arrays with brackets after the variable name. I like all variables in a declaration to have the same type, and here, we can mix types.

Why does Java give you two ways to declare the type of an array? The main reason is that C and C++ declare arrays similar to the second way (with the brackets after the variable name). The Java designers liked the first way (the one I presented first), but wanted C/C++ programmers to declare arrays the way they were used to.

public static void main( String [] args )

After so many lessons, we can finally explain this monstrosity. Even though we can explain what String [] args means (i.e., it's an 1D String array), we still have to wait to explain how it's used. For now, ignore it. (Or look it up on the Web, or read ahead to the section on Command Line Arguments).

You can see why some teachers hate this method signature. It takes a lot of work to explain it.