Array Length In Java | Java Array Examples | Edureka

An array in Java can contain multiple elements, depending on how the object was created. For the user to perform distinct operations, it is essential to know the length of the array. This article on ‘Array Length in Java‘ aims at familiarizing us with the operations used to get the length of the array as well as its usage.

This article focuses on following pointers:

So let us get started with this ‘Array Length in Java Article’ then,

Array Length Attribute:

How do you find the length of an array?

In order to obtain the Java Array Length, we need to use the ‘array length attribute’, as shown in the example below:


/**
* An Example to get the Array Length is Java
*/
public class ArrayLengthJava {
public static void main(String[] args) {
String[] myArray = { "I", "Love", "Music" };
int arrayLength = myArray.length; //array length attribute
System.out.println("The length of the array is: " + arrayLength);
}
}

 

Output

The length of the array is: 3

It must be noted, that Java Array Object does not have a method to get its length.

Often times, we are unaware of how the array object was created. For such programs, we use a function that receives an array, and prints the length.


/**
* An Example to find the Java Array Length using a function
*/
public class ArrayLengthJava {
private static void printArrayLength(String[] myArray) {
if (myArray == null) //to check whether the array is empty or not
{
System.out.println("The length of the array can't be determined.");
} else {
int arrayLength = myArray.length;
System.out.println("The length of the array is: " + arrayLength);
}
}
public static void main(String[] args) {
String[] JavaArray1 = { "I", "Love", "Music" };
String[] JavaArray2 = { "R", "S" };
String[] JavaArray3 = { "1", "2", "3", "4" };
String[] JavaArray4 = { "Java" };
printArrayLength(null);
printArrayLength(JavaArray1);
printArrayLength(JavaArray2);
printArrayLength(JavaArray3);
printArrayLength(JavaArray4);
}
}

Output:

  • The length of the array can’t be determined.

  • The length of the array is: 3

  • The length of the array is: 2

  • The length of the array is: 4

  • The length of the array is: 1

It must be noted that on accessing the length field of an empty or a null object, a NullPointerException is raised.

Searching for a value using Array Length in Java

The array length has many useful properties, that can be used while programming. In the following example, we use the length of the array to loop through all the elements and to determine whether the specific value is present.


/**
* An Example that uses Java Array Length to check if the array contains a
* specific value.
*/
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", "Love", "Music" };
System.out.println(arrayContainsValue(JavaArray, "Love"));
System.out.println(arrayContainsValue(JavaArray, "Guitar"));
}
}

Output:

  • true

  • false

The program given above outputs the value as true, as “Love” is present in the array, whereas “Guitar” is a non-existential element, hence the output is false.

Searching for the lowest value in Array

We can use the length of an array to fetch the lowest value present 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, 10 };
System.out.println("The min value in the array: "+minValue(JavaArray));
}
}

Output:

The min value in the array: 10

Searching for the highest value in Array

Furthermore , we can use the length of an array to fetch 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, 69, 10 };
System.out.println("The max value in the array: "+maxValue(JavaArray));
}
}

Output:

The max value in the array: 69

On that account, we can conclude that the array length attribute is a much versatile attribute.

Thus we have come to an end of this article on ‘Array Length in Java’. If you wish to learn more, check out the Java Course by Edureka, a trusted online learning company. Edureka’s Java J2EE and SOA training and certification course is designed to train you for both core and advanced Java concepts along with various Java frameworks like Hibernate & Spring.

Got a question for us? Please mention it in the comments section of this blog  and we will get back to you as soon as possible or join our Java Training in Cambridge.