How to Find Array Length in Java with Examples

In this blog post, we are going to learn an important topic pertaining to Java i.e Array length.

Java is a high-end programming language by accompanying robustness, security, and greater performance.

How to Find Array Length in Java?

An array length in Java represents a series of elements that an array could really hold. There really is no predetermined method for determining an object’s length. In Java, developers can discover its array length with the help of array attribute length. One such attribute is used in conjunction with array names. To gain deeper insights into this programming language, java training is compulsory. Therefore in this blog post, we’ll look at how to calculate the size as well as the length of the arrays in Java.

Length Attribute in Java

The length attribute throughout Java represents the size of the array. Each array has a length property, for which the value seems to be the array’s size. The overall quantity of elements that the array can encompass is denoted by its size. In order to access the length property, use dot (.) operator which is accompanied by an array name. Also we can determine the length of int[ ]double[] and  String[]. As an example:

int[] arr=new int[5];  
int arrayLength=arr.length

In the above sample code, arr represents the array type and it is the int with a capacity of 5 elements. In simple terms array Length perhaps is the variable that keeps track of the length of an array. An array name (arr) is accompanied by a dot operator as well as the length attribute to determine the length of the array. It defines the array’s size.

Array length = Array’s last Index+1

Array Length in Java with Examples

Array last index = 7

Array length = 7+1 = 8

It is important to note that such length of the array defines the upper limit number of the elements that can hold or its capacity. This does not include the elements which are added to the array. It is, length comes back the array’s total size. The length and size of arrays for whom the elements have been configured just at the time of their establishment were the same.

However, if we’re talking well about an array’s logical size or index, after which merely int arrayLength=arr.length-1, since an array index begins at 0. As a result, the logical, as well as an array index, was always one less than the real size.

First Index:

0 1 2 3 4 5 6 7 8 9

In the above picture, 0 is the first index and the array length will be 10.

Now we will explore how to fetch the length of an array using an example.

Arraylengthex1.java

public class ArrayLengthExample1
{
    public static void main(String[] args)
    {
        //defining an array of type int named num
        //the square bracket contain the length of an array
        int[] num = new int[11];
        
        //length is an Array attribute that determines the array length
        int arrayLength=num.length;
        
        //prints array length
        System.out.println("length of an array is "+ arrayLength);
    }
}

Output:

length of an array is 11

Arraylenghtex2.java

public class ArrayLengthExample2 {
    public static void main(String[] args) {
        //initializing an array of type String named country
        String[] country = { "cat", "dog", "pig", "mouse", };
        
        //length is an Array attribute that determines the array length
        int arrayLength=country.length;
        
        //prints array length
        System.out.println("Size of an array is: " + arrayLength);
        
    }
}

Output:

Size of an array is 4

ArraylengthEx3.java

public class ArrayLengthEx3 {
    private static void LengthOfArray(String[] array) {
        //checks array is empty or not
        if (array == null)
        {
            //if the array is empty prints the following statement
            System.out.println("an array length cannot be empty.");
        }
        else {
            //length attribute of the Array class determines the length of an array
            int arrayLength = array.length;
            //prints the array length
            System.out.println("An array length is: "+arrayLength);
        }
    }
    
    public static void main(String[] args) {
        String[] fruits = { "Banana", "Apple", "Melon", };
        String[] alphabets = { "k", "t", "l", "m", };
        String[] numbers = { "25", "63", "84", "90", "11", };
        
        //passing null value to the function
        LengthOfArray(null);
        
        //passing fruits array to the function
        LengthOfArray(fruits);
        
        //passing alphabets array to the functiona
        LengthOfArray(alphabets);
        
        //passing numbers array to the function
        LengthOfArray(numbers);
    }
}

Output:

an array length cannot be empty.
An array length is: 3
An array length is: 4
An array length is: 5

Searching a Value Using the Array Length in Java

The array length does have a lot of good properties which can be used in coding. In the following scenario, we just use the length of an array to loop through all elements and see if the specified value is visible.

Example:

public class ArrayLengthJava {
    private static boolean arrayContainsValue(String[] myArray,
    String lookForValue) {
        if (myArray != null) {
            int arrayLength = myArray.length;
            
            for (int i = 0; i <= arrayLength - 1; i++) {
                String value = myArray[i];
                if (value.equals(lookForValue)) {
                    return true;
                }
            }
        }
        
        return false;
    }
    
    public static void main(String[] args) {
        String[] JavaArray = { "I", "hate", "sweets"};

        System.out.println(arrayContainsValue(JavaArray, "hate"));
        System.out.println(arrayContainsValue(JavaArray, "love"));
    }
}

Output:

true
false

The program above returns true because “hate” is present in the array, but “love” is a non-existent element, so the result is false.

Search for the Lowest Value in the Array

The length of an array can be used to find the lowest value in an array object.

public class ArrayLengthJava {
    private static int minValue(int[] myArray) {
        int minValue = myArray[0];
        int arrayLength = myArray.length;
        
        for (int i = 1; i <= arrayLength - 1; i++) {
            int value = myArray[i];
            
            if (value < minValue) {
                minValue = value;
            }
        }
        
        return minValue;
    }
    
    public static void main(String[] args) {
        int[] JavaArray = { 28, 46, 69, 50 };
        System.out.println("The min value in the array is: "+minValue(JavaArray));
    }
}

Output:

The minimum value in an array is: 28

Search for the Max Value in an Array

Moreover, we could use an array’s length to find the highest value in an array object.

public class ArrayLengthJava {
    private static int maxValue(int[] myArray) {
        int maxValue = myArray[0];
        int arrayLength = myArray.length;
        
        for (int i = 1; i <= arrayLength - 1; i++) {
            int value = myArray[i];
            if (value > maxValue) {
                maxValue = value;
            }
        }
        
        return maxValue;
    }
    
    public static void main(String[] args) {
        int[] JavaArray = { 29, 46, 85, 69 };
        System.out.println("The max value in an array is: "+maxValue(JavaArray));
    }
}

Output:

The max value in an array is: 85

Conclusion

Hope you have got enough knowledge in handling array length in java. If you have any queries please drop them in the comments section to get them clarified.

Happy Coding!

Author Bio:

I am VarshaDutta Dusa, Working as a Senior Digital Marketing professional & Content writer in HKR Trainings. I Have good experience in handling technical content writing and aspire to learn new things to grow professionally. I am expertise in delivering content on the market demanding technologies like mulesoft Training, Dell Boomi Tutorial, Elasticsearch Course, Fortinet Course, postgresql Training, splunk, Success Factor, Denodo, etc.

Leave a Comment

Your email address will not be published. Required fields are marked *