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將會失效,變得不可使用。
刪除一個list的某個區段,比如刪除list的第2-5個元素?
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或者string長時間存放在內存中:
1. 對於sublist()方法得到的list,貌似沒有太好的辦法,只能用最直接的方式:自己創建新的list,然后將需要的內容添加進去
2. 對於substring()/split()方法得到的string,可以用String類的構造函數new String(String original)來創建一個新的String,這 樣會重新創建底層的char[]並復制需要的內容,不會造成"浪費"。
String類的構造函數new String(String original)是一個非常特別的構造函數,通常沒有必要使用,正如這個函數的javadoc所言 :Unless an explicit copy of original is needed, use of this constructor is unnecessary since Strings are immutable. 除非明確需要原始字符串的拷貝,否則沒有必要使用這個構造函數,因為String是不可變的。