[英]Converting 2D ArrayList to 2D Array
我已經看過幾個答案,但我發現的答案有錯誤。 我正在嘗試將 Doubles[] 的 ArrayList 轉換為普通的雙二維數組。 我的代碼:
public ArrayList<Double[]> ec;
public double[][] ei;
...
encogCorpus = new ArrayList<Double[]>();
...
ec.add(inputs);
...
ei = new double[ec.size()][];
for (int i = 0; i < ec.size(); i++) {
ArrayList<Double> row = ec.get(i);
ei[i] = row.toArray(new double[row.size()]);
}
我收到錯誤說
類型不匹配:無法從 Double[] 轉換為 ArrayList
和
ArrayList 類型中的 toArray(T[]) 方法不適用於參數 (double[])
- 首先,這里的
ec
是ArrayList<Double[]>
,這意味着ec.get(i)
應該返回Double[]
而不是ArrayList<Double>
。- 其次,
double
和Double
是完全不同的類型。 您不能簡單地在代碼中使用row.toArray(new double[row.size()])
。
如果你想要一個真正的Doubles
二維ArrayList
,那么ec
的類型應該是ArrayList<ArrayList<Double>>
。 但是因為我們不能使用toArray()
,所以我們手動循環。
public ArrayList<ArrayList<Double>> ec; // line changed here
public double[][] ei;
...
encogCorpus = new ArrayList<ArrayList<Double>>(); // and here also
...
ec.add(inputs); // `inputs` here should be of type `ArrayList<Double>`
...
ei = new double[ec.size()][];
for (int i = 0; i < ec.size(); i++) {
ArrayList<Double> 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;
}
但是如果你堅持使用ArrayList<Double[]>
,我們只需要改變主要部分:
public ArrayList<Double[]> ec;
public double[][] ei;
...
encogCorpus = new ArrayList<Double[]>();
...
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;
}
最后,如果您可以將Double[]
更改為double[]
,則解決方案2將變為,
public ArrayList<double[]> 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
允許我們將數組轉換為正確的類型。
另一種解決方案是:
public ArrayList<Double[]> 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<ArrayList<Double>> ec;
public double[][] ei;
// ...
for (int i = 0; i < ec.size(); i++) {
ArrayList<Double> 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<ArrayList<Double>> ec;
public Double[][] ei;
// ...
for (int i = 0; i < ec.size(); i++) {
ArrayList<Double> 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<Double> row = ec.get(i);
ei[i] = row.toArray(new double[row.size()]);
}
第一個錯誤將通過用Double[]
替換ArrayList<Double>
來解決:
for (int i = 0; i < ec.size(); i++) {
Double[] row = ec.get(i);
ei[i] = row.toArray(new double[row.size()]);
}
List<double[]> list = Arrays.asList(new double[]{1.1, 2.0}, new double[]{3.0, 5.8});
double[][] res = list.toArray(new double[list.size()][]);
// System.out.println(Arrays.deepToString(res));
將 ArrayList 轉換為 2d int 數組的方法相同。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.