Arrays length

Array size, arrayName.length, holds its length. The following code outputs the length of each array by using its length property.


public class Main {
  public static void main(String args[]) {
    int a1[] = new int[10];
    int a2[] = {1, 2, 3, 4, 5};
    int a3[] = {4, 3, 2, 1};
    System.out.println("length of a1 is " + a1.length);
    System.out.println("length of a2 is " + a2.length);
    System.out.println("length of a3 is " + a3.length);
  }
}

This program displays the following output:


length of a1 is 10
length of a2 is 5
length of a3 is 4
Home 
  Java Book 
    Language Basics  

Array:
  1. Introduction to Arrays
  2. Arrays can be initialized when they are declared
  3. Multidimensional Arrays
  4. Arrays length
  5. Calculate with Array