JavaDevNotes.com

Java Array Size

Programmers usually want to use arrays because it can hold arbitrary number of items, depending on how we created the array object. Most of the things we wish to do with arrays, like searching or sorting, needs us to know it's size. Because we usually manipulate arrays using loop, we need to know the size of an array to know how many times we need to loop. We will show how to find Java Array Size and give some examples on how to use it.

Java Array Size

In Java, Array Objects does not use a method to determine it's size. The right way to do it is documented in the Java Specification. It mentions that a Java Array has a property declared as
public final length;
Since we can't change the size of an array when created, the value of this property does not change in the lifetime of the array object. Here is a simple example on how to use a Java Array's Size.:
/**
 * A Simple Example to get the Java array size.
 */
public class JavaArraySizeTest {
   public static void main(String[] args) {
      String[] testArray = { "Basketball", "Football", "Volleyball", "Cue Ball" };
      int arrayLength = testArray.length;
      System.out.println("The size of the array is: " + arrayLength);
   }
}

Since the array has four elements, the expected output of the program will be:

The length of the array is: 4

Using the code example from above, we know that the array size is retrieved from the array length attribute:

int arrayLength = testArray.length;

We usually code where we pass arrays as parameters to functions. Hence from the perspective of a function, we don't know how the array was created. So we rely on the length property to determine the size:
/**
 * A Simple Example that uses arrays and functions.
 */
public class JavaArraySizeTest {
   private static void printArraySize(String[] anArray) {
      if (anArray == null) {
         System.out.println("Array is null and has no size.");
      } else {
         int arraySize = anArray.length;
         System.out.println("The size of the array is: " + arraySize);
      }
   }
   public static void main(String[] args) {
      String[] testArray1 = { "Cute", "Good", "Great", "Nice" };
      String[] testArray2 = { "3", "4", "7" };
      String[] testArray3 = { "A", "B", "C", "D", "E" };
      String[] testArray4 = { "Magick" };
      printArraySize(null);
      printArraySize(testArray1);
      printArraySize(testArray2);
      printArraySize(testArray3);
      printArraySize(testArray4);
   }
}

It is a good defensive programming practice to always check if an object parameter is null or not. So that we could handle it gracefully rather than wait for an Exception to be thrown.Below is the result of the code when executed:

The length of the array can't be determined.
The length of the array is: 4
The length of the array is: 3
The length of the array is: 5
The length of the array is: 1

Display Array Values

Here is an example on how we can display all items in an array if we know the size of an array in Java:
/**
 * A Simple Example displays all array values using array size
 */
public class JavaArraySizeTest {
   public static void main(String[] args) {
      String[] testArray = { "Pie", "Pasta", "Cake", "Wine" };
      int arrayLength = testArray.length;
      for (int i = 0; i <= testArray - 1; i++) {
         String value = testArray[i];
         System.out.println("The array contains the value: " + value);
      }
   }
}

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 contains the value: Pie
The array contains the value: Pasta
The array contains the value: Cake
The array contains the value: Wine

Get Min Value in Array

Here is another example of how to use size of an array to get the smallest or minimum item in an aray:

/**
 * A Simple Example that gets lowest value in an array
 */
public class JavaArraySizeTest {
   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 = { 10, 25, 18, 12, 30, 45, 11 };
      System.out.println("The min value is: "+minValue(testArray));
   }
}

Below is the result of the code:

The min value is: 10

Get Max Value in Array

The opposite of min is max. Here is another example of how to use size of an array to get the largest or maximum item in an aray:

/**
 * A Simple Example that gets lowest value in an array
 */
public class JavaArraySizeTest {
   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 = { 10, 25, 18, 12, 30, 45, 11 };
      System.out.println("The max value is: "+maxValue(testArray));
   }
}

Below is the result of the code:

The max value is: 10