«Back to Home

Core Java

Topics

Array In Java

Array

In Java, array is a collection of similar types of data, which have adjacent memory location. Java array is a data structure, where we store similar types of elements. We can store only fixed set of elements in a Java array.
 
Array in Java is index based and first element of the array is stored at 0 index.
 
In other words, we can say that an array is a container object, which holds the values of homogenous type. It is also called as a static data structure. The size of an array is specified at the time of an array declaration.
 
An array can be either primitive type or reference type and it gets memory in heap area.
 
Types of Array in Java

In Java, two types of array are there, which are,
  1. Single dimensional array

    Syntax

    dataType[] arr; or

    dataType []arr; or

    dataType arr[];

    Instantiation

    arrayRefVar=new datatype[size];

    Let’s see an example.

    Code
    1. public class JavaArray {  
    2.     public static void main(String args[]) {  
    3.         int arr[] = new int[10]; //declaration and instantiation of an array   
    4.         arr[0] = 25//array initialization    
    5.         arr[1] = 30;  
    6.         arr[2] = 35;  
    7.         arr[3] = 40;  
    8.         arr[4] = 45;  
    9.         arr[5] = 50;  
    10.         arr[6] = 55;  
    11.         arr[7] = 60;  
    12.         arr[8] = 65;  
    13.         arr[9] = 70;  
    14.         for (int i = 0; i < arr.length; i++) //length is the property of array    
    15.         {  
    16.             System.out.println(arr[i]);  
    17.         }  
    18.     }  
    19. }  
    20

    Output

    21

    In the example, mentioned above, first we declare, instantiate and initialize of an array. In Java, we can declare, instantiate and initialize the array simultaneously.

    Let’s see an example, given below.

    Code
    1. public class JavaArray {  
    2.        public static void main(String args[]) {  
    3.                int arr[] = {1045225};//declaration, instantiation and initialization    
    4.                for (int i = 0; i < arr.length; i++) {  
    5.                       System.out.println(arr[i]);  
    6.         }  
    7.     }  
    8. }  
    9.    
    22

    Output

    23

    In the example, mentioned above, we declare, instantiate and initialize the array simultaneously. Afterwards, traverse all elements of an array.

  2. Multidimentional array

    In Java, when data or elements are stored in row and column, based on index, which is called as a a multidimentional array.

    Syntax

    dataType[][] arrayRefVar; or

    dataType [][]arrayRefVar; or

    dataType arrayRefVar[][]; or

    dataType []arrayRefVar[];

    Instantiation

    int[][] arr=new int[3][3];//3 row and 3 column

    Let’s see an example, given below.

    Code
    1. public class JavaArray {  
    2.         public static void main(String args[]) {  
    3. //declaring and initializing 2D array    
    4.               int arr[][] = {{357}, {785}, {647}};  
    5.               for (int a = 0; a < 3; a++) {  
    6.                     for (int b = 0; b < 3; b++) {  
    7.                           System.out.print(arr[a][b] + " ");  
    8.              }  
    9.             System.out.println();  
    10.         }  
    11.     }  
    12. }  
    24

    Output

    25

    In the example, shown above, first we declare, instantiate, initialize and print the multidimentional array output.
Passing Array

In Java, we can pass an array to the method. Therefore, we can reuse the same logic on any array.
 
Let’s see an example, given below.
 
Code
  1. public class JavaArray {  
  2.     static void minElement(int arr[]) {  
  3.         int minElement = arr[0];  
  4.         for (int i = 1; i < arr.length; i++) {  
  5.             if (minElement > arr[i]) {  
  6.                 minElement = arr[i];  
  7.             }  
  8.         }  
  9.        System.out.println(minElement);  
  10. }  
  11.  public static void main(String args[]) {  
  12.       int a[] = {15394056};  
  13.         minElement(a);//passing array to method    
  14.     }  
  15. }  
  16.    
26

Output

27

In the example, mentioned above, we get minimum number of an array passing to method.
 
Class name of Java array

Array is an object in Java. For array object, a proxy class is created, whose name can be obtained with the help of getClass().getName() method on the object.
 
Let’s see an example, given below.

Code
  1. public class JavaArray {  
  2.        public static void main(String args[]) {  
  3.              int arr[] = {375};  
  4.   
  5.             Class c1 = arr.getClass();  
  6.             String name = c1.getName();  
  7.                    System.out.println(name);  
  8.      }  
  9. }  
1

Output

2

Copy an Array

We can copy an array to another with the use of arraycopy() method of System class.
 
Syntax

public static void arraycopy(
Object src, int srcPos,Object dest, int destPos, int length
)
 
Let’s see an example, given below.
 
Code
  1. public class JavaArray {  
  2. public static void main(String[] args) {  
  3.         char[] copyFrom = {'j''a''v''a''i''s''a''c''o''f''f''e''e'};  
  4.         char[] copyTo = new char[6];  
  5.         System.arraycopy(copyFrom, 7, copyTo, 06);  
  6.         System.out.println(new String(copyTo));  
  7.     }  
  8. }  
3

Output

5

Addition of two matrices

Let’s see an example, given below.
 
Code
  1. public class JavaArray {  
  2.      public static void main(String args[]) {  
  3.         //creating two matrices    
  4.         int x[][] = {{164}, {745}};  
  5.         int y[][] = {{294}, {384}};  
  6.       //matrix to store the sum of two matrices    
  7.         int z[][] = new int[2][3];  
  8.         for (int i = 0; i < 2; i++) {  
  9.             for (int j = 0; j < 3; j++) {  
  10.                 z[i][j] = x[i][j] + y[i][j];  
  11.                 System.out.print(z[i][j] + " ");  
  12.             }  
  13.             System.out.println();//new line    
  14.         }  
  15.     }  
  16. }  
  17.    
6
Output

7

In the example, mentioned above, add two matrices.
 
Advantages of Java Array
  • Code Optimization

  • Random access
Disadvantage of Java Array
  • Size Limit
Summary

Thus, we learnt that Java array is a data structure, where we store similar types of the elements and also learn their types, passing an array and copy an array in Java.