Array In Java with example

Array In Java with example

Array refers to the homogeneous collection of elements. Here homogeneous means same data type value can be stored in array. Also Array stores its element in contiguous memory fashion. It means when we create an array, memory allocated to the array will be in contiguous fashion.

While we create array, first we have to give the size of the array. Because size of the array is fixed and we can’t change it at run time.

Array follows the index based approach that means element stored in array start from the index 0 and grow by one each time. Suppose we have to store 1, 2 and 3 in array then at index 0, 1 element will be store. At index 1, 2 element will be stored and At index 2, 3 element will be stored.

For example:

1,2,3,4 are of int datatype. We can store all this int data into a array of int type.

Syntax Of Array

1) dataType[] varname;   // more preferred way.

2) dataType varname[];  //  less preferred way.

Example Of Array

 int[] varname;   // int array.
  or
 int varname[];   // int array.
  or
 char[] varname;  //  char array.

Advantages Of Array

  1. It is used to represent multiple data items of same type by using only single name like int [] arr={1,2,3}.
  2. It can be used to implement other data structures like linked lists, stacks, queues, trees, graphs etc.
  3. Helpful in mathematical operations like matrices with the help of 2D and 3D array.
  4. It increased code optimization in sorting and retrieving data efficiently.
  5. We can access any data from the given location by index value. We need not to perform any iteration for that.

Disadvantages Of Array

  1. Must be decide in advance the requirement of an program to define size of an array.
  2. Array are static in structure. It means that array is of fixed size. The memory which is allocated to array can not be increased or reduced.
  3. Memory space will be wasted if we allocated more memory space to array . And if we allocate less memory than requirement, then it will through errors.
  4. Elements of array are stored in consecutive memory locations. So insertions and deletions are very difficult and time consuming.

Java Program Example Of An Array

public class ArrayExample {
  public static void main(String args[]) {
    int arr[] = {1, 2, 3, 4, 5 }; 
    for (int i = 0; i < arr.length; i++)
    {
      System.out.println(arr[i]);
    }
  }
}

Output

1
2
3
4
5

Another Way To Use An Array

public class ArrayExample {
  public static void main(String args[]) {
    int arr[] = new int[5];
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;
    arr[3] = 40;
    arr[4] = 50;
    for (int i = 0; i < arr.length; i++)
    {
      System.out.println(arr[i]);
    }
  }
}

Output

10
20
30
40
50

Types of Array in java

There are two types of array.

  • Single Dimensional Array
  • Multidimensional Array

Single Dimensional Array

Syntax of single dimensional array

dataType[] variableName;    
dataType variableName[];  
dataType []variableName;

Syntax to Instantiate Single Dimensional Array

variableName=new datatype[size];  

Program to Demonstrate Single Dimensional Array

public class ArrayExample {
  public static void main(String args[]) {
    int arr[] = new int[7];
    arr[0] = 10;
    arr[1] = 20;
    arr[2] = 30;
    arr[3] = 40;
    arr[4] = 50;
    arr[5] = 60;
    arr[6] = 70;
    for (int i = 0; i < arr.length; i++)
    {
      System.out.println(arr[i]);
    }
  }
}

Output

10
20
30
40
50
60
70

Multidimensional Array in Java

Syntax of Multidimensional array

dataType[][] variableName; 
dataType [][]variableName; 
dataType variableName[][]; 
dataType []variableName[];   

Syntax to Instantiate Multidimensional Array

variableName = new int[3][3];

Program to Demonstrate Multidimensional Array

public class MultidimensionalArray {
  public static void main(String args[]) {
    int arr[][] = {
      {1,2,3},
      {4,5,6},
      {7,8,9}
    };
    for (int i = 0; i < 3; i++) {
      for (int j = 0; j < 3; j++) {
        System.out.print(arr[i][j] + " ");
      }
      System.out.println();
    }
  }
}

Output

1 2 3 
4 5 6 
7 8 9