【问题标题】:Removing objects from a list and iterator problems从列表中删除对象和迭代器问题
【发布时间】: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


    【解决方案1】:

    您正在将外部迭代器步进到内部循环中。看起来这些行可能有误:

    for (Iterator<Place> iterator2 = places.iterator(); iterator.hasNext();) {
        Place pl2 = iterator.next();
    

    看起来您可能在没有更改循环和以下行中的终止条件的情况下复制和粘贴:

    for (Iterator<Place> iterator2 = places.iterator(); iterator2.hasNext();) {
           Place pl2 = iterator2.next();
    

    【讨论】:

    • 是的,对此很抱歉,我刚刚注意到它,我也刚刚对其进行了编辑。是的,我修复了非法状态异常,谢谢。但现在我得到了一个 ConcurrentModification 异常。 :S 问题是我希望两个 for 循环都遍历同一个列表,并且内部 for 循环也可能像删除位置一样编辑位置列表,这是不允许的吗?
    • 你能解释一下你想通过从两个地方的同一个列表中删除点来完成什么吗?如果在外部循环迭代时从内部循环中删除项目,您似乎会遇到一些问题。另外,在启动内部循环之前调用iterator.remove() 的原因是什么?
    • 我的目标是根据那里的半径将地点组合在一起,因此外循环将是哈希图(参考点)的键,内循环添加其半径内的地点列表,因此为什么将其添加到列表中。我调用了 iterator.remove() 以便在执行内部循环时找不到正在使用的参考点
    • 我在内部循环期间删除位置,因为如果它找到一个在参考位置点半径内的位置,它将不会在以后用作参考点,因为它已经在 hashmap 中分组为价值。 hashmap 中的键是不在其他参考点半径范围内且尚未分组的点。
    【解决方案2】:

    不允许在同一个列表上运行多个迭代器。所以你会得到一个并发修改异常。最好复制数组列表,在内部循环中更新要更新的副本位置的数据。总之,改变你的逻辑

    【讨论】:

      【解决方案3】:

      我猜你得到了异常,因为你在迭代时更改了列表。

      一开始,我会使用for (... in ...) 构造。

      其次,我认为您必须复制 places 并对其进行迭代,但要从 places 中删除。

      【讨论】:

        猜你喜欢
        • 2017-02-10
        • 1970-01-01
        • 2016-12-02
        • 1970-01-01
        • 2019-02-12
        • 2011-04-02
        • 2011-02-27
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多