Skip to content

Instantly share code, notes, and snippets.

@zhelezoglo
Created March 5, 2015 17:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zhelezoglo/194e07db8a0df6bf40d5 to your computer and use it in GitHub Desktop.
Save zhelezoglo/194e07db8a0df6bf40d5 to your computer and use it in GitHub Desktop.
import java.util.Arrays;
public class ArrayPrintingExamples {
public static void main(String[] args) {
int[][] array2D = new int[][]{
{1,2,3},
{4,5,6},
{7,8,9}
};
System.out.println("Example 0");
for (int i = 0; i < array2D.length; i++) {
int [] array1D = array2D[i];
for (int j = 0; j < array1D.length; j++) {
System.out.print(array1D[j]);
}
System.out.println();
}
System.out.println("Example 1");
for (int i = 0; i < array2D.length; i++) {
for (int j = 0; j < array2D[i].length; j++) {
System.out.print(array2D[i][j]);
}
System.out.println();
}
System.out.println("Example 2");
for (int i = 0; i < array2D.length; i++) {
printArray(array2D[i]);
System.out.println();
}
System.out.println("Example 3");
for (int[] array1D : array2D) {
for (int element : array1D) {
System.out.print(element);
}
System.out.println();
}
System.out.println("Example 4");
for (int[] array1D : array2D) {
System.out.println(Arrays.toString(array1D));
}
System.out.println("etc...");
}
static void printArray(int[] array) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i]);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment