简体   繁体   English

Java:如何访问对象列表中包含的2D数组

[英]Java: How to access a 2D array contained in an object list

I wanted to make an array of 2D arrays, but was pointed towards an ArrayList by a member here, and following his suggestion I wrote the following code: 我想制作一个2D数组的数组,但是这里的一个成员指向ArrayList,并按照他的建议编写了以下代码:

public class matrixArray {
    double[][] array;
    public matrixArray(double[][] initialArray){
        array = initialArray;
    }
}

I then implemented it by the following: 然后,我通过以下方式实现了它:

public static ArrayList LUDecompose(double[][] A){

    ArrayList<matrixArray> LU = new ArrayList<>();
    double[][] L = new double[A.length][A.length];
    double[][] U = new double[A.length][A.length];

    for(int j=0; j<(A.length-1);j++){
        for(int i=(j+1); i<A.length; i++){

            double lower = A[i][j]/A[j][j];
            L[i][j] = lower;

            double[] replacement = row_scalar_mult(lower,A[j]);
            replacement = vector_add(replacement, A[i]);

            row_replace(i, A, replacement);
        }
    }

    LU.add(new matrixArray(L));
    LU.add(new matrixArray(A));

    return LU;
}

So far so good. 到现在为止还挺好。 Now, I want to access the 2D arrays in my fancy new ArrayList. 现在,我想访问新的ArrayList中的2D数组。 When I try the following code in my main method, I get the error "Incompatible types: Object cannot be converted to double[][]": 当我在主方法中尝试以下代码时,出现错误“不兼容的类型:对象无法转换为double [] []”:

double[][] L = LUDecompose(A).get(0);
double[][] U = LUDecompose(A).get(0);

OR 要么

ArrayList LU = LUDecompose(A);
double[][] L = LUDecompose(A).get(0);
double[][] U = LUDecompose(A).get(0);

So my question becomes: How do I access the "Object" to get out my treasured 2D Arrays? 所以我的问题变成:如何访问“对象”以取出我珍贵的2D阵列?

You need to specify the proper return type in the method signature: 您需要在方法签名中指定正确的返回类型:

public static ArrayList<matrixArray> LUDecompose(double[][] A){

note the use of ArrayList<matrixArray> rather than just ArrayList 注意使用ArrayList<matrixArray>而不是ArrayList

then use 然后使用

ArrayList LU = LUDecompose(A);
double[][] L = LUDecompose(A).get(0).array;
double[][] U = LUDecompose(A).get(0).array;

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

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