Java List Copy copyOfRange(List list, int from, int to)

Here you can find the source of copyOfRange(List list, int from, int to)

Description

Returns a new list containing all elements of the original list with an index in [from;to]

License

Open Source License

Parameter

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

Return

The sublist of list starting at index from and ending at index to

Declaration

public static <T> List<T> copyOfRange(List<T> list, int from, int to) 

Method Source Code

//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;
    }
}

Related

  1. copyNullable(List original)
  2. copyNullSafeMutableList(Collection list)
  3. copyObjectList(List objects)
  4. copyOf(List list)
  5. copyOf(List values)
  6. copyProbabilities(List targetProbs, List destinationProbs)
  7. copyQueryFilters(List queryFilters)
  8. copySafelyToObjectList(java.util.List list)
  9. copyStringList(List l1)

  10. HOME | Copyright © www.java2s.com 2016