Printing a two-dimensional array Java

I have a task, to fill up a two-dimensional array in a specific way, so it should make an output like shown on the picture, for example: 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 1 0 0 0 0 0 1 0 1 0 0 0 1 0 0 0 1 0 1 0 0 0 0 0 1

This is what I did and it works fine:

public class TwoDArrays {
    public static void main(String[] args) {
        FillUpTwoDArray(7, 7);
    }

    public static void FillUpTwoDArrays(int rows, int columns) {
        int twoD[][] = new int[rows][columns];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                twoD[i][j] = 0;
                if (i == j) {
                    twoD[i][j] = 1;
                }
                if (i + j == rows - 1) {
                    twoD[i][j] = 1;
                }
            }
        }
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++)
                System.out.print(twoD[i][j] + " ");
            System.out.println();
        }
    }
    ...
}

My headache starts here: 1 1 1 1 1 1 1 0 1 1 1 1 1 0 0 0 1 1 1 0 0 0 0 0 1 0 0 0 0 0 1 1 1 0 0 0 1 1 1 1 1 0 1 1 1 1 1 1 1

Maybe logic looks obvious for someone, but I have no clue at all, how to explain this to JVM. I don’t ask you for a solution-ready answer, some hints would be perfect, thank you in advance!

Answer

So what you need to do is attack the other boundary conditions that exist within the diagonals that you’re already printing. The first boundary is defined as the area where the column index is greater than the row index, but less than the row’s length minus the row index:

for (int k = j; k < twoD[i].length - i; k++) {
    if (k > i) {
        twoD[i][k] = 1;
    }
}

The lower portion of the triangle is a bit tougher. The boundary there is defined as the area where the column index is between the row index, and the length of the row, minus the row index, minus 1:

for (int h = j; h < twoD[i].length; h++) {
    if (h < i && j > (rows - i - 1)) {
        twoD[i][h] = 1;
    }
}

Placed altogether with the rest of your code, the solution (or rather, a solution; I don’t claim that this is optimized) would be:

public class TwoDArrays {
    public static void main(String[] args) {
        FillUpTwoDArrays(7, 7);
    }

    public static void FillUpTwoDArrays(int rows, int columns) {
        int twoD[][] = new int[rows][columns];
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++) {
                twoD[i][j] = 0;

                for (int k = j; k < twoD[i].length - i; k++) {
                    if (k > i) {
                        twoD[i][k] = 1;
                    }
                }

                if (i == j) {
                    twoD[i][j] = 1;
                }

                if (i + j == rows - 1) {
                    twoD[i][j] = 1;
                }

                for (int h = j; h < twoD[i].length; h++) {
                    if (h < i && j > (rows - i - 1)) {
                        twoD[i][h] = 1;
                    }
                }
            }
        }
        for (int i = 0; i < rows; i++) {
            for (int j = 0; j < columns; j++)
                System.out.print(twoD[i][j] + " ");
            System.out.println();
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *