java 2darray_java-将2D ArrayList转换为2D Array

问题

First of all, ec here is of type ArrayList, which means ec.get(i) should return Double[] and not ArrayList.

Second, double and Double are completely different types. You can’t simply use row.toArray(new double[row.size()]) on your code.

解决方案

1.

如果您想要真正的Double的2D ArrayList,则ec的类型应为ArrayList< ArrayList< Double>>.但是因为不能使用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< Double []&gt ;,则只需更改主要部分:

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

}

...

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值