TreeMap Force关闭

时间:2014-10-20 13:35:06

标签: android linked-list treemap

我是TreeMap的新手。

我尝试过以下代码:

    for(Map.Entry<Integer,Integer> entry : MultiSelect_TreeMap.entrySet()) {
        int Books = entry.getValue();

        for(Map.Entry<Integer,Integer> entry2 : MultiSelect_TreeMap2.entrySet()) {
            int CalculatedBooks = entry2.getKey();
            int Books2 = entry2.getValue();
            int Pages = CalculatedBooks - (BooksPos * MaxPos);
            MySQLITE_DATABASE.DeleteIfExist(Books, Pages);
            if(Books == Books2) 
            {
                MultiSelect_LinkedList.add(Pages);
                MultiSelect_TreeMap.remove(CalculatedBooks);
                MultiSelect_TreeMap2.remove(CalculatedBooks);
            }
        }
    }

但我得到了力量关闭错误:

logcat的:

10-20 08:15:54.220: E/AndroidRuntime(764): FATAL EXCEPTION: main
10-20 08:15:54.220: E/AndroidRuntime(764): java.util.ConcurrentModificationException
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.util.TreeMap$MapIterator.stepForward(TreeMap.java:883)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.util.TreeMap$EntrySet$1.next(TreeMap.java:924)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.util.TreeMap$EntrySet$1.next(TreeMap.java:922)
10-20 08:15:54.220: E/AndroidRuntime(764):  at test.project.MainActivity.DeleteAllSelectedNames(MainActivity.java:1012)
10-20 08:15:54.220: E/AndroidRuntime(764):  at test.project.MainActivity$15.onClick(MainActivity.java:1274)
10-20 08:15:54.220: E/AndroidRuntime(764):  at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
10-20 08:15:54.220: E/AndroidRuntime(764):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-20 08:15:54.220: E/AndroidRuntime(764):  at android.os.Looper.loop(Looper.java:137)
10-20 08:15:54.220: E/AndroidRuntime(764):  at android.app.ActivityThread.main(ActivityThread.java:5103)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.lang.reflect.Method.invokeNative(Native Method)
10-20 08:15:54.220: E/AndroidRuntime(764):  at java.lang.reflect.Method.invoke(Method.java:525)
10-20 08:15:54.220: E/AndroidRuntime(764):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
10-20 08:15:54.220: E/AndroidRuntime(764):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-20 08:15:54.220: E/AndroidRuntime(764):  at dalvik.system.NativeStart.main(Native Method)

有人可以告诉我我做错了吗?

非常感谢

1 个答案:

答案 0 :(得分:0)

每个循环的A都无法从基础集合中删除项目。您可以手动计算indeces

for(int i = 0; i < MultiSelect_TreeMap.size(); i++){
//...
}

或使用迭代器

Iterator<Entry<Integer, Integer>> it = MultiSelect_TreeMap.entrySet().iterator();

while(it.hasNext()){
    //...
    if (condition) {
        it.remove();
    }
}

修改 我猜,你的用例实际上是什么。如果mapA中有条目,则以下代码将从两个映射中删除条目,该条目等于mapB中的条目。

Iterator<Entry<Integer,Integer>> it = mapA.entrySet().iterator();

while(it.hasNext()){
    Entry<Integer,Integer> entry = it.next();

    Integer calcBooks = entry.getKey();
    Integer books1 = entry.getValue();

    if(mapB.containsKey(calcBooks)){

        Integer books2 = mapB.get(calcBooks);

        if(books1.equals(books2)){
            mapB.remove(entry);
            it.remove();
        }
    }               
}

EDIT2:类强制转换错误与第一个问题没有直接关系。树形图通过比较来组织元素的顺序。因此,树的元素需要Comparator