正确截取List指定位置的内容

正确截取List指定位置的内容

import java.util.ArrayList;
import java.util.List;

public class ListUtils {
    public static <T> List<T> getSubList(List<T> list, int fromIndex, int toIndex) {
        //list.stream().limit(toIndex);
        if (toIndex > list.size()) {
        }
        List listClone = list;
        List sub = listClone.subList(fromIndex, toIndex);
        List two = new ArrayList(sub);
        sub.clear(); // since sub is backed by one, this removes all sub-list items from one
        return two;
    }

}

posted @ 2018-04-24 09:22  西北野狼  阅读(9120)  评论(0编辑  收藏  举报