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

时间:2015-12-14 03:40:44

标签: java multidimensional-array

如果我的问题措辞严厉,我想先道歉。我有一个考试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();
        }

    }

2 个答案:

答案 0 :(得分:1)

首先,你正在初始化新数组错误应该是

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

您不希望它与您想要的长度+1相同。同样在你的for循环中你想要的是arr而不是矩阵的长度,因为那就是你从中得到的东西。将值放入新的N + 1xM + 1数组时,如果&gt; = 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;

答案 1 :(得分:0)

在你的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;
}