简体   繁体   English

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

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

I would like to first apologize if my question is worded badly. 如果我的问题措辞不好,我想首先道歉。 I have an exam tmmrw and the prof gave a sample final exam for us to practice with. 我参加了tmmrw考试,教授给了最终考试样本供我们练习。 He unfortunately isn't responding with the solutions on the forum so I am trying to provide solutions on there. 不幸的是,他没有在论坛上提供解决方案,因此,我尝试在该论坛上提供解决方案。 I seem to be stuck on this question. 我似乎被这个问题困扰。 I have to write a method that accepts and NxM array filled with integer values as a parameter. 我必须编写一个方法,该方法接受以整数值填充的NxM数组作为参数。 The method is to return an (N+1)x(M+1) array which contains the contents of the original array in the first N rows and M columns plus a count of items greater than or equal to zero in each row/column at the end of that row/column and put the value -1 in the bottom right corner. 该方法将返回一个(N + 1)x(M + 1)数组,其中包含前N行和M列中原始数组的内容,以及每行/列中大于或等于零的项数在该行/列的末尾,将值-1放在右下角。 for example. 例如。

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

I seem to be able to copy the array yet I am puzzled as to how I can enter the values in the outer parts of the new array. 我似乎能够复制该数组,但是我对如何在新数组的外部输入值感到困惑。 Here is what I have so far. 这是我到目前为止所拥有的。

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

    }

First off you are initializing the new array wrong it should be 首先,您正在错误地初始化新数组

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

You don't want it to be the same length you want it to be the length +1. 您不希望它的长度与您希望的长度+1相同。 Also in your for loops you want to go by the length of arr not matrix since thats what you're taking from. 另外,在您的for循环中,您想要的是arr而不是矩阵的长度,因为这就是您要提取的内容。 While putting the values into the new N+1xM+1 array, increment the value of the corresponding last element in that row and column by 1 if it is >=0. 将值放入新的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];

            }

After putting all the values back into the new N+1xM+1 array you should now take the values in the n sized and m sized arrays and put them into the corresponding slot in the N+1xM+1 array. 将所有值放回到新的N + 1xM + 1数组中之后,现在应该将这些值放入n个大小和m个大小的数组中,并将它们放入N + 1xM + 1数组中的相应插槽中。 After that just put the -1 in the bottom right slow manually. 之后,只需手动将-1放到右下角。

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

In your process2D method start off by creating an array with the correct size which has 1 more row and 1 more column than the original: 在您的process2D方法中,首先创建一个具有正确大小的数组,该数组比原始数组多1行,多1列:

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

Then to populate the matrix array you do like you were doing before, but you need to take care not to reference an index of the arr array that is out of bounds. 然后,要像以前一样填充矩阵数组,但是需要注意不要引用超出范围的arr数组的索引。 Because your matrix index is bigger than arr. 因为您的矩阵索引大于arr。 If you are populating the new indexes then you can just use random numbers. 如果要填充新索引,则可以使用随机数。

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

So here is the full method process2D: 所以这是完整的方法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