Learn Java Easy. Chapter 8 Exercises

Exercises Topic 8 – 2D Arrays in Java

Exercises Topic 8

Exercise 1. Create a 5×5 2D array representing prison cells. Fill all positions with the value 0, indicating that all cells are empty.

Solution:

public class Main {

    public static void main(String[] args) {
        int[][] cells = new int[5][5];

        // Fill the array with 0
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j] = 0;
            }
        }

        // Print the array
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 2. Suppose cells [0][1], [2][2], and [4][3] are occupied by prisoners. Update the array from the previous exercise to reflect this using the value 1 for occupied cells.

Solution:

public class Main {

    public static void main(String[] args) {
        int[][] cells = new int[5][5];

        // Fill the array with 0
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                cells[i][j] = 0;
            }
        }

        // Mark occupied cells
        cells[0][1] = 1;
        cells[2][2] = 1;
        cells[4][3] = 1;

        // Print the array
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 3. Count how many cells are occupied in the array created in exercise 2.

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        int counter = 0;

        // Count occupied cells
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                if (cells[i][j] == 1) {
                    counter++;
                }
            }
        }

        System.out.println("Occupied cells: " + counter);
    }
}

Exercise 4. Release the prisoner from cell [2][2] in the array from exercise 2 and update the array.

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        // Release the prisoner
        cells[2][2] = 0;

        // Print the array
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 5. Find all empty cells in the array from exercise 2 and display their positions.

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        System.out.println("Empty cells:");

        // Find empty cells
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                if (cells[i][j] == 0) {
                    System.out.println("[" + i + "][" + j + "]");
                }
            }
        }
    }
}

Exercise 6. Transfer a prisoner from cell [4][3] to cell [1][4] in the array from exercise 2

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        // Transfer the prisoner
        cells[4][3] = 0;
        cells[1][4] = 1;

        // Print the array
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                System.out.print(cells[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 7. Count how many occupied neighboring cells the cell [2][2] has in the array from exercise number 2.

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        int[][] directions = {
            {-1, -1}, {-1, 0}, {-1, 1},
            { 0, -1},          { 0, 1},
            { 1, -1}, { 1, 0}, { 1, 1}
        };

        int x = 2, y = 2;
        int counter = 0;

        // Count occupied neighboring cells
        for (int[] dir : directions) {
            int newX = x + dir[0];
            int newY = y + dir[1];

            if (newX >= 0 && newX < cells.length && newY >= 0 && newY < cells[0].length) {
                if (cells[newX][newY] == 1) {
                    counter++;
                }
            }
        }

        System.out.println("Occupied neighboring cells of [2][2]: " + counter);
    }
}

💡Initialization and Declaration: Always initialize your array correctly after declaring it. You can do it in a single line to keep the code clean and easy to read.

💡Use of Nested Loops: Use nested loops to traverse and manipulate elements in the array. The outer loop traverses the rows and the inner loop traverses the columns. This is essential for performing operations on each element of the array.

Exercise 8. Find all prisoners in row 0 of the array from exercise 2 and display their positions.

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        System.out.println("Prisoners in row 0:");
        
        for (int j = 0; j < cells[0].length; j++) {
            if (cells[0][j] == 1) {
                System.out.println("[0][" + j + "]");
            }
        }
    }
}

💡Check Limits: Make sure not to exceed the array limits. Always verify that your indices are within the valid range.

Exercise 9. Rotate the array from exercise 2 90 degrees clockwise.

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        int[][] rotated = new int[cells[0].length][cells.length];

        // Rotate the array 90 degrees to the right
        for (int i = 0; i < cells.length; i++) {
            for (int j = 0; j < cells[i].length; j++) {
                rotated[j][cells.length - 1 - i] = cells[i][j];
            }
        }

        // Print the rotated array
        for (int i = 0; i < rotated.length; i++) {
            for (int j = 0; j < rotated[i].length; j++) {
                System.out.print(rotated[i][j] + " ");
            }
            System.out.println();
        }
    }
}

Exercise 10. Verify if there is an escape pattern in the array from exercise 2, defined as three consecutive occupied cells in any row.

Solution:

public class Main {

    public static void main(String[] args) {

        int[][] cells = {
            {0, 1, 0, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 1, 0, 0},
            {0, 0, 0, 0, 0},
            {0, 0, 0, 1, 0}
        };

        boolean escapeFound = false;

        for (int i = 0; i < cells.length && !escapeFound; i++) {
            int consecutive = 0;

            for (int j = 0; j < cells[i].length; j++) {
                if (cells[i][j] == 1) {
                    consecutive++;
                    if (consecutive == 3) {
                        escapeFound = true;
                        break;
                    }
                } else {
                    consecutive = 0;
                }
            }
        }

        if (escapeFound) {
            System.out.println("Escape pattern found!");
        } else {
            System.out.println("No escape pattern found.");
        }
    }
}

Did you like it? Don’t keep it to yourself — share it like juicy gossip! 😏