How to Find Size or Length of Array in Java

How to find the size or length of an array in Java? How to find the size or length of the multidimensional array in Java?

The length is an in-built property of the array variable, which stores the information about the number of elements in the array. 

Whenever we initialize an array then by default length property is assigned to the array and we can access them through arrayVariable.length

Java program to display size or length of array in Java

public class ArrayLength {
  public static void main(String[] args) {
      
    // declare and initialize an array
    int arr[] = {10, 20, 30, 40, 50};
      
    // display array length
    System.out.print("The length of the given array = ");
      System.out.println(arr.length);
  }
}

Output:-

The length of the given array = 5

Using the length property we can iterate through the array,

class TestArray{
  public static void main(String[] args) {

    // defining array
    int[] a = {10,20,30,40,50};

    // display size of array
    System.out.println("Size = "+ a.length);

    // display array using length property
    System.out.println("Array elements:");
    for(int i=0; i < a.length; i++){
      System.out.print(a[i]+"\t");
    }

  }
}

Output:-

Size = 5
Array elements:
10 20 30 40 50

How to Find the length of Multidimensional Array in Java

Java Multi-dimensional arrays have multiple length property, each for every dimension basis. For example, if we have a two-dimensional array “arr” then we can find their length as arr.length, and arr[index].length

public class ArrayLength {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int arr[][] = {{50,60},{70,80},{90,100}};
    
    // display array length
    System.out.print("The length of the given array = ");
    System.out.println(arr.length);
    System.out.println("arr[0] length = " + arr[0].length);
    System.out.println("arr[1] length = " + arr[0].length);
    System.out.println("arr[2] length = " + arr[0].length);
  }
}

Output:-

The length of the given array = 3
arr[0] length = 2
arr[1] length = 2
arr[2] length = 2

It can be also displayed as,

// display array length
System.out.print("The length of the given array = ");
System.out.println(arr.length);
for(int i=0; i<arr.length; i++) {
  System.out.println("arr["+ i +"] length = " + arr[0].length);
}

The above length property can be used to manipulate the multi-dimensional array-like to display the multi-dimensional array. 

// display 2D array using for loop
for(int i=0; i<arr.length; i++) {
  for(int j=0; j<arr[i].length; j++) {
    System.out.print(arr[i][j] + " ");
  }
}

Java program to demonstrate these points,

class TestArray{

  public static void main(String[] args) {

    // 2d array
    int[][] a = {{10,20},{30,40},{50,60}};

    // display size of array
    System.out.println("2D array size = "+ a.length);
    System.out.println("First row size = "+ a[0].length);
    System.out.println("Second row size = "+ a[1].length);

    // display array using length property
    System.out.println("Array elements:");
    for(int i=0; i < a.length; i++){
      for (int j=0; j < a[i].length; j++) {
        System.out.print(a[i][j]+"\t");
      }
      System.out.println();
    }

  }
}

Output:-

2D array size = 3
First row size = 2
Second row size = 2
Array elements:
10 20
30 40
50 60

Size of Jagged Array in Java

Jagged Array in Java:- A multi-dimensional array with different sizes child array is called Jagged array. It creates a table with different sizes of columns in a row. 

class TestArray{

  public static void main(String[] args) {
    int[][] a = {{10,20},{30},{50,60,70},{80,90}};

    // display size of array
    System.out.println("Array size = "+ a.length);
    System.out.println("First row size = "+ a[0].length);
    System.out.println("Second row size = "+ a[1].length);

    // display array using length property
    System.out.println("Array elements:");
    for(int i=0; i < a.length; i++){
      for (int j=0; j < a[i].length; j++) {
        System.out.print(a[i][j]+"\t");
      }
      System.out.println();
    }

  }
}

Output:-

Array size = 4
First row size = 2
Second row size = 1
Array elements:
10 20
30
50 60 70
80 90

Size of Three Dimensional Java Array

In the three dimensional array there will be three properties:- arr.length, arr[index].length, arr[index-1][index-2].length. Below program shows it,

public class ArrayLength {
  public static void main(String[] args) {
    
    // declare and initialize an array
    int[][][] arr = { {{1,2},{3,4},{5,6}}, {{7,8},{9,1},{2,3}} };
    
    // display array length
    System.out.print("The length of the given array = ");
    System.out.println(arr.length);
    System.out.println("arr[0] length = " + arr[0].length);
    System.out.println("arr[0][0] length = " + arr[0][0].length);
    System.out.println("arr[0][1] length = " + arr[0][1].length);
    System.out.println("arr[0][2] length = " + arr[0][2].length);
  }
}

Output:-

The length of the given array = 2
arr[0] length = 3
arr[0][0] length = 2
arr[0][1] length = 2
arr[0][2] length = 2

We can use these properties to display the three dimensional array,

// displaying three dimension array in Java 
// using for loop and length property
for(int i=0; i < arr.length; i++){
  for(int j=0; j < arr[i].length; j++){
   for(int k=0; k < arr[i][j].length; k++){
    System.out.println( arr[i][j][k] );
   }
  }
}

Output:-

1 2 3 4 5 6 7 8 9 1 2 3 

If you enjoyed this post, share it with your friends. Do you want to share more information about the topic discussed above or you find anything incorrect? Let us know in the comments. Thank you!

Leave a Reply