Java tutorial
//package com.java2s; //License from project: Open Source License import java.util.List; public class Main { /** * Gets a sublist of a list, truncated at the end of the list if too many elements are selected. * This behaves exactly like List.subList, including all notes in its Javadoc concerning * structural modification of the backing List, etc. with one difference: if the end index is * beyond the end of the list, instead of throwing an exception, the sublist simply stops at the * end of the list. After the fifth or so time writing this idiom, it seems worth having a * function for. :-) */ public static <T> List<T> truncatedSubList(List<T> inList, int start, int end) { // List.sublist will do our error checking for us final int limit = Math.min(end, inList.size()); return inList.subList(start, limit); } }