取list中某一段连续元素

import java.util.List;

public class ListUtils {
    /**
     * 取list中某一段连续元素
     *
     * @param list
     * @param beginIndex
     * @param endIndex
     * @return
     */
    public static <T> List<T> fetchElementFromList(List<T> list, int beginIndex, int endIndex) {
        List<T> result = null;
        if (null != list && !list.isEmpty()) {
            int size = list.size();
            int fromIndex = beginIndex < 0 ? 0 : beginIndex;
            int toIndex = endIndex > size ? size : endIndex;
            if (fromIndex < toIndex) {
                result = list.subList(fromIndex, toIndex);
            }
        }
        return result;
    }
}

 

posted @ 2023-06-26 17:22  一生爱你  阅读(90)  评论(0编辑  收藏  举报