Here you can find the source of invertArray(int[] a, int top)
Parameter | Description |
---|---|
a | Array to invert |
top | The top value - that is, the maximum value of the array, exclusive. The minimum is always 0. |
public static int[] invertArray(int[] a, int top)
//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); } }