zoukankan      html  css  js  c++  java
  • List subList()的一个demo

    只要保证
    toIndex不大于size
    并且
    fromIndex不大于toIndex即可
    (谁会传一个负数的下标呢?)

        public List<E> subList(int fromIndex, int toIndex) {
            subListRangeCheck(fromIndex, toIndex, size);
            return new SubList(this, 0, fromIndex, toIndex);
        }
    
        static void subListRangeCheck(int fromIndex, int toIndex, int size) {
            if (fromIndex < 0)
                throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
            if (toIndex > size)
                throw new IndexOutOfBoundsException("toIndex = " + toIndex);
            if (fromIndex > toIndex)
                throw new IllegalArgumentException("fromIndex(" + fromIndex +
                                                   ") > toIndex(" + toIndex + ")");
        }
        public static void main(String[] args) {
            ArrayList<String> list = new ArrayList<>();
            list.add("1");
            int size = list.size();
            System.out.println(size);
            System.out.println(list.subList(0, size));
            System.out.println(list.subList(size, size));
        }
    1
    [1]
    []
    import org.junit.Test;
    
    import java.util.ArrayList;
    import java.util.List;
    
    import static org.hamcrest.MatcherAssert.assertThat;
    import static org.hamcrest.core.Is.is;
    
    /**
     * Created by MyWorld on 2016/1/21.
     */
    public class SubListDemoTest {
    
        @Test
        public void ListSizeShouldEquals1_WhenSourceList9Step5() {
            List<Integer> sourceList = new ArrayList<Integer>();
            for (int i = 0; i < 9; i++) {
                sourceList.add(i);
            }
            int step = 5;
            List<String> targetList = SubListDemo.split(sourceList, step);
            assertThat(targetList.size(), is(2));
        }
    
        @Test
        public void ListSizeShouldEquals2_WhenSourceList10Step5() {
            List<Integer> sourceList = new ArrayList<Integer>();
            for (int i = 0; i < 10; i++) {
                sourceList.add(i);
            }
            int step = 5;
            List<String> targetList = SubListDemo.split(sourceList, step);
            assertThat(targetList.size(), is(2));
        }
    
        @Test
        public void ListSizeShouldEquals3_WhenSourceList11Step5() {
            List<Integer> sourceList = new ArrayList<Integer>();
            for (int i = 0; i < 11; i++) {
                sourceList.add(i);
            }
            int step = 5;
            List<String> targetList = SubListDemo.split(sourceList, step);
            assertThat(targetList.size(), is(3));
        }
    
    
    }
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * Created by MyWorld on 2016/1/21.
     */
    public class SubListDemo {
        public static List<String> split(List<Integer> list, int step) {
            List<String> listStr = new ArrayList<String>();
            int size = list.size();
            int times = size / step + (size % step == 0 ? 0 : 1);
            for (int i = 0; i < times; i++) {
                int fromIndex = i * step;
                int toIndex = (i + 1) * step;
                listStr.add(list.subList(fromIndex, (toIndex < size ? toIndex : size)).toString());
            }
            return listStr;
        }
    }

     
    tips:

    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

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

  • 相关阅读:
    sql分页(收藏)
    检索 COM 类工厂中 CLSID 为 {000209FF00000000C00000000, 80070005, 8000401a, asp.net生成word服务器部署, DCOM, asp.net 导出word格式的数据
    根据模板生成word文档《转》
    自动执行SQL脚本<codesmith>
    C#操作Word模板文件《收藏》
    Jquery表单验证插件《转》
    关闭文档时总是提示Normal.dot文件被占用《转》
    RepeaterItem
    NET Repeater控件使用
    文件的上传下载《转》
  • 原文地址:https://www.cnblogs.com/softidea/p/5147087.html
Copyright © 2011-2022 走看看