繁体   English   中英

复制2D数组并增加大小以填充新值

[英]Copy 2D array and increase size to fill new values

如果我的问题措辞不好,我想首先道歉。 我参加了tmmrw考试,教授给了最终考试样本供我们练习。 不幸的是,他没有在论坛上提供解决方案,因此,我尝试在该论坛上提供解决方案。 我似乎被这个问题困扰。 我必须编写一个方法,该方法接受以整数值填充的NxM数组作为参数。 该方法将返回一个(N + 1)x(M + 1)数组,其中包含前N行和M列中原始数组的内容,以及每行/列中大于或等于零的项数在该行/列的末尾,将值-1放在右下角。 例如。

    1 -2  0              returns          1 -2  0  2
    3 -4 -5                               3 -4 -5  1
                                          2  0  1 -1

我似乎能够复制该数组,但是我对如何在新数组的外部输入值感到困惑。 这是我到目前为止所拥有的。

public static void main(String[] args) {

        int[][] arr = { { 1, -2, 0 }, { 3, -4, -5 } };

        int[][] newMatrix = processing2D(arr);

        printArray(newMatrix);

    }

    //Method where I am having problems
    public static int[][] processing2D(int[][] arr) {

        int[][] matrix = new int[arr.length][arr[0].length];

        for (int row = 0; row < matrix.length; row++) {

            for (int col = 0; col < matrix[0].length; col++) {

                // once I reach the last pos I enter the count
                // of numbers greater than or equal to zero in that row/col

                matrix[row][col] = arr[row][col];

            }

        }

        // assign the corner -1 here

        return matrix;
    }

    public static void printArray(int[][] list) {

        for (int row = 0; row < list.length; row++) {
            for (int col = 0; col <= list.length; col++) {
                System.out.print(list[row][col] + " ");
            }
            System.out.println();
        }

    }

首先,您正在错误地初始化新数组

int[][] matrix = new int[arr.length+1][arr[0].length+1];

您不希望它的长度与您希望的长度+1相同。 另外,在您的for循环中,您想要的是arr而不是矩阵的长度,因为这就是您要提取的内容。 将值放入新的N + 1xM + 1数组中时,如果该行和列中对应的最后一个元素的值> = 0,则将其递增1。

for (int row = 0; row < arr.length; row++) {

        for (int col = 0; col < arr[0].length; col++) {

            // once I reach the last pos I enter the count
            // of numbers greater than or equal to zero in that row/col
            if(arr[row][col]>=0){
              matrix[row][matrix[row].length-1] = matrix[row][matrix[row].length-1] + 1;

              matrix[matrix.length-1][col]= matrix[matrix.length-1][col] + 1;
            }


            matrix[row][col] = arr[row][col];

            }

将所有值放回到新的N + 1xM + 1数组中之后,现在应该将这些值放入n个大小和m个大小的数组中,并将它们放入N + 1xM + 1数组中的相应插槽中。 之后,只需手动将-1放到右下角。

matrix[matrix.length-1][matrix[0].length-1]=-1;

在您的process2D方法中,首先创建一个具有正确大小的数组,该数组比原始数组多1行,多1列:

int[][] matrix = new int[arr.length+1][arr[0].length+1];

然后,要像以前一样填充矩阵数组,但是需要注意不要引用超出范围的arr数组的索引。 因为您的矩阵索引大于arr。 如果要填充新索引,则可以使用随机数。

        if(row < arr.length && col < arr[0].length)
        {
            matrix[row][col] = arr[row][col];
        }
        else
        {
            matrix[row][col] = new Random().nextInt(10);
        }

所以这是完整的方法process2D:

public static int[][] processing2D(int[][] arr) {

    int[][] matrix = new int[arr.length+1][arr[0].length+1];

    for (int row = 0; row < matrix.length; row++) {

        for (int col = 0; col < matrix[0].length; col++) {

            // once I reach the last pos I enter the count
            // of numbers greater than or equal to zero in that row/col
            if(row < arr.length && col < arr[0].length)
            {
                matrix[row][col] = arr[row][col];
            }
            else
            {
                matrix[row][col] = new Random().nextInt(10);
            }

        }

    }

    // assign the corner -1 here

    return matrix;
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM