Java printArray: Usage & Examples

In Java, the printArray method is used to print the elements of an array. It is a static method in the java.util.Arrays class and can be called directly by the class name.

There are two overloaded versions of the printArray method.

  1. output the array of Objects
  2. Output the array of integers.
  3. integer

Here is an example using the printArray method:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        Integer[] numbers = {1, 2, 3, 4, 5};
        System.out.print("Numbers: ");
        Arrays.printArray(numbers);
        
        int[] grades = {85, 90, 75, 95, 80};
        System.out.print("Grades: ");
        Arrays.printArray(grades);
    }
}

Output results:

Numbers: 1 2 3 4 5
Grades: 85 90 75 95 80

Note: the printArray method can only print elements of a one-dimensional array. If you need to print elements of a multi-dimensional array, you will need to use a different method or loop through the array.

bannerAds