Arrays
Arrays
Rating:
An
Elements are stored in an array in a definite order using nonnegative incremental values called the indices. An element within an array is uniquely identified by its location (a positive integer value called the index) within the array. A particular element in an array is accessed by using its index value, the first element being at index 0 and the last at index one less than the length of the array.
The figure given below shows an array of length 10, or in other words, an array that can hold 10 elements of the same data type:

Arrays are created in the computer memory using the following three steps:
- Declaring a variable to refer to an array.
- Allocating memory locations for the array.
- Putting values into the array.
The array declaration statement informs the compiler about the data type and the name of the array variable. The name used to declare an array variable must be a valid identifier. An array declaration statement takes the same form as the variable declaration.
Here, the type represents the data type of the elements that the array is going to hold and the square brackets (i.e., []) after the type suggests that the variable array_name refers to an array. The square brackets may be placed before or after the array_name.
For example,
The array construction expression constructs an array at the run time by allocating the required memory locations to the array. This is done by specifying the size of the array and using the new operator. After an array construction statement is executed, the array becomes a fixed length structure capable of storing values of the specified data type.
Here, the type after the new operator specifies the data type of the values the array stores, and the size within [] specifies the length of the array. The equality operator assigns the reference of the array to the array_name.
For example,
The declaration and construction of arrays can be combined in single line as shown below:
As soon as the array is created using this form of array creation statement, its elements are automatically initialized to their default values. The table given below shows the initial value of the elements of an array:
| boolean | false |
| byte | 0 |
| short | 0 |
| int | 0 |
| long | 0L |
| char | 'u0000' |
| float | 0.0F |
| double | 0.0D |
| Object reference | null |
array1 = new int[size];
Although the elements of an array are automatically initialized as soon the array is created using the new operator discussed earlier, a user can explicitly initialize the elements of the array using an array initialization expression. An element within an array can be initialized explicitly by using the index associated with it.
For example,
This statement initializes the third element of the array referenced by array1 with the value 30.
An array initializer is a list of comma-separated values or expressions enclosed within curly braces i.e., between '{' and '}'. The array initializer is a language-supported feature to simultaneously create and initialize the elements of an array. When an array initializer is used to initialize an array, there is no need to specify the size of the array. The number of elements inside the array initializer determines the size of the array.
For example,
This statement creates an array of five float values, as the number of elements in the array initializer is five. The given array initializer initializes the first element of the array with the value 1.1f, the second with the value 1.2f, and so on. The reference of the array created in this expression is assigned to the array variable array2 by using the equality operator. This form of array creation statements must be declared and initialized in a single line. Splitting this form in more than one line will cause a compile error.
An array can also be created using an array initializer along with the new operator as shown below:
This statement creates an array of float values in the same way as discussed earlier. Here, the keyword new is followed by the information about the type of values contained inside the curly braces. This statement can be split in more than one line, i.e., the array variable may be declared in a separate line and later initialized with the reference with the reference of the array after the array is created as shown below:
array2 = new float[] {1.1f, 1.2f, 1.3f, 1.4f, 1.5f};
All arrays in Java have a public final field known as length. This field is used to find the length of the array. Once an array is created, its size can be obtained by using the length field qualified by the array name along with a dot operator. For example, the given statement finds the size of the array named MyArray.
An array having more than one dimension is called a multidimensional array. In Java, multidimensional arrays are simply arrays of arrays. The most commonly used multidimensional array is a two-dimensional array. In a two-dimensional array, an element within an array is uniquely identified by a set of values representing the row index and the column index.
In order to declare a variable to refer to a two-dimensional array, two pairs of square brackets are used.
For example,
In order to create a multidimensional array, it is necessary to specify the size for the leftmost dimension. The memory allocations for the other dimensions can be done separately. For example,
char2d = new char[3] [];
Here, the first expression creates a two dimensional array conceptually having 3 rows and 4 columns. The second expression creates a two-dimensional array conceptually having 3 rows with memory locations for the columns yet to be allocated separately for each elements of the row.
A multi-dimensional array can also be created using an array initializer as shown below:
This statement creates an array having three elements in its row with each element of the row referencing to a four-element column array.
An array of objects is created in the same way as the array of primitive types. The declaration and the creation of the array of objects take the same form as the array of primitives.
This statement creates an array of five string objects with the array variable str assigned the reference to the array. After the array of objects is constructed using a new operator, each of the elements of array is initialized with the default value null as shown in the table given earlier. However, this array may also be constructed and initialized at the same time by using an array initializer as given below:
This statement creates an array of string objects having five elements with the first element referencing to the string "Jack", the second to the string "John", and so on.
Rating:
Other articles
- What is the longValue() method?
- What is the Double class?
- What is a Scanner?
- What is the floatValue() method?
- What is modifier?