shift double value array left or right - Java java.lang

Java examples for java.lang:double Array

Description

shift double value array left or right

Demo Code


//package com.java2s;

public class Main {
    public static void main(String[] argv) throws Exception {
        double[] toShift = new double[] { 34.45, 35.45, 36.67, 37.78,
                37.0000, 37.1234, 67.2344, 68.34534, 69.87700 };
        int startPosition = 2;
        int targetPosition = 2;
        boolean cyclic = true;
        boolean rightShift = true;
        System.out.println(java.util.Arrays.toString(shift(toShift,
                startPosition, targetPosition, cyclic, rightShift)));
    }/*from  w w  w. jav  a2  s  . com*/

    public static double[] shift(double[] toShift, int startPosition,
            int targetPosition, boolean cyclic, boolean rightShift) {
        double left;
        if (cyclic) {
            if (rightShift) {
                left = toShift[targetPosition - 1];
            } else {
                left = toShift[startPosition];
            }
        } else {
            left = 0;
        }
        double swap;
        int index;
        for (int i = 0; i < targetPosition - startPosition; ++i) {
            if (rightShift) {
                index = i + startPosition;
            } else {
                index = targetPosition - 1 - i;
            }
            swap = toShift[index];
            toShift[index] = left;
            left = swap;
        }

        return toShift;
    }

    public static int[] shift(int[] toShift, int startPosition,
            int targetPosition, boolean cyclic, boolean rightShift) {
        int left;
        if (cyclic) {
            if (rightShift) {
                left = toShift[targetPosition - 1];
            } else {
                left = toShift[startPosition];
            }
        } else {
            left = 0;
        }
        int swap;
        int index;
        for (int i = 0; i < targetPosition - startPosition; ++i) {
            if (rightShift) {
                index = i + startPosition;
            } else {
                index = targetPosition - 1 - i;
            }
            swap = toShift[index];
            toShift[index] = left;
            left = swap;
        }

        return toShift;
    }

    public static String toString(int[] array) {
        StringBuilder result = new StringBuilder("{");

        if (array != null && array.length > 0) {
            for (int i = 0; i < array.length - 1; i++) {
                result.append(array[i] + ", ");
            }

            result.append(array[array.length - 1]);
        }

        result.append("}");

        return result.toString();
    }

    public static String toString(double[] array) {
        StringBuilder result = new StringBuilder("{");

        if (array != null && array.length > 0) {
            for (int i = 0; i < array.length - 1; i++) {
                result.append(array[i] + ", ");
            }

            result.append(array[array.length - 1]);
        }

        result.append("}");

        return result.toString();
    }
}

Related Tutorials