Java

How to Get the Length of an Array in Java

In programming, array length can be used in various ways. In Java an array is a data structure that is used to hold multiple elements of the same data type, to manage and to perform different operations on arrays it is crucial to know the length of an array. In this article, we will teach you how you can get the array length property and how to use it in Java programming:

Getting array length in Java

First, we will create a class by the name of “ALength”:

public class ALength

Now in the main function we have taken two arrays one is with a variable type integer and the second is with a string:

int[] numbers = {11,34,78,98,12,92};
String[] cities = {"NewYork City", "Washington", "San Antonio", "California"};

To display the length of both of these arrays you need to type:

System.out.println("The length of numbers: "+numbers.length+"\n");
System.out.println("The length of cities: "+cities.length);

So the full code that we have written is shown below

public class ALength {
    public static void main(String[] args) {
        int[] numbers = {11,34,78,98,12,92};
        String[] cities = {"NewYork City", "Washington", "San Antonio", "California"};
        System.out.println("The length of numbers: "+numbers.length+"\n");
        System.out.println("The length of cities: "+cities.length);
    }
}

Note: You need to install the java development kit (JDK) to execute the java based programs in Linux OS by typing

$ sudo apt install default-jdk

After that, you need to open any text editor and create a Java file by the name of “ALength.java”:

$ nano ALength.java

Now you need to compile the code first before executing it:

$ javac ALength.java
$ java ALength

Now we have taken another example in which we have used a scanner class to take input from users:

Scanner in=new Scanner(System.in);

Now after that, we have created a public class with the name of ArrayLength:

public class ArrayLength

After defining the main function, for this example, we are going to initialize an array with 5 elements and store those values into the variable with a name of ‘A’:

int[] A=new int[5]; //An array of 5 elements

Now we are going to take the input for each array element from the user using a for loop:

for(int i=0;i<A.length;i++)
{
    System.out.println("Enter element # " + (i+1));
    A[i]=in.nextInt();
}

So this loop will start from the array element 0 and then terminate till it reaches the length of an array ‘A’ by using an A.length property (equals to 5) and at the end, A[i] will store the input values taken from the user. After that, we are going to display the value of each array element:

System.out.println();
for(int i=0;i<A.length;i++)
{
    System.out.println("A["+i+"]="+A[i]);
}

So the complete code for getting the length of an array would be:

import java.util.Scanner;
public class ArrayLength {
    public static void main(String[] args) {
        //Define a Scanner object for data input.
        Scanner in=new Scanner(System.in);
        //Declare  an array.of 10 integers
        int[] A=new int[5]; //An array of 5 elements
        //Use a for loop to accept values from the user for each element of the array.
        for(int i=0;i<A.length;i++) //Java arrays start from zero index.
        {
            //Accept value of each element i from the user
            System.out.println("Enter element # " + (i+1));
            A[i]=in.nextInt();
        }
        System.out.println();
        //Display elements of array from 0 to length of array minus 1.
        for(int i=0;i<A.length;i++)
        {
            System.out.println("A["+i+"]="+A[i]);
        }
   }    
}

After that, create the java file for the above code using any text editor:

$ nano ArrayLength.java

Now you need to write the above code in the text editor and then save it with the name of class “ArrayLength.java”:

You will get the output after compiling as shown below:

$ javac ArrayLength.java
$ java ArrayLength

Conclusion

Array length is a very handy property in Java programming to perform distinct functions on arrays. In this article, we have learned how to acquire the length of an array and how to use it in a Java program. Two examples are provided, first to simply print the array length of user defined arrays and second getting 5 elements from the user and using array length property in for loop.

About the author

Taimoor Mohsin