Extracts a sub-array - Java Collection Framework

Java examples for Collection Framework:Array Sub Array

Description

Extracts a sub-array

Demo Code


//package com.java2s;

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)));
    }/*from   w ww. j av a  2 s .c om*/

    /**
     * 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)) {
            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 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(int[][] 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();
    }
}

Related Tutorials