/** * A Simple Example to get the Java array length. */ public class JavaArrayLengthTest { public static void main(String[] args) { String[] testArray = { "Apple", "Banana", "Carrots" }; int arrayLength = testArray.length; System.out.println("The length of the array is: " + arrayLength); } }
Since the array has three elements, the expected output of the program will be:
The length of the array is: 3
From the code sample, the array length is retrieved from the array length attribute:
int arrayLength = testArray.length;
But often in our code, we need to process an array where we are not sure how the array object was created. Here is a sample code where we write a function that receives an array and print the length of it.
/** * A Simple Example that uses a function to print the Java array length. */ public class JavaArrayLengthTest { private static void printArrayLength(String[] anArray) { if (anArray == null) { System.out.println("The length of the array can't be determined."); } else { int arrayLength = anArray.length; System.out.println("The length of the array is: " + arrayLength); } } public static void main(String[] args) { String[] testArray1 = { "Apple", "Banana", "Carrots" }; String[] testArray2 = { "A", "B" }; String[] testArray3 = { "1", "2", "3", "4" }; String[] testArray4 = { "Test" }; printArrayLength(null); printArrayLength(testArray1); printArrayLength(testArray2); printArrayLength(testArray3); printArrayLength(testArray4); } }
Notice that we check first if the array is null or not. This is because if we access the length field of a null object, a NullPointerException will be raised. Below is the result of the code when executed:
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
/** * A Simple Example that loops through all the values of an array using the * length field. */ public class JavaArrayLengthTest { private static void printArrayValues(String[] anArray) { if (anArray == null) { System.out.println("The array has no elements."); } else { int arrayLength = anArray.length; for (int i = 0; i <= arrayLength - 1; i++) { String value = anArray[i]; System.out.println("The array contains the value: " + value); } } } public static void main(String[] args) { String[] testArray = { "Apple", "Banana", "Carrots" }; printArrayValues(null); printArrayValues(testArray); } }
Since an array is 0 based, the index of the items range from 0 upto "arrayLength - 1", instead of 1 to arrayLength. Below is the result when the code is executed:
The array has no elements. The array contains the value: Apple The array contains the value: Banana The array contains the value: Carrots
Below is another example of how useful an Array's length is. The code checks if an array contains a specific value. The length of the Java array is used to loop through all the items and check if the value is the same.
/** * A Simple Example that uses Java Array Length to check if an array contains a * specific value. */ public class JavaArrayLengthTest { private static boolean arrayContainsValue(String[] anArray, String valueToLookFor) { if (anArray != null) { int arrayLength = anArray.length; for (int i = 0; i <= arrayLength - 1; i++) { String value = anArray[i]; if (value.equals(valueToLookFor)) { return true; } } } return false; } public static void main(String[] args) { String[] testArray = { "Apple", "Banana", "Carrots" }; System.out.println(arrayContainsValue(testArray, "Banana")); System.out.println(arrayContainsValue(testArray, "Grapes")); } }
Since "Banana" is in the array, the first statement is true. While the second statement is false because "Grapes" is not in the array. Below is the result when the code is run:
true false
Here is another example on how to use the length of an array to retrieve the lowest value in all the items of an array object:
/** * A Simple Example that uses Java Array Length to determine the lowest value in * an array. */ public class JavaArrayLengthTest { private static int minValue(int[] anArray) { int minValue = anArray[0]; int arrayLength = anArray.length; for (int i = 1; i <= arrayLength - 1; i++) { int value = anArray[i]; if (value < minValue) { minValue = value; } } return minValue; } public static void main(String[] args) { int[] testArray = { 100, 50, 20, 99 }; System.out.println("The min value is: "+minValue(testArray)); } }
The code assumes that the array is not null and there is at least one element. The result of the code is shown below:
The min value is: 20
Similarly, here is another example on how to use the length of an array to retrieve the highest value in all the items of an array object:
/** * A Simple Example that uses Java Array Length to determine the highest value in * an array. */ public class JavaArrayLengthTest { private static int maxValue(int[] anArray) { int maxValue = anArray[0]; int arrayLength = anArray.length; for (int i = 1; i <= arrayLength - 1; i++) { int value = anArray[i]; if (value > maxValue) { maxValue = value; } } return maxValue; } public static void main(String[] args) { int[] testArray = { 100, 50, 20, 99 }; System.out.println("The max value is: "+maxValue(testArray)); } }
Below is the rendered result:
The max value is: 100