如何在Java中增加或减少矩阵的大小?

时间:2021-05-14 17:20:10

I have m×n matrix and I need to change the number of columns (increase or decrease). I have the following code but it does not work.

我有m×n矩阵,我需要更改列数(增加或减少)。我有以下代码但它不起作用。

public class Resize {

    public static int [][] A = new int [2][2];
    public static int i, j;

    public static void main(String[] args) {
        A = (int[][])resizeArray(A,4);
        for(i = 0 ; i < 2 ; i++){
        for(j = 0 ; j < 4 ; j++){
            A[i][j] = j+i;
            System.out.print(A[i][j]+"   ");
        }
        System.out.println("");
    }
}
// resize arr from dimension n = 20 to dimension n = 14  ///////////////
private static Object resizeArray (Object oldArray, int newSize) {
       int oldSize = java.lang.reflect.Array.getLength(oldArray);
       Class elementType = oldArray.getClass().getComponentType();
       Object newArray = java.lang.reflect.Array.newInstance(elementType, newSize);
       int preserveLength = Math.min(oldSize, newSize);
       if (preserveLength > 0)
          System.arraycopy(oldArray, 0, newArray, 0, preserveLength);
       return newArray; 
    }
}

2 个解决方案

#1


1  

The problem is that you're changing the number of rows rather than the number of columns in your resizeArray method. You can tell by printing at the end of your main method, A.length, which equals the number of rows in the 2D array. The line

问题是您正在更改行数而不是resizeArray方法中的列数。您可以通过在main方法的末尾打印来确定A.length,它等于2D数组中的行数。这条线

int oldSize = java.lang.reflect.Array.getLength(oldArray);

is the same as setting oldSize to A.length. So we both agree that oldSize is the number of rows in the inputted array. And then the line

与将oldSize设置为A.length相同。所以我们都同意oldSize是输入数组中的行数。然后就行了

System.arraycopy(oldArray, 0, newArray, 0, preserveLength);

copies elements oldArray[0], oldArray[1], oldArray[2], ... oldArray[preserveLength - 1] to newArray[0], newArray[1], newArray[2], ... newArray[preserveLength - 1] respectively. With a 2D array, you're basically copying rows of the old array and putting them into a new array.

将元素oldArray [0],oldArray [1],oldArray [2],... oldArray [preserveLength - 1]复制到newArray [0],newArray [1],newArray [2],... newArray [preserveLength - 1 ] 分别。使用2D数组,您基本上复制旧数组的行并将它们放入新数组中。

A possible solution could be to make a new array of size Math.min(oldArray[0].length, newLength) and then loop through the new array by putting elements from the old array into the new array.

一个可能的解决方案是创建一个大小为Math.min(oldArray [0] .length,newLength)的新数组,然后通过将旧数组中的元素放入新数组中来遍历新数组。

private static int[][] resizeArray (int[][] oldArray, int newSize) {
   int oldSize = oldArray[0].length; //number of columns
   int preserveLength = Math.min(oldSize, newSize);
   int[][] newArray = new int[oldArray.length][newSize];
   for(int i = 0; i < oldArray.length; i++) {
       for(int j = 0; j < preserveLength; j++) {
           newArray[i][j] = oldArray[i][j];
       }
   } 
   return newArray;  
}

#2


0  

You cannot assign it to array A as it's dimensions are already defined. You can declare another array which is not initiated.

您无法将其分配给阵列A,因为它的尺寸已经定义。您可以声明另一个未启动的数组。

Also I think you are making it too much complicated in resizeArray method. unless you want to learn reflection you can just create a new array with new size then copy and return;

另外我认为你在resizeArray方法中使它太复杂了。除非您想学习反射,否则您可以创建一个具有新大小的新数组,然后复制并返回;

#1


1  

The problem is that you're changing the number of rows rather than the number of columns in your resizeArray method. You can tell by printing at the end of your main method, A.length, which equals the number of rows in the 2D array. The line

问题是您正在更改行数而不是resizeArray方法中的列数。您可以通过在main方法的末尾打印来确定A.length,它等于2D数组中的行数。这条线

int oldSize = java.lang.reflect.Array.getLength(oldArray);

is the same as setting oldSize to A.length. So we both agree that oldSize is the number of rows in the inputted array. And then the line

与将oldSize设置为A.length相同。所以我们都同意oldSize是输入数组中的行数。然后就行了

System.arraycopy(oldArray, 0, newArray, 0, preserveLength);

copies elements oldArray[0], oldArray[1], oldArray[2], ... oldArray[preserveLength - 1] to newArray[0], newArray[1], newArray[2], ... newArray[preserveLength - 1] respectively. With a 2D array, you're basically copying rows of the old array and putting them into a new array.

将元素oldArray [0],oldArray [1],oldArray [2],... oldArray [preserveLength - 1]复制到newArray [0],newArray [1],newArray [2],... newArray [preserveLength - 1 ] 分别。使用2D数组,您基本上复制旧数组的行并将它们放入新数组中。

A possible solution could be to make a new array of size Math.min(oldArray[0].length, newLength) and then loop through the new array by putting elements from the old array into the new array.

一个可能的解决方案是创建一个大小为Math.min(oldArray [0] .length,newLength)的新数组,然后通过将旧数组中的元素放入新数组中来遍历新数组。

private static int[][] resizeArray (int[][] oldArray, int newSize) {
   int oldSize = oldArray[0].length; //number of columns
   int preserveLength = Math.min(oldSize, newSize);
   int[][] newArray = new int[oldArray.length][newSize];
   for(int i = 0; i < oldArray.length; i++) {
       for(int j = 0; j < preserveLength; j++) {
           newArray[i][j] = oldArray[i][j];
       }
   } 
   return newArray;  
}

#2


0  

You cannot assign it to array A as it's dimensions are already defined. You can declare another array which is not initiated.

您无法将其分配给阵列A,因为它的尺寸已经定义。您可以声明另一个未启动的数组。

Also I think you are making it too much complicated in resizeArray method. unless you want to learn reflection you can just create a new array with new size then copy and return;

另外我认为你在resizeArray方法中使它太复杂了。除非您想学习反射,否则您可以创建一个具有新大小的新数组,然后复制并返回;