【问题标题】:2d array constructor that take array less equals or more than 3x3采用小于等于或大于 3x3 的数组的 2d 数组构造函数
【发布时间】:2022-01-12 18:20:44
【问题描述】:

从我的学位中得到一个项目,我被困在其中一个构造函数上。

  • 构造函数名称需要类似于Square3x3(int[][] array)
  • 它必须构造一个大小为 3x3 的二维数组,其值取自给定数组。
  • 如果给定数组的大小大于 3x3,则仅采用前 3x3 单元格。
  • 如果给定的数组较小,则其余单元格初始化为 -1。

请注意,给定的数组可能是非对称的,甚至可能有不同长度的行。

当且仅当给定数组中对应的单元格不存在时,确保将单元格初始化为 -1。

你可以假设数组不为空。

当给定数组大于 3x3 时,以下代码适用于我,但当它小于 3x3 时,我无法使其工作。

请注意,所有这些都需要在与main 不同的类中。

感谢您的帮助。

我得到以下代码:

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++;
        }
    }
}

【问题讨论】:

    标签: java arrays multidimensional-array constructor initialization


    【解决方案1】:
    1. 构造一个 3x3 大小的结果数组并用 -1 填充。
    2. 根据需要从输入数组中复制值,选择最小值 3 和输入数组的行/列。
    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 == null ? -1 : array.length); i < n; i++) {
            for(int j = 0, m = Math.min(3, array[i] == null ? -1 : array[i].length); j < m; j++) {
                square3x3[i][j] = array[i][j];
            }
        }
    }
    

    因此,如果输入 array 的尺寸小于 3x3,则在复制值时将使用 -1 的预填充值。

    测试:

    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));
    // input with nulls
    mc = new MyClass(new int[][]{{1, 2}, null, {3}});
    System.out.println(Arrays.deepToString(mc.square3x3));
    

    输出:

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

    为了解决关于将单元格设置为 -1 的一点异想天开的要求当且仅当给定数组中的相应单元格不存在时可以提供以下实现:

    private int[][] square3x3;
    public Square3x3(int[][] array) {
        square3x3 = new int[3][3]; // anyway an array filled with 0 is created
            
        for (int i = 0; i < 3; i++) {
            for(int j = 0; j < 3; j++) {
                square3x3[i][j] = null != array    && i < array.length 
                               && null != array[i] && j < array[i].length
                                ? array[i][j] : -1;
            }
        }
    }
    

    该要求试图以复杂/复杂的方式提供特定的实现,但这与预期的输出无关,实际上需要遍历循环中的所有 3x3 单元,而通过正确的初始化和不相关的行/列可以实现完全相同可以跳过。

    【讨论】:

    • 感谢您的帮助,它有效。我将 consturct 数组放在第二个数组中。
    • 这个解决方案肯定更紧凑,但它跳过了其中一个要求:“当且仅当给定数组中的相应单元格不存在时,确保将单元格初始化为 -1。”跨度>
    • @geco17,这个要求似乎与输出不太相关。无论如何,提供了一个更新版本(添加空检查以防万一)
    • @AlexRudenko 我同意,它与输出无关,问题看起来有点像“家庭作业”问题;无论如何,我在更正后投了赞成票
    【解决方案2】:

    您必须更详细地查看初始化数组的逻辑。如果索引不存在,则为 -1;否则它将是 i-j 索引处的值。所以你必须看看数组是否至少有第 i 行。如果没有,你马上就知道对于给定的 (i,j) 你有 -1。如果它至少有第 i 行,它是否有第 j 个元素?如果不是,再次-1。否则它是数组的第 i-j 个元素。

    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;
            }
        }
    }
    

    【讨论】:

    • 感谢您的帮助,它可以工作,但与第二个答案一起使用。
    【解决方案3】:
    public void whosThereRow (int row, boolean [] values)
    

    方法的工作原理:

    该方法接收一个行号(来自值 2、1、0)和一个布尔数组 尺寸 10。

    对于方法类的二维数组中对应行中出现的每个9-1之间的数字,都会在values数组的对应单元格中放置一个真值。

    数组中的其余单元格将保持不变。

    【讨论】:

    • 您在描述一种方法,但实际上并未给出其逻辑应该是什么样子的示例。这还不错,但如果你能提供一个工作代码示例,甚至是伪代码,以更清晰的方式呈现你的解决方案,它会更有用。
    猜你喜欢
    • 2020-02-08
    • 1970-01-01
    • 1970-01-01
    • 2012-09-22
    • 2011-09-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多