【发布时间】:2011-12-14 17:32:57
【问题描述】:
我正在尝试从 3 维数组中删除项目。
我知道最好的方法之一是将数组转换为列表,并在遍历原始数组的同时从列表中删除这些项目,然后将列表转换回数组并返回。
我试过了,但返回数组时出现类型不匹配。我怀疑从数组转换为列表时我没有对维度做任何事情。
建议?
import java.util.List;
import java.util.Arrays;
public class RepatitionRemoval {
public float[][][] process(float[][][] data) {
//this method with step through all of the the strokes and remove
//points which occupy the same position. This should help with time
//warping regconition
//Change the array to a list
List points = Arrays.asList(data);
for (int i = 0; i < data.length; i++) {
for (int j = 0; j < data[i].length; j++) {
for (int k = 0; k < data[i][j].length-1; k++) {
//if the current coordinate is the same as the one next
//then remove it
if (data[i][j][k] == data[i][j][k+1]) {
points.remove(data[i][j][k]);
}
}
}
}
float[][][] returnData = points.toArray();
return returnData;
}
}
【问题讨论】:
-
toArray()返回一个Object[],因此您不能将其影响到float[][][]。 -
@talnicolas:好吧,如果他投射它,他可以......但无论如何这不是 OP 的问题:他试图做的事情不会奏效,因为他创建的 List由 float[][][] 支持,因此如果他尝试删除现有元素会抛出 UnsupportedOperationException (但即使在那里“删除”的代码也是坏了,因为他试图从 float[][] 列表中删除 Float)。看我的回答:)