集册 Java实例教程 生成一个a到b的数组,其步数为size step。

生成一个a到b的数组,其步数为size step。

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

486
生成一个a到b的数组,其步数为size step。
/** 来自 时 代 J a v a - N o w J a v a . c o m**/


//package com.nowjava;


public class Main {



    /**

     * Generates an array going for a to b

     * with steps of size step. If it can't

     * get to the value b in a finite number

     * of steps, it gets as close as possible

     * (a can be larger or smaller than b)

     *

     * @param step size of steps, must be positive.

     * @param a    first value of array.

     * @param b    last value of array.

     * @throws IllegalArgumentException if step is negative of if a=b.

     */

    public static double[] range(double a, double b, double step) {

        if (step <= 0.0) {

            throw new IllegalArgumentException(

                    "The argument step should be positive: " + step

                            + " < 0");

        }

        if (a == b) {

            double[] ans = new double[1];

            ans[0] = a;
            /*
            n o w  j a v a  . c o m 提供
            */

            return (ans);

        }

        int sizeOfArray = (new Double(Math.abs(a - b) / step)).intValue() + 1;

        double[] ans = new double[sizeOfArray];

        ans[0] = a;

        if (a > b) {

            step = -step;

        }

        for (int k = 1; k < sizeOfArray; k++) {

            ans[k] = ans[k - 1] + step;

        }

        return (ans);

    }


    /**

     * Generates an array going for a to b

     * inclusively with steps of size 1. a can be

     * smaller or larger than b.

     */

    public static double[] range(double a, double b) {

        return (range(a, b, 1.0));

    }


    /**

     * returns the difference between the max and the min element

     */

    public static double range(double[] population) {

        double min = Double.POSITIVE_INFINITY;

        double max = Double.NEGATIVE_INFINITY;

        for (int i = 0; i < population.length; i++) {

            if (population[i] < min)

                min = population[i];

            if (population[i] > max)

                max = population[i];

        }

        return max - min;

    }


    /**

     * Generates an array going for 0 to b

     * with steps of size 1. 0 can be

     * smaller or larger than b.

     */

    public static double[] range(double b) {

        return (range(0, b));

    }


    /**

     * Generates an array going for a to b

     * with steps of size 1. a can be

     * smaller or larger than b.

     */

    public static int[] range(int a, int b) {

        return (range(a, b, 1));

    }


    /**

     * Generates an array going for 0 to b

     * with steps of size 1. 0 can be

     * smaller or larger than b.

     */

    public static int[] range(int b) {

        return (range(0, b));

    }


    /**

     * Generates an array going for a to b

     * with steps of size step. If it can't

     * get to the value b in a finite number

     * of steps, it gets as close as possible

     * (a can be larger or smaller than b)

     *

     * @param step size of steps, must be positive

     * @param a    first value of array

     * @param b    last value of array

     * @throws IllegalArgumentException if step is

     *                                  negative or if a=b.

     */

    public static int[] range(int a, int b, int step) {

        if (step <= 0) {

            throw new IllegalArgumentException(

                    "The argument step should be positive: " + step

                            + " < 0");

        }

        if (a == b) {

            int[] ans = new int[1];

            ans[0] = a;

            return (ans);

        }

        int sizeOfArray = (new Double(Math.abs(a - b) / step)).intValue();

        int[] ans = new int[sizeOfArray];

        ans[0] = a;

        if (a > b) {

            step = -step;

        }

        for (int k = 1; k < sizeOfArray; k++) {

            ans[k] = ans[k - 1] + step;

        }

        return (ans);

    }


    /**

     * Takes the absolute value of each component of an array.

     */

    public static double[] abs(double[] v) {

        double[] ans = new double[v.length];

        for (int i = 0; i < v.length; i++) {

            ans[i] = Math.abs(v[i]);

        }

        return (ans);

    }


    /**

     * Takes the absolute value of each component of an array.

     */

    public static double[][] abs(double[][] v) {

        double[][] ans = new double[v.length][];

        for
展开阅读全文