将2D ArrayList转换为2D数组

我已经看过几个答案了,我发现我得到的答案都有错误。 我正在尝试将Doubles []的ArrayList转换为普通的双2D数组。 我的代码:

public ArrayList ec; public double[][] ei; ... encogCorpus = new ArrayList(); ... ec.add(inputs); ... ei = new double[ec.size()][]; for (int i = 0; i < ec.size(); i++) { ArrayList row = ec.get(i); ei[i] = row.toArray(new double[row.size()]); } 

我收到错误的说法

类型不匹配:无法从Double []转换为ArrayList

类型ArrayList中的方法toArray(T [])不适用于参数(double [])

问题

  1. 首先,这里的ecArrayList ,这意味着ec.get(i)应该返回Double[]而不是ArrayList
  2. 其次, doubleDouble是完全不同的类型。 您不能简单地在代码上使用row.toArray(new double[row.size()])

解决方案

1。

如果你想要一个真正的Doubles 2D ArrayList ,那么ec的类型应该是ArrayList> 。 但是因为我们不能使用toArray() ,所以我们手动循环。

 public ArrayList> ec; // line changed here public double[][] ei; ... encogCorpus = new ArrayList>(); // and here also ... ec.add(inputs); // `inputs` here should be of type `ArrayList` ... ei = new double[ec.size()][]; for (int i = 0; i < ec.size(); i++) { ArrayList row = ec.get(i); // Perform equivalent `toArray` operation double[] copy = new double[row.size()]; for (int j = 0; j < row.size(); j++) { // Manually loop and set individually copy[j] = row.get(j); } ei[i] = copy; } 

2。

但是如果你坚持使用ArrayList ,我们只需要改变主要部分:

 public ArrayList ec; public double[][] ei; ... encogCorpus = new ArrayList(); ... ec.add(inputs); ... ei = new double[ec.size()][]; for (int i = 0; i < ec.size(); i++) { // Changes are only below here Double[] row = ec.get(i); double[] copy = new double[row.length]; // Still, manually loop... for (int j = 0; j < row.length; j++) { copy[j] = row[j]; } ei[i] = copy; } 

3。

最后,如果您可以将Double[]更改为double[] ,则解决方案2将变为,

 public ArrayList ec; // Changed type public double[][] ei; ... ... for (int i = 0; i < ec.size(); i++) { // Simpler changes here ei[i] = ec.get(i).clone(); } ... 

基本上你需要做的就是:

 for (int i = 0; i < ec.size(); i++) { Double[] boxedRow = ec.get(i); double[] unboxedRow = new double[boxedRow.length]; for (int j = 0; j < boxedRow.length; j++) unboxedRow[j] = boxedRow[j]; ei[i] = unboxedRow; } 

你因拳击/拆箱而遇到麻烦。 将Doubles手动取消装箱为doubles允许我们将arrays转换为正确的类型。

另一种解决方案是:

 public ArrayList ec; public Double[][] ei; // no need to unbox // ... for (int i = 0; i < ec.size(); i++) { ei[i] = ec.get(i); } 

我要补充一点,你当前的解决方案不是2D ArrayList ; 它是Double数组的ArrayList 。 看起来你可能想做的事情是这样的:

 public ArrayList> ec; public double[][] ei; // ... for (int i = 0; i < ec.size(); i++) { ArrayList row = ec.get(i); Double[] rowArray = row.toArray(new Double[row.size()]); double[] unboxedRow = new double[rowArray.length]; for (int j = 0; j < rowArray.length; j++) unboxedRow[j] = rowArray[j]; ei[i] = unboxedRow; } 

再次,这可能是这样的:

 public ArrayList> ec; public Double[][] ei; // ... for (int i = 0; i < ec.size(); i++) { ArrayList row = ec.get(i); ei[i] = row.toArray(new Double[row.size()]); } 

最后,请注意,当您实例化一个新的Double[] ,数组初始化为null不是 0 。 如果您尝试以下操作,您将获得NullPointerException ,但它将进行编译。

 Double[] boxedArray = new Double[1]; double unboxed = boxedArray[0]; // equiv to "double unboxed = null;" 

拆箱时需要小心,并确保正确处理空值。

错误在以下行中:

 for (int i = 0; i < ec.size(); i++) { ArrayList row = ec.get(i); ei[i] = row.toArray(new double[row.size()]); } 

第一个错误将通过用Double[]替换ArrayList来解决:

 for (int i = 0; i < ec.size(); i++) { Double[] row = ec.get(i); ei[i] = row.toArray(new double[row.size()]); }