Java Array Invert invertArray(int[] a, int top)

Here you can find the source of invertArray(int[] a, int top)

Description

Invert an array.

License

Open Source License

Parameter

Parameter Description
a Array to invert
top The top value - that is, the maximum value of the array, exclusive. The minimum is always 0.

Return

Inverted array

Declaration

public static int[] invertArray(int[] a, int top) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

import java.util.*;

public class Main {
    /**//w  ww  . java  2 s  . com
     * Invert an array. That is, {1, 3, 5} is changed to {0, 2, 4, 6, 7} if top = 8.
     * @param a Array to invert
     * @param top The top value - that is, the maximum value of the array, exclusive. The minimum is always 0.
     * @return Inverted array
     */
    public static int[] invertArray(int[] a, int top) {
        int[] r = new int[top];
        int ri = 0;
        for (int i = 0; i < top; ++i) {
            if (!inArray(i, a)) {
                r[ri++] = i;
            }
        }
        return shrinkArray(r, ri);
    }

    /**
     * Tell whether an object is in an array
     * @param <T>
     * @param needle Object to look for
     * @param hayshack The array to look from
     * @return True if found, false otherwise.
     */
    public static <T> boolean inArray(T needle, T[] hayshack) {
        for (int i = 0; i < hayshack.length; ++i) {
            if (needle.equals(hayshack[i])) {
                return true;
            }
        }
        return false;
    }

    /**
     * Tell whether an int is in an array of ints
     * @param needle int to find
     * @param hayshack int list to find from
     * @return true if the int is in the list, false otherwise
     */
    public static boolean inArray(int needle, int[] hayshack) {
        for (int i = 0; i < hayshack.length; ++i) {
            if (needle == hayshack[i]) {
                return true;
            }
        }
        return false;
    }

    /**
     * Remove any surplus elements in the end of the array
     * @param <T>
     * @param p Array of player to be shrunk
     * @param l Only this number of the elements will be kept in the front of the array
     * @return shrunk array
     */
    public static <T> T[] shrinkArray(T[] p, int l) {
        return Arrays.copyOf(p, l);
    }

    /**
     * Remove any surplus elements in the end of the array of ints
     * @param p Array of ints to be shrunk
     * @param l Only this number of the elements will be kept in the front of the array
     * @return shrunk array
     */
    public static int[] shrinkArray(int[] p, int l) {
        return Arrays.copyOf(p, l);
    }
}

Related

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