Array in Java
An array object contains fixed number of values of a single type. To understand the necessity of array, consider a class of 50 students. We need to store their marks in Mathematics in variables. If we use individual variables we will need to declare 50 variables. This is not programming. With array we can declare a single variabe that will hold marks of all the 50 students.
Following is a practical example where we have stored the number of days in each of the 12 months in an array. You may want to copy paste the program and compile and run it to see it in action.
|
If you compile and run, you get the following output
Number of days in month 1 is 31 Number of days in month 2 is 28 Number of days in month 3 is 31 Number of days in month 4 is 30 Number of days in month 5 is 31 Number of days in month 6 is 30 Number of days in month 7 is 31 Number of days in month 8 is 31 Number of days in month 9 is 30 Number of days in month 10 is 31 Number of days in month 11 is 30 Number of days in month 12 is 31 |
Declaring an Array
Declaring an array consists of two parts - its type and its name. In the above example we declared the array with the statement
int[] numberofdays ;
The int indicates the type of the array and numberofdays indicates the name of the variable. The square bracket following the type int is a special symbol the indicates that this symbol is an array and contains and sequence of elements of same type.
Declaration only tells the compiler that this variable will hold the array of a specified type. It does not actually create an array.
Creating and Initializing an Array
One simple create an array is with the new operator. In the above example, we did it using
numberofdays = new int[12];
It delcares that the array variable numberofdays has a size of 12. It can hold 12 numbers of type int.
Assigning and accessing values
An individual member of an Array can be assigned values in this fashion.
numberofdays[0] = 31; // In month of January
numberofdays[1] = 28; // In month of Feb
numberofdays[2] = 31; // In month of March
In the statement the 0 inside the square bracket is called the index of the array. Notice that the index starts with 0 and not 1. Individual members of the array can be accessed in the same fashion.
System.out.println(" Number of days in Janauary is " + numberofdays[0]);
System.out.println(" Number of days in Feb is " + numberofdays[1]);
Alternate way of declaring and Initializing Array
Ther is another way declaring the array. We could combine
int[] numberofdays ; numberofdays = new int[12];
into a single line as
int[] numberofdays = new int[12];
We could also create and initialize the array in a single step. In our example, the array could be declared and initialized in a single step using
int[] numberofdays = {31,28,31,30,31,30,31,31,30,31,30,31} ;
Notice that we do not need to explicitly declare the length of the array. It is determined by the number of elements in the array.
See the complete example with this type of declaration and assignment.
|
Finding Length of an Array
We had earlier indicated that array is an object in Java. We will discuss what a class and a pre defined class is later on. For, just be aware that a an in built class supports a number of functions in it. It makes our life easy. We will no dicuss some of the in built functions is available. The first one is the length of an array. Look a the following example, where we have printed the length of the array.
|
The length of an array can be found using
length = numberofdays.length;
where numberofdays is an array.
Increasing Size of an Array
You can not increase size of an array. If you attemt as in the example below, it will compile but will throw exception at run time.
|
You can dynamically change the size of an array using ArrayList which we will cover later on.