Java List Sub List subList(final List oriList, int[] indexes)

Here you can find the source of subList(final List oriList, int[] indexes)

Description

Copies the elements at the indexes positions

License

Open Source License

Parameter

Parameter Description
E a parameter
oriList a parameter
indexes a parameter

Declaration

public static <E> List<E> subList(final List<E> oriList, int[] indexes) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//w  w  w  . j a  va  2  s. c  o m
     * Copies the elements at the indexes positions
     *
     * @param <E>
     * @param oriList
     * @param indexes
     * @return
     */
    public static <E> List<E> subList(final List<E> oriList, int[] indexes) {
        List<E> list = new ArrayList<E>(indexes.length);
        for (int index : indexes) {
            if (index >= 0 && index < oriList.size()) {
                list.add(oriList.get(index));
            }
        }
        return list;
    }

    /**
     * Copies all the elements form start (excluded) to end (included)
     *
     * @param <E>
     * @param oriList
     * @param start
     * @param end
     * @return The list with the elements
     */
    public static <E> List<E> subList(final List<E> oriList, int start, int end) {
        if (start < 0 || end >= oriList.size()) {
            return null;
        }
        List<E> list = new ArrayList<E>(end - start + 1);
        for (int i = start; i <= end; i++) {
            list.add(oriList.get(i));
        }
        return list;
    }
}

Related

  1. splitListToSubLists(List parentList, int subListSize)
  2. startsWith(final List list, final List subList)
  3. sub(List l, int index)
  4. subArray(List listA, List listB)
  5. subList(final Iterable

    parent, final Iterable child)

  6. subList(final List list, final int first, final int count)
  7. subList(final List list, final int offset, final int amount)
  8. subList(final List list, final int startIndex, final int endIndex)
  9. subList(final List source, final int... indices)