Java examples for java.util:List Sub List
Safe sub list operation
//package com.book2s; import java.util.ArrayList; import java.util.Collection; import java.util.List; public class Main { public static void main(String[] argv) { List list = java.util.Arrays.asList("asdf", "book2s.com"); int start = 42; int end = 42; System.out.println(sub(list, start, end)); }/* w ww . ja v a 2s. co m*/ public static <T> List<T> sub(List<T> list, int start, int end) { if (list == null || list.isEmpty()) { return null; } if (start < 0) { start = 0; } if (end < 0) { end = 0; } if (start > end) { int tmp = start; start = end; end = tmp; } final int size = list.size(); if (end > size) { if (start >= size) { return null; } end = size; } return list.subList(start, end); } public static <T> List<T> sub(Collection<T> list, int start, int end) { if (list == null || list.isEmpty()) { return null; } return sub(new ArrayList<T>(list), start, end); } public static <T> boolean isEmpty(T[] array) { return array == null || array.length == 0; } public static <T> boolean isEmpty(Collection<T> collection) { return collection == null || collection.isEmpty(); } }