Here you can find the source of copyOfRange(List
Parameter | Description |
---|---|
T | Type of list elements |
list | Basic list for operation |
from | Start-index (inclusive) for copy operation |
to | End-index (inclusive) for copy operation |
list
starting at index from
and ending at index to
public static <T> List<T> copyOfRange(List<T> list, int from, int to)
//package com.java2s; //License from project: Open Source License import java.util.ArrayList; import java.util.List; public class Main { /**//from w w w . j a v a2 s.c o m * Returns a new list containing all elements of the original list with an * index in [from;to] * * @param <T> * Type of list elements * @param list * Basic list for operation * @param from * Start-index (inclusive) for copy operation * @param to * End-index (inclusive) for copy operation * @return The sublist of <code>list</code> starting at index * <code>from</code> and ending at index <code>to</code> */ public static <T> List<T> copyOfRange(List<T> list, int from, int to) { if (from < 0 || from >= list.size() || to < 0 || to >= list.size() || from > to) throw new IllegalArgumentException("Illegal extraction bounds"); List<T> result = new ArrayList<>(to - from + 1); for (int i = from; i <= to; i++) result.add(list.get(i)); return result; } }