这个问题我想了很久。我需要一个可以吸收一些的类(class) double[][]
数组,然后存储此数组以备将来使用。我能想到的唯一存储选项是存储 double[][]
在ArrayList<>()
的 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
我收到此调用的空指针异常,我认为不可能创建“ArrayList”。我还有什么其他选择,我真的想不出这个?
最佳答案
你初始化数组列表了吗?
parametersToChange = new ArrayList<>();
关于java - 是否可以在 JAVA 中的 ArrayList<double[][]> 中存储 double[][] 数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21033614/