集册 Java实例教程 提取子对象

提取子对象

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

519
提示:您可在线编辑运行本教程的实例 - 运行实例,去试试!
提取子数组
/*
 from nowjava.com - 时代Java 
*/


//package com.nowjava;


public class Main {

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

        int k0 = 2;

        int k1 = 2;

        double[] invect = new double[] { 34.45, 35.45, 36.67, 37.78,

                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };

        System.out.println(java.util.Arrays

                .toString(extract(k0, k1, invect)));

    }


    /**

     * Extracts a sub-array (will invert the resulting array if k0 > k1).

     *

     * @param k0 location of the first component

     * @param k1 location of the last component

     */

    public static double[] extract(int k0, int k1, double[] invect) {

        if ((k0 < 0) || (k1 < 0) || (k0 > invect.length - 1)

                || (k1 > invect.length - 1)) {
                /**
                来 自 时代Java公众号 - nowjava.com
                **/

            throw new IllegalArgumentException(

                    "The parameters are incorrect : " + k0 + ", " + k1

                            + ", " + invect.length);

        }

        if (k1 > k0) {

            double[] ans = new double[k1 - k0 + 1];

            System.arraycopy(invect, k0, ans, 0, k1 - k0 + 1);

            return (ans);

        }

        double[] ans = new double[-k1 + k0 + 1];

        for (int k = k1; k <= k0; k++) {

            ans[k0 - k] = invect[k];

        }

        return (ans);

    }


    /**

     * Extracts a sub-array (will invert the

     * resulting array if k0 > k1).

     *

     * @param k0 location of the first component.

     * @param k1 location of the last component.

     */

    public static int[] extract(int k0, int k1, int[] invect) {

        if ((k0 < 0) || (k1 < 0) || (k0 > invect.length - 1)

                || (k1 > invect.length - 1)) {

            throw new IllegalArgumentException(

                    "The parameters are incorrect : " + k0 + ", " + k1

                            + ", " + invect.length);

        }

        if (k1 > k0) {

            int[] ans = new int[k1 - k0 + 1];

            System.arraycopy(invect, k0, ans, 0, k1 - k0 + 1);

            return (ans);

        }

        int[] ans = new int[-k1 + k0 + 1];

        for (int k = k1; k <= k0; k++) {

            ans[k0 - k] = invect[k];

        }

        return (ans);

    }


    /**

     * Returns a comma delimited string representing the value of the array.

     */

    public static String toString(double[] array) {


        StringBuffer buf = new StringBuffer(array.length);

        int i;

        for (i = 0; i < array.length - 1; i++) {

            buf.append(array[i]);

            buf.append(',');

        }

        buf.append(array[i]);

        return buf.toString();

    }


    /**

     * Returns a comma delimited string representing the value of the array.

     */

    public static String toString(double[][] array) {

        StringBuffer buf = new StringBuffer();

        for (int k = 0; k < array.length; k++) {

            buf.append(toString(array[k]));

            buf.append(System.getProperty("line.separator"));

        }

        return buf.toString();

    }


    /**

     * Returns a comma delimited string representing the value of the array.

     */

    public static String toString(int[] array) {

        StringBuffer buf = new 
展开阅读全文