2014-01-09 56 views
1

我一直在想这个问题。我需要一个可以接受一些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 

我收到一个空指针异常这个电话,我在想,它不可能使一个'阵列列表'。我有什么其他选择,我真的无法弄清楚这一点吗?

回答

7

你初始化了数组列表吗?

parametersToChange = new ArrayList<>(); 
+1

它的工作原理。我感到尴尬,但它确实解决了这个问题。 – SteewDK

0

你忘了初始化parametersToChange

0

由于仿制药的方式在Java中(类型擦除)来实现的,数组和仿制药不能很好的一起工作。

Joshua Bloch在Effective Java有专题的讨论,你可以找到该部分here

相关问题