Arrays in Java

Arrays are collection of elements of a similar type.

An array is a data structure in Java which stores a sequential collection of elements of a fixed size and having the same data type. Only a fixed set of elements can be stored here.

Arrays are objects in java.


Arrays is a class in java package: java.util.Arrays We can define arrays in two ways:

  • Static initialization :
    int x =10;
    int a[] ={ 10, 20, 30, 40 };
    double []d = {1.2, 2.3, 3.4 };
  • Dynamic initialization :
    int a[] = new int[4]; 
              OR
    int a[] = null;
    a = new int[4]; 
    a[0] = 11;
    a[1] = 22;
    a[2] = 201 + 1 * 4; // = 201+4 =205
    a[3] = 201 + 2 * 4; // = 209

Example 1:

Example 2:


It is also called as arrays of array.