java.util.List中有一個subList方法,用來返回一個list的一部分的視圖。
List<E> subList(int fromIndex, int toIndex);
它返回原來list的從[fromIndex, toIndex)之間這一部分的視圖,之所以說是視圖,是因為實際上,返回的list是靠原來的list支持的。
所以,你對原來的list和返回的list做的“非結構性修改”(non-structural changes),都會影響到彼此對方。
所謂的“非結構性修改”,是指不涉及到list的大小改變的修改。相反,結構性修改,指改變了list大小的修改。
那么,如果涉及到結構性修改會怎么樣呢?
如果發生結構性修改的是返回的子list,那么原來的list的大小也會發生變化;
而如果發生結構性修改的是原來的list(不包括由於返回的子list導致的改變),那么返回的子list語義上將會是undefined。在AbstractList(ArrayList的父類)中,undefined的具體表現形式是拋出一個ConcurrentModificationException。
因此,如果你在調用了sublist返回了子list之后,如果修改了原list的大小,那么之前產生的子list將會失效,變得不可使用。
tips: 如何刪除一個list的某個區段,比如刪除list的第2-5個元素?
方法是: 可以利用sublist的幕后還是原來的list的這個特性,比如
list.subList(from, to).clear();
這樣就可以了。
示例代碼:
public static void main(String[] args) { List<String> parentList = new ArrayList<String>(); for(int i = 0; i < 5; i++){ parentList.add(String.valueOf(i)); } List<String> subList = parentList.subList(1, 3); for(String s : subList){ System.out.println(s);//output: 1, 2 } //non-structural modification by sublist, reflect parentList subList.set(0, "new 1"); for(String s : parentList){ System.out.println(s);//output: 0, new 1, 2, 3, 4 } //structural modification by sublist, reflect parentList subList.add(String.valueOf(2.5)); for(String s : parentList){ System.out.println(s);//output:0, new 1, 2, 2.5, 3, 4 } //non-structural modification by parentList, reflect sublist parentList.set(2, "new 2"); for(String s : subList){ System.out.println(s);//output: new 1, new 2 } //structural modification by parentList, sublist becomes undefined(throw exception) parentList.add("undefine"); // for(String s : subList){ // System.out.println(s); // } // subList.get(0); }
一個很有趣的思考:如何最高效的實現一個list的split方法?
參見:http://stackoverflow.com/questions/379551/java-split-a-list-into-two-sub-lists。