Java List Sub List copySubList(List list, int fromIndex, int toIndex)

Here you can find the source of copySubList(List list, int fromIndex, int toIndex)

Description

Unlike List.subList(), which returns a live view of a set of List elements, this method returns a new copy of the list.

License

Open Source License

Parameter

Parameter Description
list The source List
fromIndex Starting index in the source List
toIndex Ending index in the source List

Exception

Parameter Description
IndexOutOfBoundsException Call exceeds the bounds of the source List
IllegalArgumentException fromIndex and toIndex are not in sequence

Return

a copy of the selected range

Declaration

public static <T> List<T> copySubList(List<T> list, int fromIndex, int toIndex) 

Method Source Code

//package com.java2s;
/**/*from w  w w  .j a v  a2 s.c om*/
 * Copyright 2005-2014 Restlet
 * 
 * The contents of this file are subject to the terms of one of the following
 * open source licenses: Apache 2.0 or or EPL 1.0 (the "Licenses"). You can
 * select the license that you prefer but you may not use this file except in
 * compliance with one of these Licenses.
 * 
 * You can obtain a copy of the Apache 2.0 license at
 * http://www.opensource.org/licenses/apache-2.0
 * 
 * You can obtain a copy of the EPL 1.0 license at
 * http://www.opensource.org/licenses/eclipse-1.0
 * 
 * See the Licenses for the specific language governing permissions and
 * limitations under the Licenses.
 * 
 * Alternatively, you can obtain a royalty free commercial license with less
 * limitations, transferable or non-transferable, directly at
 * http://restlet.com/products/restlet-framework
 * 
 * Restlet is a registered trademark of Restlet S.A.S.
 */

import java.util.ArrayList;
import java.util.List;

public class Main {
    /**
     * Unlike List.subList(), which returns a live view of a set of List
     * elements, this method returns a new copy of the list. List.subList() is
     * not available in GWT 1.5 and was removed on purpose.
     * 
     * @param list
     *            The source List
     * @param fromIndex
     *            Starting index in the source List
     * @param toIndex
     *            Ending index in the source List
     * @throws IndexOutOfBoundsException
     *             Call exceeds the bounds of the source List
     * @throws IllegalArgumentException
     *             fromIndex and toIndex are not in sequence
     * @return a copy of the selected range
     */
    public static <T> List<T> copySubList(List<T> list, int fromIndex, int toIndex) {
        if (fromIndex < 0)
            throw new IndexOutOfBoundsException("fromIndex = " + fromIndex);
        if (toIndex > list.size())
            throw new IndexOutOfBoundsException("toIndex = " + toIndex);
        if (fromIndex > toIndex)
            throw new IllegalArgumentException("fromIndex(" + fromIndex + ") > toIndex(" + toIndex + ")");
        ArrayList<T> subList = new ArrayList<T>();
        for (int i = fromIndex; i <= toIndex; i++) {
            subList.add(list.get(i));
        }
        return subList;
    }
}

Related

  1. copySubList(final List list, final int fromIndex, final int toIndex)
  2. divideListInSublistsOfNSize(List list, int n)
  3. getIndexesId(List aList, List aSubList)
  4. getIndicesOfItems(List superList, List sublist)
  5. getRandomSubList(List inputList, double percentage)