How to Print Arrays in Java

When working with arrays in Java, it is useful to print the array contents on the console or on the log file. This is useful for debugging the application. It is possible to print one dimensional or multi-dimensional arrays using for loops. However a much easier solution is to use the deepToString() static method of Arrays class. The following sample program in Java shows how any type of arrays can be printed using the Arrays class of util package.

import java.util.Arrays;

/**
 * How to print arrays in Java. This works with multi-dimensional arrays as well. 
 */
public class PrintArray {
    public static String[] LANGUAGES = new String[] {
                    "English","Spanish","German"
                    };
    
    public static String[][] COUNTRIES = new String[][] {
                     {"India","New Delhi"},
                     {"United States","Washington"},
                     {"Germany","Berlin"},
                    };
    
    public static void main(String[] args) {
        System.out.println(Arrays.deepToString(LANGUAGES));
        System.out.println(Arrays.deepToString(COUNTRIES));
    }
}