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.
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,
new
new int
new int[
new int[ 10
new int[ 10 ]
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.
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.
String [] arr5 = new String[ 10 ] ; Object [] arr6 = new Object[ 10 ] ;
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 ;
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,
Index | 0 | 1 | 2 | 3 | 4 | 5 | 6 |
Value | 1 | 2 | 3 | 4 | 10 | 11 | 12 |
For example, this is wrong:
int [] arr ; // NO! Initializers can only be used in declarations arr = { 1, 2, 3, 4, 10, 11, 12 } ;
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.
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 ;
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.
You can see why some teachers hate this method signature. It takes a lot of work to explain it.