【发布时间】:2012-04-01 14:08:20
【问题描述】:
我在使用迭代器从数组列表中删除项目时遇到问题。我的目标是检索一定半径内的点并将它们聚集成一组组。我使用初始点作为参考。代码有一个初始的 for 循环,它将贯穿每个地方,然后为每个地方创建一个内部 for 循环来检查参考地方和其余地方之间的半径。如果参考地点和其他地点之间的半径小于我设置的阈值,它将被添加到与其他相似点分组的数组列表中。随着它们被添加到组中,它们将从原始数组列表中删除。
但是我遇到了一些问题,例如它要么只执行一次外部 for 循环,要么我得到一个 IllegalStateException。
代码如下:
HashMap<Place, ArrayList<Place>> sets = new HashMap<Place, ArrayList<Place>>();
private void cluster(ArrayList<Place> places) {
for (Iterator<Place> iterator = places.iterator(); iterator.hasNext();) {
Place pl = iterator.next();
ArrayList<Place> subset = new ArrayList<Place>(); // Group
GeoPoint g = new GeoPoint((int) (pl.getGeometry().getLocation()
.getLat() * 1e6), (int) (pl.getGeometry().getLocation()
.getLng() * 1e6));
Point point = new Point();
mapView.getProjection().toPixels(g, point);
sets.put(pl, subset);
subset.add(pl);
iterator.remove();
for (Iterator<Place> iterator2 = places.iterator(); iterator2
.hasNext();) {
Place pl2 = iterator2.next();
int threshold = 100;
GeoPoint g2 = new GeoPoint((int) (pl2.getGeometry()
.getLocation().getLat() * 1e6), (int) (pl2
.getGeometry().getLocation().getLng() * 1e6));
Point point2 = new Point();
mapView.getProjection().toPixels(g2, point);
int dx = Math.abs(point2.x - point.x);
int dy = Math.abs(point2.y - point.y);
if (dx < threshold && dy < threshold) {
subset.add(pl2);
iterator2.remove();
}
}
}
}
抱歉信息过载,非常感谢您的帮助。
提前谢谢大家
热汉
【问题讨论】:
标签: java android google-maps arraylist iterator