Arrays
Arrays
An array is a collection of a fixed number of values or objects of a single type. Once declared, the length of an array
cannot be altered. The values or objects contained in an array are called elements and they are accessed by the
common name (i.e. the array name). Arrays can be one dimensional or multi-dimensional. The array elements are
accessed by their numerical indexes. Indexes start from 0, i.e. the index of the first element is 0.
One Dimensional Array: - The general form of the declaration of a one dimensional array is
datatype arrayName[];
or
datatype[] arrayName;
E.g.
char vowels[];
In the above example, we have declared a char array and have named it vowels. Right now, it does not contain
any elements and it is set to null. To actually assign values to the elements of this array, do the following:
The above statement causes the vowels array to hold 5 elements. new is a java keyword and here it allocates
memory to the array. Now, to assign values to each elements, do the following:
In an array, arrayName.length gives the total number of elements in that array. In our example,
vowels.length will yield 5.
class CharArray
{
public static void main(String args[])
{
char vowels[];
vowels = new char[5];
vowels[0] = 'a'; // The first element has the index 0;
vowels[1] = 'e';
vowels[2] = 'i';
vowels[3] = 'o';
vowels[4] = 'u';
System.out.println(vowels[0]);
System.out.println(vowels[1]);
System.out.println(vowels[2]);
System.out.println(vowels[3]);
System.out.println(vowels[4]);
a
e
i
o
u
No of elements in vowels array = 5
Multi-dimensional Array:- Multi-dimensional arrays are actually arrays of arrays, i.e. an array whose elements are
themselves an array. For example, a 2-dimensional array is the one whose elements are themselves a 1-
dimensional array. A 2-dimensional array can be visualized in a row-column style and is used to represent data in
rows and columns.
System.out.println(arr[0][0] + " " + arr[0][1] + " " + arr[0][2]); //print the 1st
//row
System.out.println(arr[1][0] + " " + arr[1][1] + " " + arr[1][2]); //print the
//2nd row
System.out.println(arr[2][0] + " " + arr[2][1] + " " + arr[2][2]); //print the 3rd
//row
}
}
Note that the above program has allocated memory for both the dimensions automatically when the array was
declared. It is also possible to allocate memory manually. This is particularly helpful if you want to create a 2-
dimensional irregular array. A 2-dimensional irregular array is the one whose elements are 1-dimensional arrays of
different lengths. The program below explains 2-dimensional irregular array and manual memory allocation. It
creates a 2-dimensional array whose first element is 1-dimensional array of length 1, second element is a 1-
dimensional array of length 2 and the third element is a 1-dimensional array of length 3.
arr[0][0] = 1;
System.out.println(arr[0][0]);
System.out.println(arr[1][0] + " " + arr[1][1]);
System.out.println(arr[2][0] + " " + arr[2][1] + " " + arr[2][2]);
}
}
1
2 3
4 5 6
In a similar way, a 3-dimensional array is the one whose elements are 2-dimensional arrays. The declaration
statement below creates a 3-dimensional array of 2 elements which are 2-dimensional arrays with 3 rows and 4
columns.
A 3-dimensional array can be visualized as the pages of a book. The pages are the elements of the array and each
page is a 2-dimensional array consisting of rows and columns.
An array can be initialized with values at the time of the declaration of the array. The following piece of code
demonstrates such technique of initialization of a 1-dimensional array:
int intArr = {
{1,2,3,4,5},
{11,22,33,44,55},
{111,222,333,444,555}
Now an assignment for the students: - Write a program to create a 3-dimensional array of data type int whose
elements are 2-dimensional arrays of 3 rows and 4 columns and assign values to it.