Generates an array going for a to b with steps of size step. - Java Collection Framework

Java examples for Collection Framework:Array Element

Description

Generates an array going for a to b with steps of size step.

Demo Code


//package com.java2s;

public class Main {


    /**/*w  w  w.j a v  a 2 s  .  c o m*/
     * 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;
            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 (int i = 0; i < v.length; i++) {
            ans[i] = abs(v[i]);
        }
        return (ans);
    }

    /**
     * Takes the absolute value of each component of an array.
     */
    public static int[] abs(int[] v) {
        int[] ans = new int[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 int[][] abs(int[][] v) {
        int[][] ans = new int[v.length][];
        for (int i = 0; i < v.length; i++) {
            ans[i] = abs(v[i]);
        }
        return (ans);
    }
}

Related Tutorials