Arrays.copyOfRange支持:boolean[], byte[] ,char[],double[],float[],int[],long[]以及泛型的 T[]

import java.util.Arrays;

public class hello {
    public static void main(String[] args) {
        int[] src = new int[]{1, 2, 3, 4, 5};
        int newArray[] = Arrays.copyOfRange(src, 0, 2);
        for (int i : newArray) {


public static <T> T[] copyOfRange(T[] original,
                                  int from,
                                  int to)
Copies the specified range of the specified array into a new array. The initial index of the range (from) must lie between zero and original.length, inclusive. The value at original[from] is placed into the initial element of the copy (unless from == original.length or from == to). Values from subsequent elements in the original array are placed into subsequent elements in the copy. The final index of the range (to), which must be greater than or equal to from, may be greater than original.length, in which case null is placed in all elements of the copy whose index is greater than or equal to original.length - from. The length of the returned array will be to - from.
The resulting array is of exactly the same class as the original array.

	original - the array from which a range is to be copied
from - the initial index of the range to be copied, inclusive
to - the final index of the range to be copied, exclusive. (This index may lie outside the array.)
	a new array containing the specified range from the original array, truncated or padded with nulls to obtain the required length
	ArrayIndexOutOfBoundsException - if from < 0 or from > original.length()
	IllegalArgumentException - if from > to
	NullPointerException - if original is null



import java.util.ArrayList;

public class hello {
    public static void main(String[] args) {
        // create an empty array list
        ArrayList<String> color_list = new ArrayList<String>();

        // use add() method to add values in the list
        System.out.println("List of the colors :" + color_list);

        //Return portion of the list : fromindex(inclusive)->1,  toindex(exclusive)->3
        ArrayList<String> new_color_list1 = new ArrayList<String>(color_list.subList(1, 3));
        System.out.println("Portion of the list: " + new_color_list1);


public List<E> subList(int fromIndex,
                       int toIndex)
Returns a view of the portion of this list between the specified fromIndex, inclusive, and toIndex, exclusive. (If fromIndex and toIndex are equal, the returned list is empty.) The returned list is backed by this list, so non-structural changes in the returned list are reflected in this list, and vice-versa. The returned list supports all of the optional list operations.
This method eliminates the need for explicit range operations (of the sort that commonly exist for arrays). Any operation that expects a list can be used as a range operation by passing a subList view instead of a whole list. For example, the following idiom removes a range of elements from a list:

      list.subList(from, to).clear();
Similar idioms may be constructed for indexOf(Object) and lastIndexOf(Object), and all of the algorithms in the Collections class can be applied to a subList.
The semantics of the list returned by this method become undefined if the backing list (i.e., this list) is structurally modified in any way other than via the returned list. (Structural modifications are those that change the size of this list, or otherwise perturb it in such a fashion that iterations in progress may yield incorrect results.)

Specified by:
	subList in interface List<E>
	subList in class AbstractList<E>
	fromIndex - low endpoint (inclusive) of the subList
	toIndex - high endpoint (exclusive) of the subList
	a view of the specified range within this list
	IndexOutOfBoundsException - if an endpoint index value is out of range (fromIndex < 0 || toIndex > size)
	IllegalArgumentException - if the endpoint indices are out of order (fromIndex > toIndex)
已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页