subList 内部用了跟 iterator 类似的机制来保证自己看到的父 list 的结构是正确的。根据 List 的 subList 方法的协议: The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. 即假如父 list 的结构在其 subList 不知情的情况下发生了改变,那么这个 subList 所有的语义都变成“未定义”,换句话说它本来对遵守List接口协议的保证都作废。你在 list.subList(3, 8).clear(); 这一句另建了一个subList对list的结构做了改变,就已经导致原来的 subList 不可用。然后你又 System.out.println(subList); 实际上调用了其 toString 方法,toString 方法内又试图对其迭代,所以报错。
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. 即假如父 list 的结构在其 subList 不知情的情况下发生了改变,那么这个 subList 所有的语义都变成“未定义”,换句话说它本来对遵守List接口协议的保证都作废。你在
list.subList(3, 8).clear();
这一句另建了一个subList对list的结构做了改变,就已经导致原来的 subList 不可用。然后你又
System.out.println(subList);
实际上调用了其 toString 方法,toString 方法内又试图对其迭代,所以报错。