简体   繁体   English

将 2D ArrayList 转换为 2D Array

[英]Converting 2D ArrayList to 2D Array

I've looked at several answers already, and I am getting errors with the answers that I've found.我已经看过几个答案,但我发现的答案有错误。 I'm trying to convert an ArrayList of Doubles[] to a normal double 2D array.我正在尝试将 Doubles[] 的 ArrayList 转换为普通的双二维数组。 My code:我的代码:

    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()]);
    }

I'm getting errors that say我收到错误说

Type mismatch: cannot convert from Double[] to ArrayList类型不匹配:无法从 Double[] 转换为 ArrayList

And

The method toArray(T[]) in the type ArrayList is not applicable for the arguments (double[]) ArrayList 类型中的 toArray(T[]) 方法不适用于参数 (double[])

Problems问题

  1. First of all, ec here is of type ArrayList<Double[]> , which means ec.get(i) should return Double[] and not ArrayList<Double> .首先,这里的ecArrayList<Double[]> ,这意味着ec.get(i)应该返回Double[]而不是ArrayList<Double>
  2. Second, double and Double are completely different types.其次, doubleDouble是完全不同的类型。 You can't simply use row.toArray(new double[row.size()]) on your code.您不能简单地在代码中使用row.toArray(new double[row.size()])

Solutions解决方案

1. 1.

If you want a true 2D ArrayList of Doubles then the type of ec should be ArrayList<ArrayList<Double>> .如果你想要一个真正的Doubles二维ArrayList ,那么ec的类型应该是ArrayList<ArrayList<Double>> But because we can't use toArray() , we manually loop instead.但是因为我们不能使用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;
}

2. 2.

But if you insist of using ArrayList<Double[]> , we only need to change the main part:但是如果你坚持使用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;
}

3. 3.

Finally, if you could change Double[] to double[] , solution 2 would become,最后,如果您可以将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();
}
...

Basically all you need to do is this:基本上你需要做的就是:

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;
}

You're having trouble because of boxing / unboxing.由于装箱/拆箱,您遇到了麻烦。 Manually unboxing the Doubles to doubles allows us to convert the array to the right type.手动将Doubles拆箱为doubles允许我们将数组转换为正确的类型。

An alternate solution would be this:另一种解决方案是:

public ArrayList<Double[]>  ec;
public Double[][]           ei; // no need to unbox

// ...

for (int i = 0; i < ec.size(); i++) {
    ei[i] = ec.get(i);
}

I'll add that your current solution is not a 2D ArrayList ;我要补充一点,您当前的解决方案不是 2D ArrayList it is an ArrayList of Double arrays.它是一个Double数组的ArrayList It looks like what you might be trying to do is something like this:看起来您可能正在尝试做的是这样的:

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;
}

Which, again, could potentially be this:同样,这可能是这样的:

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()]);
}

Finally, please note that when you instantiate a new Double[] , the arrays initialize to null and not to 0 .最后,请注意,当您实例化一个新的Double[] ,数组会初始化为null不是0 If you try the following, you'll get a NullPointerException , though it will compile.如果您尝试以下操作,您会得到一个NullPointerException ,尽管它会编译。

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

You need to be careful when unboxing, and make sure that you are handling nulls correctly.拆箱时需要小心,并确保正确处理空值。

Error is in the following lines:错误在以下几行:

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

The first error will be resolved by replacing ArrayList<Double> with Double[] :第一个错误将通过用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));

Same approach to convert ArrayList to 2d int array.将 ArrayList 转换为 2d int 数组的方法相同。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM