我一直在想这个问题。我需要一个可以接受一些double[][]
数组的类,然后存储这个数组以备将来使用。我能想到的唯一存储选项是将double[][]
存储在double[][]
的ArrayList<>()
中。是否可以在JAVA ArrayList <double[][]>中存储double [] []数组?
我也如下实现它:
public class AddToArray {
public String[] parameterNames;
public ArrayList<double[][]> parametersToChange;
public AddToArray(String[] parameterNames){
this.parameterNames = parameterNames;
}
public void addToArray(double[][] parametersToChange) throws InsufficientInputException
for(int i = 0; i < parametersToChange.length; i++){
if(parametersToChange[i].length != this.parameterNames.length)
throw new InsufficientInputException("DATA DIMENSION MISMATCH");
}
// This below gives nullpointexception.
this.parametersToChange.add(parametersToChange);
}
我通过这个例子拨打:
double[][] parametersToChange = {{0.005,0.006},{0.007,0.008}};
String[] par = {"SI1","SI2"};
AddToArray abc = new AddToArray(par);
abc.addToArray(parametersToChange);
System.out.println(abc.parametersToChange.get(0)[0][0]); // this would (in my ideal world) print out 0.005
我收到一个空指针异常这个电话,我在想,它不可能使一个'阵列列表'。我有什么其他选择,我真的无法弄清楚这一点吗?
它的工作原理。我感到尴尬,但它确实解决了这个问题。 – SteewDK