ConcurrentModificationExcrption当我清除主列表后的子列表时,为什么以下代码抛出,但如果我清除子列表然后清除主列表则不会?
ArrayList<Integer> masterList = new ArrayList<Integer>();
List<Integer> subList;
// Add some values to the masterList
for (int i = 0; i < 10; i++) {
masterList.add(i * i);
}
// Extract a subList from the masterList
subList = masterList.subList(5, masterList.size() - 1);
// The below throws ConcurrentModificationException
masterList.clear();
subList.clear(); // Exception thrown in this line
// The below doesn't throw any exception
subList.clear();
masterList.clear(); // No exception thrown. Confused??
Run Code Online (Sandbox Code Playgroud) 码:
var list = new List<KeyValuePair<int, string[]>>();
list .Add(new KeyValuePair<int, string[]>(1, new string[] { "1", "One"}));
list .Add(new KeyValuePair<int, string[]>(2, new string[] { "2", "Two"}));
list .Add(new KeyValuePair<int, string[]>(3, new string[] { "3", "Three"}));
list .Add(new KeyValuePair<int, string[]>(4, new string[] { "4", "Four"}));
Run Code Online (Sandbox Code Playgroud)
需要仅选择KeyValuePairs列表中的值并构建数组.我试过这个.
var values = list.Select(v => v.Value.ToList()).ToArray();
Run Code Online (Sandbox Code Playgroud)
期待string[]这样的.
{"1", "One", "2", "Two", "3", "Three", "4", "Four"}
Run Code Online (Sandbox Code Playgroud)
但它会返回List<string>[]这样的
{{"1", "One"}, {"2", "Two"}, {"3", "Three"}, {"4", "Four"}}
Run Code Online (Sandbox Code Playgroud)
也试过了
var values = list.Select(v => v.Value.ToArray()).ToArray();
Run Code Online (Sandbox Code Playgroud)
但它回来了 …