Lines Matching defs:array

23  * arrays. Common array operations are implemented for efficient use in dynamic containers.
25 * All methods in this class assume that the length of an array is equivalent to its capacity and
26 * NOT the number of elements in the array. The current size of the array is always passed in as a
32 * Appends an element to the end of the array, growing the array if there is no more room.
33 * @param array The array to which to append the element. This must NOT be null.
34 * @param currentSize The number of elements in the array. Must be less than or equal to
35 * array.length.
37 * @return the array to which the element was appended. This may be different than the given
38 * array.
40 public static <T> T[] append(T[] array, int currentSize, T element) {
41 assert currentSize <= array.length;
43 if (currentSize + 1 > array.length) {
44 T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(),
46 System.arraycopy(array, 0, newArray, 0, currentSize);
47 array = newArray;
49 array[currentSize] = element;
50 return array;
56 public static int[] append(int[] array, int currentSize, int element) {
57 assert currentSize <= array.length;
59 if (currentSize + 1 > array.length) {
61 System.arraycopy(array, 0, newArray, 0, currentSize);
62 array = newArray;
64 array[currentSize] = element;
65 return array;
71 public static long[] append(long[] array, int currentSize, long element) {
72 assert currentSize <= array.length;
74 if (currentSize + 1 > array.length) {
76 System.arraycopy(array, 0, newArray, 0, currentSize);
77 array = newArray;
79 array[currentSize] = element;
80 return array;
86 public static boolean[] append(boolean[] array, int currentSize, boolean element) {
87 assert currentSize <= array.length;
89 if (currentSize + 1 > array.length) {
91 System.arraycopy(array, 0, newArray, 0, currentSize);
92 array = newArray;
94 array[currentSize] = element;
95 return array;
99 * Inserts an element into the array at the specified index, growing the array if there is no
102 * @param array The array to which to append the element. Must NOT be null.
103 * @param currentSize The number of elements in the array. Must be less than or equal to
104 * array.length.
106 * @return the array to which the element was appended. This may be different than the given
107 * array.
109 public static <T> T[] insert(T[] array, int currentSize, int index, T element) {
110 assert currentSize <= array.length;
112 if (currentSize + 1 <= array.length) {
113 System.arraycopy(array, index, array, index + 1, currentSize - index);
114 array[index] = element;
115 return array;
118 T[] newArray = (T[]) Array.newInstance(array.getClass().getComponentType(),
120 System.arraycopy(array, 0, newArray, 0, index);
122 System.arraycopy(array, index, newArray, index + 1, array.length - index);
129 public static int[] insert(int[] array, int currentSize, int index, int element) {
130 assert currentSize <= array.length;
132 if (currentSize + 1 <= array.length) {
133 System.arraycopy(array, index, array, index + 1, currentSize - index);
134 array[index] = element;
135 return array;
139 System.arraycopy(array, 0, newArray, 0, index);
141 System.arraycopy(array, index, newArray, index + 1, array.length - index);
148 public static long[] insert(long[] array, int currentSize, int index, long element) {
149 assert currentSize <= array.length;
151 if (currentSize + 1 <= array.length) {
152 System.arraycopy(array, index, array, index + 1, currentSize - index);
153 array[index] = element;
154 return array;
158 System.arraycopy(array, 0, newArray, 0, index);
160 System.arraycopy(array, index, newArray, index + 1, array.length - index);
167 public static boolean[] insert(boolean[] array, int currentSize, int index, boolean element) {
168 assert currentSize <= array.length;
170 if (currentSize + 1 <= array.length) {
171 System.arraycopy(array, index, array, index + 1, currentSize - index);
172 array[index] = element;
173 return array;
177 System.arraycopy(array, 0, newArray, 0, index);
179 System.arraycopy(array, index, newArray, index + 1, array.length - index);
184 * Given the current size of an array, returns an ideal size to which the array should grow.