Multidimensional Arrays

public class MultidimensionalArrays {
    public static final ArrayPrinter out = new ArrayPrinter("\t");
    public static void main(String args[]) {
        out.printHeading("One-dimensional arrays of primitives.\n");
        out.println(new short[2]);
        out.println(new int[2]);
        out.println(new long[2]);
        out.println(new float[2]);
        out.println(new double[2]);
        out.println(new char[2]);
        out.println(new boolean[2]);
        out.printHeading("One-dimensional arrays of objects.\n");
        out.println(new Object[2]);
        out.println(new String[2]);
        out.println(new Short[2]);
        out.println(new Integer[2]);
        out.println(new Long[2]);
        out.println(new Float[2]);
        out.println(new Double[2]);
        out.println(new Character[2]);
        out.println(new Boolean[2]);
        out.printHeading("Two-dimensional arrays of primitives.\n");
        out.println(new short[2][]);
        out.println(new int[2][]);
        out.println(new long[2][]);
        out.println(new float[2][]);
        out.println(new double[2][]);
        out.println(new char[2][]);
        out.println(new boolean[2][]);
        out.printHeading("More two-dimensional arrays of primitives.\n");
        out.println(new short[2][2]);
        out.println(new int[2][2]);
        out.println(new long[2][2]);
        out.println(new float[2][2]);
        out.println(new double[2][2]);
        out.println(new char[2][2]);
        out.println(new boolean[2][2]);
        out.printHeading("Hodge-podge.\n");
        final Object[] arrayOfObjects = {"apple", false, 3.14, -2, 'Z'};
        final Number[][] twoDimArrayOfNumbers = { {1, 2, 3}, {4.5, 5} };
        final Object[][] twoDimArrayOfObjects = {
            {}, {"cat", "dog"}, {'A', 'B'}, arrayOfObjects, args
        };
        final Object[][][] threeDimArrayOfObjects = {
            {{true, false}, new Boolean[2]},
            twoDimArrayOfObjects,
            twoDimArrayOfNumbers
        };
        out.println(new int[0][][]);
        out.println(new Object[3][3][2]);
        out.println(new Integer[2][2][2][]);
        out.println(twoDimArrayOfNumbers);
        out.println(twoDimArrayOfObjects);
        out.println(threeDimArrayOfObjects);
    }
}

final class ArrayPrinter {

    private final String tab;
    private int headNum = 0;

    public ArrayPrinter(final String tab) { this.tab = tab; }
    public void println(final short[] array) { printPrimitives(array, tab); end(); }
    public void println(final int[] array) { printPrimitives(array, tab); end(); }
    public void println(final long[] array) { printPrimitives(array, tab); end(); }
    public void println(final float[] array) { printPrimitives(array, tab); end(); }
    public void println(final double[] array) { printPrimitives(array, tab); end(); }
    public void println(final char[] array) { printPrimitives(array, tab); end(); }
    public void println(final boolean[] array) { printPrimitives(array, tab); end(); }
    public void println(final Object[] array) { printObjects(array, tab); end(); }
    public void printHeading(final String heading) { System.out.println(++headNum + ". " + heading); }

    private void printObjects(final Object[] array, final String tabs) {
        final String type = array.toString().substring(0, 2).equals("[[") ? "array" : "object";
        System.out.println(tabs + "Array of " + type + "s: " + array);
        for (Object element : array) {
            if (element instanceof Object[])
                printObjects((Object[])element, tabs + tab);
            else if (element instanceof short[])
                printPrimitives((short[])element, tabs + tab);
            else if (element instanceof int[])
                printPrimitives((int[])element, tabs + tab);
            else if (element instanceof long[])
                printPrimitives((long[])element, tabs + tab);
            else if (element instanceof float[])
                printPrimitives((float[])element, tabs + tab);
            else if (element instanceof double[])
                printPrimitives((double[])element, tabs + tab);
            else if (element instanceof char[])
                printPrimitives((char[])element, tabs + tab);
            else if (element instanceof boolean[])
                printPrimitives((boolean[])element, tabs + tab);
            else
                System.out.println(tabs + tab + element);
        }
    }
    private void printPrimitives(final short[] array, final String tabs) {
        System.out.println(tabs + "array of shorts: " + array);
        for (short value : array) System.out.println(tabs + tab + value);
    }
    private void printPrimitives(final int[] array, final String tabs) {
        System.out.println(tabs + "array of ints: " + array);
        for (int value : array) System.out.println(tabs + tab + value);
    }
    private void printPrimitives(final long[] array, final String tabs) {
        System.out.println(tabs + "array of longs: " + array);
        for (long value : array) System.out.println(tabs + tab + value);
    }
    private void printPrimitives(final float[] array, final String tabs) {
        System.out.println(tabs + "array of floats: " + array);
        for (float value : array) System.out.println(tabs + tab + value);
    }
    private void printPrimitives(final double[] array, final String tabs) {
        System.out.println(tabs + "array of doubles: " + array);
        for (double value : array) System.out.println(tabs + tab + value);
    }
    private void printPrimitives(final boolean[] array, final String tabs) {
        System.out.println(tabs + "array of booleans: " + array);
        for (boolean value : array) System.out.println(tabs + tab + value);
    }
    private void printPrimitives(final char[] array, final String tabs) {
        System.out.println(tabs + "array of chars: " + array);
        for (char value : array) System.out.println(tabs + tab + value);
    }
    private void end() { System.out.println(); }
}

Exercises

  1. Read https://docs.oracle.com/javase/specs/jls/se7/html/jls-10.html.
  2. Read the documentation for the toString and hashCode methods at https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#toString() and https://docs.oracle.com/javase/7/docs/api/java/lang/Object.html#hashCode() respectively.
  3. Declare a constant variable named x that refers to an array of values. Initialize x with a value that is not null.
    1. What expression did you use to set the value of x?
    2. What is the value of the expression x.toString()?
    3. What output is produced by the statement System.out.print(x);?
  4. Assume that a variable y refers to an array of values and that the value of y.toString() is "[[Ljava.lang.Number;@4ca8195f".
    1. What does the substring "[[" indicate about the value of y?
    2. What does the letter 'L' in the substring "[[L" signify?
    3. What other single letters besides 'L' may appear after a left square bracket in similar strings? What does each of those letters signify?
    4. What is the meaning of the value to the right of the '@' character?
  5. What does the following program print? Try to figure out the answer without running the program. Then run the program to check your answer.
        public class MyArrays {
            public static void main(String args[]) {
                System.out.println((new boolean[1])[0]);
                System.out.println(new boolean[1][0]);
                System.out.println((new boolean[1][])[0]);
                System.out.println((new boolean[1][0])[0]);
                System.out.println(new boolean[1][0][0]);
                System.out.println((new boolean[1][1])[0][0]);
                System.out.println((new boolean[1][1][0])[0]);
            }
        }
    
  6. How will the output of the program from the previous exercise change if all occurrences of boolean are replaced with int? Try to figure out the answer without running the program. Then run the modified program to check your answer.
  7. For each of the following statements indicate whether the statement is valid or not. If it is not, explain why not. Check your answers by adding each statement, one at a time, to a program that compiles: if the program still compiles after adding a statement, then the statement is valid; otherwise, it is not.
    1. final char[] array;
    2. final char[2] array = new char[3];
    3. final char[][] array = new char[3][];
    4. final char[][] array = new char[][3];
    5. final char[][] array = new char[0][3];
    6. final char[] array = {"a", "b", "c"};
    7. final char[] array = new char[3][];
    8. final char[][] array = new char[3][3];
    9. final char[] array = (new char[3])[0];
    10. final char array = (new char[3])[0];
    11. final char array = (new char[3][])[0];
    12. final Character array = (new char[3][])[0];
    13. final char array = (new char[3][])[0][0];
    14. final Object[] array = {"a", 1, false, 'b', 'c'};
    15. final Object array[] = {"a", 1, false, 'b', 'c'};
    16. final Object[] array = {"a", 1, false, {'b', 'c'}};