Printing an array - top of Musings page

By: Nicholas Duchon


// 1-D array of int
public static final int DL = 5; // length of int field
public static void printIntArray (int [] a) {
for (int x: a)
System.out.printf ("%" + DL + "d", x);
} // end printIntArray

// 2-D array of int
public static void printIntArray2 (int [][] a) {
for (int [] y: a) {
for (int x: y)
System.out.printf ("%" + DL + "d", x);
System.out.println ();
} // end for each row
} // end printIntArray2

// Arrays of objects are even simpler:
public static void printObjectArray (Object [] a) {
for (Object x: a) System.out.println (x);
} // end print printObjectArray

// 2-D array of objects:
public static void printObjectArray2 (Object [][] a)
for (Object [] y: a) {
for (Object x: y)
System.out.println (x);
System.out.println ();
} // end for each row
} // end method printObjectArray2

ND.