How to find Array Length in Java

[ad_1]

java array length

  1. Java Array Length: How do you find the length of an array?
  2. Searching a value using Array Length in Java
  3. Searching for the lowest value in the array
  4. Searching for the highest value in the array
  5. Frequently Asked Questions
  6. Conclusion

Array Length Length: How do you find the length of an array?

The length attribute of Java holds the number of elements in the array. In Java, there is no predefined method by which you can find the length of an array. But you can use the length attribute to find the length of an array. When we declare an array, the total number of elements in it is called the array’s length, or we can also call it the size of the array. Let us see the below example for a better understanding:

int len = thisArray.length;

Let us see a program using the Array Length attribute of Java:

import java.util.*;
class Main
{
    public static void main(String[] args)
    {
        Integer[] intArray = {1,3,5,7}; //integer array
        String[] strArray = { "one", "two", "three", “four” }; //string array
 
                //print each array and their corresponding length
        System.out.println("Contents of Integer Array : " + Arrays.toString(intArray));
        System.out.println("The length of the array is : " + intArray.length);
 
        System.out.println("Contents of String array : " + Arrays.toString(strArray));
        System.out.println("The length of the String array is : " + strArray.length);
    }
}

OUTPUT:

Contents of Integer Array: [1,2,5,7]

The length of the array is: 4

Contents of String array: [one, two, three, four]

The length of the String array is: 4

In the above program, the length function counts the total number of elements in the array, whether a string or a number. The length attribute takes the length of the array. There can be different scenarios where you can use the Array Length attribute, such as:

  • To search for a minimum value in the array.
  • To find the maximum number in an array.
  • To get any specific value from the array. 
  • To check if any specific value is there in the array or not.

There can be more scenarios where you can use the Array Length attribute in Java. 

In this article, we will see more scenarios of using the Array Length attribute of Java with some useful programs and examples. 

Searching a value using Array Length in Java

The Array Length attribute can be used for several cases, so it is essential. It can also be used for searching for a value in an array. You need to use a loop that will iterate through all the elements in the array one after the other until it finds the searched element. 

When the code runs, the loop will start searching the element in the array until it reaches the last element or traverses the complete length of the array. When it is traversing through each element in the array, it compares the value of each element to the value to be searched, and if the value of the element is matched, then it stops the loop.

The program below does the same we just discussed, and it will help you to understand better how the code will work:

import java.util.*;
class Main{
public static void main(String[] args) {
    String[] strArray = { “HTML”, “CSS”, "Java", "Python", "C++", "Scala", }; //array of strings
                //search for a string using searchValue function
    System.out.println(searchValue(strArray, "C++")?" value C++ found":"value C++ not found");
    System.out.println(searchValue(strArray, "Python")?"value Python found":"value Python not found");
}
 
private static boolean findValue(String[] searchArray, String lookup)
{
    if (searchArray != null) {
        int arrayLength = searchArray.length; //computes the array length
        for (int i = 0; i <= arrayLength - 1; i++)
        {
            String value = searchArray[i]; //searching for value using for loop
            if (value.equals(lookup)) {
               return true;
            }
        }
    }
return false;
}

Value C++ found

Value Python found

In the above program, as you can see, we have an array that contains the names of some programming languages. There’s a function with the name ‘findValue’ that searches for the specific value we are trying to find in the array. We used the for loop that traverses through each element in the array and finds it the value exists in our array or not. We gave two statements for both the cases, such as if the value is in the array or not. What it does is it returns true if the value is found in the array and false otherwise. 

In our case, the values C++ and Python were there in our array and that’s the reason it returned true or the statements that we provided as true. 

Searching for the lowest value in the array

As we have seen, how the Array length attribute works and how it makes our code easier to find the value we are searching for. In this section, we will see how we can find the minimum value in an array. 

The below program is used to find the lowest value in an array:

import java.util.*;
class Main {
    public static void main(String[] args) {
       int[] intArray = { 2,40,11,10,3,44 }; //int array
       System.out.println("The given array:" + Arrays.toString(intArray));
       int min_Val = intArray[0]; // we are assigning first element to min value
       int length = intArray.length;
       for (int i = 1; i <= length - 1; i++) // it goes till end of array, compares with each element and finds the minimum value
        {
            int value = intArray[i];
            if (value <min_Val) {
               min_Val = value;
            }
        }
 
    System.out.println("The lowest value in the array: "+min_Val);
    }
}

OUTPUT:

The given array: [2, 40, 11, 10, 3, 44]

The lowest value in the array: 2

We gave the first element as the lowest element in the array in the above program. But still, it assigned the first element 2 as the minimum value and compared it with other elements in the array. When it is found that the value 2 is the only minimum, it comes out from the loop and returns the min value from the array. 

Suppose we have provided the minimum value at some other place in the array. In that case, it will assign each value in the array as a minimum value and compare it with other elements until it finds the correct one. This way, we can find the minimum value in the array.

Searching for the highest value in the array

In the above section, we saw how to find the minimum value in an array. The same logic will be applied in this example, where we search for the maximum value in an array. The only thing that will change is only the smaller than (<) sign. 

Let us see the example below:

import java.util.*;
class Main {
   public static void main(String[] args) {
        int[] intArray = { 2,40,1,10,95,24 }; //int array
        System.out.println("The given array:" + Arrays.toString(intArray));
        int max_Val = intArray[0]; //reference element as maximum value
        int length = intArray.length;
        for (int i = 1; i <= length - 1; i++) // It finds the maximum value of the element by comparing others to reference
        {
        int value = intArray[i];
        if (value >max_Val) {
             max_Val = value;
            }
        }
       System.out.println("The largest value in the array: "+max_Val);
    }
}

OUTPUT:

The given array: [2,40,1, 10,95,24]

The largest value in the array: 95

The above code did the same as it did to find the smallest value in the array. The thing that is changed is the condition for comparing with another element. We compared the smallest elements, and now we used to find the largest ones. 

Frequently Asked Questions

Q. What is the difference between the Size of ArrayList and the length of an Array?

A. In the ArrayList attribute, there is no length property, but it still gives the length by using the size() method, whereas the length property of the array gives the total number of elements in the array or the length of the array. 

Q. Are length and length() same in Java?

A. Length() is a method used for the string objects to return the number of characters in a string where ‘length’ is the property to find the total number of elements in an array, and it returns the size of the array. 

Q. How to find the largest number in an array using the array length attribute in Java?

A. To find the largest number or value in an array, we need to create a loop that will traverse through each element in the array and compares each element by assigning the first value of the array as the maximum. To understand better, go to the section ‘Searching for the highest value in the array’ of this article, where we discussed an example. 

Q. How to get the length in Java?

A. There are different scenarios to find the length. For example, we have two options to use the length() method or the java length attribute. Both of them are used for different purposes and return different values. So, if you want to find the length of characters in a string, you need to use the length() method, whereas if you want to find the length or size of an array, you must use the length attribute of Java. 

Q. What do you mean by length function in Java?

A. The length() function in Java is used to get the number of characters in a string. 

Conclusion

Thus, we have come to the end of this article where we discussed the Java Array Length attribute, which is very useful in different programming scenarios. We also discussed specific cases, such as finding the largest and smallest value in an array. However, there are more uses for this attribute. So, we encourage you to find more cases and try to use this length attribute. 

[ad_2]

Leave a Comment