Home > Net >  2d array constructor that take array less equals or more than 3x3
2d array constructor that take array less equals or more than 3x3

Time:11-24

Got a project from my degree that I'm stuck on one of the constructors.

  • The constructor name needs to look like this Square3x3(int[][] array).
  • It has to construct a 2d array of the size 3x3, whose value are taken from the given array.
  • If the given array's size is bigger than 3x3, only the first 3x3 cells are taken.
  • If the given array is smaller, the rest of the cells are initialized to -1.

Note that the given array may be non-symmetrical, or may even have rows of different length.

Make sure to initialize cells to -1 if and only if the corresponding cell in the given array does not exist.

You may assume the array is not null.

The following code works for me when the given array is bigger than 3x3, but I cannot make it work when it's less than 3x3.

Note that all of this needs to be in a different class than main.

Thank you for the help.

I got the following code:

public class Square3x3 {
        
    private int[][] Square3x3;
    
    public Square3x3(int[][]array) {
        Square3x3 = new int[3][3];
        int count =0;
        for (int i = 0; i < 3 && i < array.length; i  ) {
            for (int j = 0; j < 3 && j < array[count].length; j  ) {
                Square3x3[i][j] = array[i][j];
            }
            count  ;
        }
    }
}

CodePudding user response:

You have to look in more detail at the logic for initializing the array. It's -1 if the index doesn't exist; otherwise it will be the value at the i-j index. So you have to see if the array has at least the i-th row. If it doesn't, you know right away that for a given (i,j) you've got -1. If it does have at least the i-th row, does it have the j-th element? If no, -1 again. Otherwise it's the i-j-th element of the array.

private int[][] square3x3;

public Square3x3(int[][] array) {
    final int dim = 3;
    final int defVal = -1;
    square3x3 = new int[dim][dim];
    for (int i = 0; i < dim; i  ) {
        for (int j = 0; j < dim; j  ) {
            final int val;
            if (array.length < (i   1)) {
                val = defVal;
            } else {
                if (array[i].length < (j   1)) {
                    val = defVal;
                } else {
                    val = array[i][j];
                }
            }
            square3x3[i][j] = val;
        }
    }
}

CodePudding user response:

  1. Construct a result array of 3x3 size and fill it with -1.
  2. Copy the values from the input array as necessary, selecting a minimal value of 3 and rows/columns of the input array.
private int[][] square3x3 = {
    {-1, -1, -1},
    {-1, -1, -1},
    {-1, -1, -1}
};

public Square3x3(int[][] array) {
    for (int i = 0, n = Math.min(3, array.length); i < n; i  ) {
        for(int j = 0, m = Math.min(3, array[i].length); j < m; j  ) {
            square3x3[i][j] = array[i][j];
        }
    }
}

So, if the dimensions of the input array are less than 3x3, the prepopulated values of -1 will be used when copying the values.

Tests:

Square3x3 sq = new Square3x3(new int[3][2]); // input 2D arr initialized with 0
        
System.out.println(Arrays.deepToString(sq.square3x3));

// jagged input
sq = new Square3x3(new int[][]{{1, 2}, {3}, {4, 5, 6}});
System.out.println(Arrays.deepToString(sq.square3x3));

Output:

[[0, 0, -1], [0, 0, -1], [0, 0, -1]]
[[1, 2, -1], [3, -1, -1], [4, 5, 6]]
  • Related