90
91 return append(localArr, newObj);
92 }
93
94 /**
95 * Join two arrays.
96 * @param array1 The first array to be joined.
97 * @param array2 The second array to be joined.
98 * @return If either argument is null, the result is the other argument;
99 * otherwise, the result is a new array, whose element type is taken from
100 * the first array, containing the elements of the first array, followed
101 * by the elements of the second array.
102 */
103 public static <T> T[] join(T[] array1, T[] array2) {
104 if (array1 == null)
105 return array2;
106
107 if (array2 == null)
108 return array1;
109
110 Class type = array1.getClass().getComponentType();
111 int size = array1.length + array2.length;
112 T[] newArray = (T[]) Array.newInstance(type, size);
113 System.arraycopy(array1, 0, newArray, 0, array1.length);
114 System.arraycopy(array2, 0, newArray, array1.length, array2.length);
115 return newArray;
116 }
117
118 /**
119 * Insert an object into the array at the specified index.
120 *
121 * @param oldArr The array to insert into. May be null, in which case a new array
122 * will be created using the type of <code>newObj</code>.
123 * @param newObj The object to insert.
124 * @param location The index at which to insert the item. An exception will occur
125 * if the location in out of range. This location can be equal to
126 * <code>oldArr.length</code>, in which case this becomes an append
127 * operation.
128 * @return A new array with the object inserted into it at the specified location.
129 */
130 public static <T> T[] insert(T[] oldArr, T newObj, int location) {
244 */
245 public static <T> int find(T[] arr, T target) {
246 int index = -1;
247
248 for(int i = 0; i < arr.length; i++) {
249 if(arr[i] == target) {
250 index = i;
251 break;
252 }
253 }
254
255 return index;
256 }
257
258 /**
259 * Get the type of objects that the array is declared to hold.
260 *
261 * @param arr The array to examine.
262 * @return The class of objects that the given array can hold.
263 */
264 protected static Class getArrayClass(Object[] arr) {
265 if(arr != null) {
266 return arr.getClass().getComponentType();
267 } else {
268 return null;
269 }
270 }
271
272 }
273
|
90
91 return append(localArr, newObj);
92 }
93
94 /**
95 * Join two arrays.
96 * @param array1 The first array to be joined.
97 * @param array2 The second array to be joined.
98 * @return If either argument is null, the result is the other argument;
99 * otherwise, the result is a new array, whose element type is taken from
100 * the first array, containing the elements of the first array, followed
101 * by the elements of the second array.
102 */
103 public static <T> T[] join(T[] array1, T[] array2) {
104 if (array1 == null)
105 return array2;
106
107 if (array2 == null)
108 return array1;
109
110 Class<?> type = array1.getClass().getComponentType();
111 int size = array1.length + array2.length;
112 T[] newArray = (T[]) Array.newInstance(type, size);
113 System.arraycopy(array1, 0, newArray, 0, array1.length);
114 System.arraycopy(array2, 0, newArray, array1.length, array2.length);
115 return newArray;
116 }
117
118 /**
119 * Insert an object into the array at the specified index.
120 *
121 * @param oldArr The array to insert into. May be null, in which case a new array
122 * will be created using the type of <code>newObj</code>.
123 * @param newObj The object to insert.
124 * @param location The index at which to insert the item. An exception will occur
125 * if the location in out of range. This location can be equal to
126 * <code>oldArr.length</code>, in which case this becomes an append
127 * operation.
128 * @return A new array with the object inserted into it at the specified location.
129 */
130 public static <T> T[] insert(T[] oldArr, T newObj, int location) {
244 */
245 public static <T> int find(T[] arr, T target) {
246 int index = -1;
247
248 for(int i = 0; i < arr.length; i++) {
249 if(arr[i] == target) {
250 index = i;
251 break;
252 }
253 }
254
255 return index;
256 }
257
258 /**
259 * Get the type of objects that the array is declared to hold.
260 *
261 * @param arr The array to examine.
262 * @return The class of objects that the given array can hold.
263 */
264 protected static Class<?> getArrayClass(Object[] arr) {
265 if(arr != null) {
266 return arr.getClass().getComponentType();
267 } else {
268 return null;
269 }
270 }
271
272 }
273
|