集册 Java实例教程 在一个数组中得到一个部分,子数组

在一个数组中得到一个部分,子数组

欢马劈雪     最近更新时间:2020-01-02 10:19:05

1263
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
在一个数组中得到一个部分,子数组


//package com.nowjava;


public class Main {/*n o w    j a v a  . c o m 提供*/

    public static void main(String[] argv) throws Exception {

        Object[] data = new String[] { "1", "abc", "level", null,

                "nowjava.com", "asdf 123" };

        int startIndex = 2;

        int endIndex = 2;

        System.out.println(java.util.Arrays.toString(subArray(data,

                startIndex, endIndex)));

    }


    /**

     * Get a part on an array

     * @param data The array to get the sub array from

     * @param startIndex The first index of the subarray

     * @param endIndex The first index not in the subarray (last index +1)

     * @return an array with the elements from data in the fields startIndex to endIndex-1

     */

    public static Object[] subArray(Object[] data, int startIndex,

            int endIndex) {

        Object[] output = new Object[endIndex - startIndex];

        for (int i = startIndex; i < endIndex; i++) {

            output[i - startIndex] = data[i];

        }/*来自 时 代 J a v a 公 众 号 - N o w J a v  a . c o m*/

        return output;

    }


    /**

     * Get a part on an array

     * @param data The array to get the sub array from

     * @param startIndex The first index of the subarray

     * @param endIndex The first index not in the subarray (last index +1)

     * @return an array with the elements from data in the fields startIndex to endIndex-1

     */

    public static boolean[] subArray(boolean[] data, int startIndex,

            int endIndex) {

        boolean[] output = new boolean[endIndex - startIndex];

        for (int i = startIndex; i < endIndex; i++) {

            output[i - startIndex] = data[i];

        }

        return output;

    }


    /**

     * Get a part on an array

     * @param data The array to get the sub array from

     * @param startIndex The first index of the subarray

     * @param endIndex The first index not in the subarray (last index +1)

     * @return an array with the elements from data in the fields startIndex to endIndex-1

     */

    public 
展开阅读全文