/** March.java - Let's simulate a group of people "marching" around a field. * We'll display a 2-d array, and then tell our people to move 1 square * in various directions. Purpose is to practice with 2-d arrays. * Since I'm going to use i and j a lot in this program, I'll just declare * them once at the top. */ public class March { public static void main(String [] args) { int [][] a = new int[10][10]; int i, j; // Initialize. Use 0 to represent an empty cell. for (i = 0; i < a.length; ++i) for (j = 0; j < a[0].length; ++j) if (i >= 2 && i <= 6 && j >= 2 && j <= 6) a[i][j] = 1; else a[i][j] = 0; // Display for (i = 0; i < a.length; ++i) { for (j = 0; j < a[0].length; ++j) System.out.print("" + a[i][j] + " "); System.out.println(); } System.out.println(); // Move right! Do the same thing for every row. for (i = 0; i < a.length; ++i) { for (j = 2; j <= 6; ++j) { a[i][j+1] = a[i][j]; } a[i][2] = 0; } // Display again System.out.println("After moving to the right:"); for (i = 0; i < a.length; ++i) { for (j = 0; j < a[0].length; ++j) System.out.print("" + a[i][j] + " "); System.out.println(); } System.out.println(); // Can we move up? I'm assuming that the data is in rows 2-6 // and columns 3-7 before the move. for (j = 3; j <= 7; ++j) { for (i = 2; i <=6; ++i) a[i-1][j] = a[i][j]; a[6][j] = 0; } // Display again System.out.println("After moving UP:"); for (i = 0; i < a.length; ++i) { for (j = 0; j < a[0].length; ++j) System.out.print("" + a[i][j] + " "); System.out.println(); } System.out.println(); } }