get Part Of Array by start and length - Java Collection Framework

Java examples for Collection Framework:Array Length

Description

get Part Of Array by start and length

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        Object[] array = new String[] { "1", "abc", "level", null,
                "java2s.com", "asdf 123" };
        int len = 2;
        System.out.println(java.util.Arrays.toString(getPartOfArray(array,
                len)));//from   w ww  . j av a  2s.  com
    }

    public static Object[] getPartOfArray(Object[] array, int len) {
        return getPartOfArray(array, 0, len);
    }

    public static Object[] getPartOfArray(Object[] array, int start, int len) {
        Object[] newArray = new Object[len];
        for (int i = start; i < start + len; i++) {
            newArray[i - start] = array[i];
        }
        return newArray;
    }
}

Related Tutorials