【发布时间】:2011-12-11 16:45:27
【问题描述】:
我正在尝试创建一种从二维数组中删除重复项的方法。外部数组包含点索引,内部数组包含它们的坐标。看起来我必须使用 arraylist 才能删除元素,而不会在数组中得到空值。然后我想将数组列表转换回二维数组,以便以我需要的格式返回它。问题是 arraylsit 包含一个对象数组,所以我不能将它转换为为浮点数设计的数组。从我的数组列表中过滤浮点数的正确语法是什么。我的代码如下:
public class rem_duplicates {
public float [][] rem_geo_duplicates(float a[][]){
ArrayList<float[]> al = new ArrayList<float[]>();
float no_points = a.length;
int count = 0;
for (int i = 0; i < no_points-1; i++){
if((a[i][0] == a[i+1][0])&&(a[i][1] == a[i+1][1])){
a[i] = null;
count ++;
}
for (int j = 0; j < no_points; j++){
if (a[j] != null){
al.add(a[j]);
}
}
//how do i get the arraylist 'al' into this array b[][]?
float b[][] = new float [a.length-count][3];
b = al.toArray();
}
}
return b
}
【问题讨论】:
标签: java arrays arraylist indexing duplicates