当我排序List它的迭代器会发生什么? [英] When I sort a List what happens to its iterators?

查看:151
本文介绍了当我排序List它的迭代器会发生什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我对该列表有 a List 对象迭代器

Let's say I have a List object and an iterator for that list.

现在我用 java.util.Collections.sort()


  • 迭代器会发生什么?

  • 其行为是否仍然定义,是否仍可使用?

  • What happens to the iterator?
  • Is its behavior still defined and can it still be used?
  • If not, can I prevent destroying the iterators for the list?

我知道,这个问题可以通过改变程序设计,克隆列表例如,但我特别想知道Java的官方行为绕过。

I know, this problem could be circumvented by changing the program design, cloning the list for example, but I specificly want to know the "official" behavior of Java.

推荐答案

java.util 中的大多数集合都是fail-fast,可能抛出 ConcurrentModificationException 如果底层集合被改变。应该指出,这是为了调试,所以不能保证。根据javadoc,这适用于的所有后果 AbstractList ,但这是不 /docs/api/java/util/concurrent/CopyOnWriteArrayList.htmlrel =nofollow noreferrer> CopyOnWriteArrayList ,用于多线程使用。

Most of the collections in java.util are "fail-fast" and may throw a ConcurrentModificationException if the underlying collection is changed. It should be pointed out that this is intended for debugging and so is not guaranteed. According to the javadocs, this is true of all decedents of AbstractList, but this is not true of CopyOnWriteArrayList, which is intended for multi-threaded use.

这篇关于当我排序List它的迭代器会发生什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆