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.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   understanding arrays
  4.   */
  5.  
  6. class daysinmonth{
  7. public static void main (String args[]) {
  8. int[] numberofdays ;
  9. numberofdays = new int[12];
  10. int i ;
  11.  
  12. numberofdays[0] = 31; // In month of January
  13. numberofdays[1] = 28; // In month of Feb
  14. numberofdays[2] = 31; // In month of March
  15. numberofdays[3] = 30; // In month of April
  16. numberofdays[4] = 31; // In month of May
  17. numberofdays[5] = 30; // In month of June
  18. numberofdays[6] = 31; // In month of July
  19. numberofdays[7] = 31; // In month of Aug
  20. numberofdays[8] = 30; // In month of Sept
  21. numberofdays[9] = 31; // In month of Oct
  22. numberofdays[10] = 30; // In month of Nov
  23. numberofdays[11] = 31; // In month of Dec
  24.  
  25. for ( i=0; i<=11; i++)
  26.  
  27. System.out.println(" Number of days in month "+ (i+1) + " is " + numberofdays[i]);
  28. }
  29.  
  30. }


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.



  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   Declaring and assigning the array in a single step
  4.   */
  5.  
  6. class daysinmonth{
  7. public static void main (String args[]) {
  8. int[] numberofdays = {31,28,31,30,31,30,31,31,30,31,30,31} ;
  9. int i;
  10. for ( i=0; i<=11; i++)
  11.  
  12. System.out.println(" Number of days in month "+ (i+1) + " is " + numberofdays[i]);
  13. }
  14.  
  15. }
  16.  


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.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   Finding length of Array
  4.   */
  5. class arrayexample{
  6. public static void main (String args[]) {
  7. int[] numberofdays = {31,28,31,30,31,30,31,31,30,31,30,31} ;
  8. int length = numberofdays.length;
  9. System.out.println(" Number of months is " + length);
  10. }
  11. }


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.

  1. /*
  2.   ReferenceDesigner.com Java Tutorial
  3.   You can not increase size of array
  4.   */
  5.  
  6. class arrayexample{
  7. public static void main (String args[]) {
  8. int[] x = {10,20} ;
  9. System.out.println(" Number of elements before adding more elements " + x.length);
  10. x[2]=30; // Illegal - throws exception
  11. x[3]=40;
  12. System.out.println(" Number of elements after adding more elements " + x.length);
  13. }
  14. }


You can dynamically change the size of an array using ArrayList which we will cover later on.