Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;
import java.lang.reflect.Array;

public class Main {
    /**
     * Create a new array by pulling a slice out of an existing one.
     *
     * @param <T>   the type of the array elements
     * @param array the array
     * @param off   the start offset of the slice
     * @return a subset of the array from the offset to the end of the array
     */
    public static <T extends Object> T[] slice(T[] array, int off) {
        return slice(array, off, array.length - off);
    }

    /**
     * Create a new array by slicing a subset out of an existing one.
     *
     * @param <T>   the type of the array elements
     * @param array the array
     * @param off   the starting offset of the slice
     * @param len   the length of the slice.
     * @return a slice of the array starting at the given offset and with the specified length
     */
    public static <T extends Object> T[] slice(T[] array, int off, int len) {
        len = Math.min(len, array.length - off);
        T[] res = (T[]) Array.newInstance(array.getClass().getComponentType(), len);
        System.arraycopy(array, off, res, 0, len);
        return res;
    }
}