Java Array Invert invertArray(Object[] data)

Here you can find the source of invertArray(Object[] data)

Description

Turns an Object array backwards

License

Apache License

Parameter

Parameter Description
data The array to be inverted

Return

The inverted array

Declaration

public static Object[] invertArray(Object[] data) 

Method Source Code

//package com.java2s;
/*   Copyright 2014 BossLetsPlays(Matthew Rogers)
*
*   Licensed under the Apache License, Version 2.0 (the "License");
*   you may not use this file except in compliance with the License.
*   You may obtain a copy of the License at
*
*     http://www.apache.org/licenses/LICENSE-2.0
*
*   Unless required by applicable law or agreed to in writing, software
*   distributed under the License is distributed on an "AS IS" BASIS,
*   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*   See the License for the specific language governing permissions and
*   limitations under the License.//from  w  w  w  .j  a  va  2s. c  o  m
*/

public class Main {
    /**
     * Turns an <code>Object array</code> backwards
     * @param data The array to be inverted
     * @return The inverted array
     */
    public static Object[] invertArray(Object[] data) {
        Object[] result = new Object[data.length];
        int n = 0;
        for (int i = data.length - 1; i >= 0; i--) {
            result[n] = data[i];
            n++;
        }
        return result;
    }

    /**
     * Turns an <code>int array</code> backwards
     * @param data The array to be inverted
     * @return The inverted array
     */
    public static int[] invertArray(int[] data) {
        int[] result = new int[data.length];
        int n = 0;
        for (int i = data.length - 1; i >= 0; i--) {
            result[n] = data[i];
            n++;
        }
        return result;
    }

    /**
     * Turns a <code>double array</code> backwards
     * @param data The array to be inverted
     * @return The inverted array
     */
    public static double[] invertArray(double[] data) {
        double[] result = new double[data.length];
        int n = 0;
        for (int i = data.length - 1; i >= 0; i--) {
            result[n] = data[i];
            n++;
        }
        return result;
    }

    /**
     * Turns a <code>float array</code> backwards
     * @param data The array to be inverted
     * @return The inverted array
     */
    public static float[] invertArray(float[] data) {
        float[] result = new float[data.length];
        int n = 0;
        for (int i = data.length - 1; i >= 0; i--) {
            result[n] = data[i];
            n++;
        }
        return result;
    }

    /**
     * Turns a <code>boolean array</code> backwards
     * @param data The array to be inverted
     * @return The inverted array
     */
    public static boolean[] invertArray(boolean[] data) {
        boolean[] result = new boolean[data.length];
        int n = 0;
        for (int i = data.length - 1; i >= 0; i--) {
            result[n] = data[i];
            n++;
        }
        return result;
    }
}

Related

  1. invert(T[] array)
  2. invert3x3(float m[], float inv[])
  3. invert4x4(float m[], float inv[])
  4. invertArray(final byte[] array)
  5. invertArray(int[] a, int top)
  6. invertBits(byte[] input)
  7. InvertBitsInByte(Boolean[] booleanByte)
  8. invertByteArray(byte[] array)
  9. InvertBytes(Boolean[] dataToInvert)