国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Java > 正文

java.util.List.subList

2019-11-06 08:21:06
字體:
供稿:網(wǎng)友

原文鏈接: http://www.cnblogs.com/gaojing/archive/2012/06/17/java-list-sublist-caution.html

java.util.List中有一個(gè)subList方法,用來返回一個(gè)list的一部分的視圖。

List<E> subList(int fromIndex, int toIndex);

它返回原來list的從[fromIndex, toIndex)之間這一部分的視圖,之所以說是視圖,是因?yàn)閷?shí)際上,返回的list是靠原來的list支持的。

所以,你對原來的list和返回的list做的“非結(jié)構(gòu)性修改”(non-structural changes),都會影響到彼此對方。

所謂的“非結(jié)構(gòu)性修改”,是指不涉及到list的大小改變的修改。相反,結(jié)構(gòu)性修改,指改變了list大小的修改。

 

那么,如果涉及到結(jié)構(gòu)性修改會怎么樣呢?

如果發(fā)生結(jié)構(gòu)性修改的是返回的子list,那么原來的list的大小也會發(fā)生變化;

而如果發(fā)生結(jié)構(gòu)性修改的是原來的list(不包括由于返回的子list導(dǎo)致的改變),那么返回的子list語義上將會是undefined。在AbstractList(ArrayList的父類)中,undefined的具體表現(xiàn)形式是拋出一個(gè)ConcurrentModificationException。

因此,如果你在調(diào)用了sublist返回了子list之后,如果修改了原list的大小,那么之前產(chǎn)生的子list將會失效,變得不可使用。

 

tips: 如何刪除一個(gè)list的某個(gè)區(qū)段,比如刪除list的第2-5個(gè)元素?

方法是: 可以利用sublist的幕后還是原來的list的這個(gè)特性,比如

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);    }

一個(gè)很有趣的思考:如何最高效的實(shí)現(xiàn)一個(gè)list的split方法?

參見:http://stackoverflow.com/questions/379551/java-split-a-list-into-two-sub-lists。

        /** Split a list into two sublists. The original list will be modified to         * have size i and will contain exactly the same elements at indices 0          * through i-1 as it had originally; the returned list will have size          * len-i (where len is the size of the original list before the call)          * and will have the same elements at indices 0 through len-(i+1) as          * the original list had at indices i through len-1.         */        <T> List<T> split(List<T> list, int i){            List sub=list.subList(0,i);            List two=new ArrayList(sub);            sub.clear(); // since sub is backed by one, this removes all sub-list items from one        }


發(fā)表評論 共有條評論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 饶河县| 旅游| 项城市| 石嘴山市| 阿克苏市| 泰和县| 禹州市| 华容县| 昆明市| 珲春市| 栾城县| 政和县| 雷波县| 太仆寺旗| 行唐县| 白河县| 大兴区| 莫力| 合水县| 贵阳市| 吉安县| 永新县| 广南县| 大埔区| 四子王旗| 澎湖县| 怀化市| 安远县| 汉源县| 绥德县| 环江| 建湖县| 民县| 义马市| 南召县| 长寿区| 沙雅县| 拜泉县| 察隅县| 高阳县| 友谊县|