KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > commons > lang > ArrayUtils


1 /*
2  * Copyright 2002-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.commons.lang;
17
18 import java.lang.reflect.Array JavaDoc;
19 import java.util.HashMap JavaDoc;
20 import java.util.Map JavaDoc;
21
22 import org.apache.commons.lang.builder.EqualsBuilder;
23 import org.apache.commons.lang.builder.HashCodeBuilder;
24 import org.apache.commons.lang.builder.ToStringBuilder;
25 import org.apache.commons.lang.builder.ToStringStyle;
26
27 /**
28  * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
29  * primitive wrapper arrays (like <code>Integer[]</code>).</p>
30  *
31  * <p>This class tries to handle <code>null</code> input gracefully.
32  * An exception will not be thrown for a <code>null</code>
33  * array input. However, an Object array that contains a <code>null</code>
34  * element may throw an exception. Each method documents its behaviour.</p>
35  *
36  * @author Stephen Colebourne
37  * @author Moritz Petersen
38  * @author <a HREF="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
39  * @author Nikolay Metchev
40  * @author Matthew Hawthorne
41  * @author Tim O'Brien
42  * @author Pete Gieser
43  * @author Gary Gregory
44  * @author <a HREF="mailto:equinus100@hotmail.com">Ashwin S</a>
45  * @author Maarten Coene
46  * @since 2.0
47  * @version $Id: ArrayUtils.java 161243 2005-04-14 04:30:28Z ggregory $
48  */

49 public class ArrayUtils {
50
51     /**
52      * An empty immutable <code>Object</code> array.
53      */

54     public static final Object JavaDoc[] EMPTY_OBJECT_ARRAY = new Object JavaDoc[0];
55     /**
56      * An empty immutable <code>Class</code> array.
57      */

58     public static final Class JavaDoc[] EMPTY_CLASS_ARRAY = new Class JavaDoc[0];
59     /**
60      * An empty immutable <code>String</code> array.
61      */

62     public static final String JavaDoc[] EMPTY_STRING_ARRAY = new String JavaDoc[0];
63     /**
64      * An empty immutable <code>long</code> array.
65      */

66     public static final long[] EMPTY_LONG_ARRAY = new long[0];
67     /**
68      * An empty immutable <code>Long</code> array.
69      */

70     public static final Long JavaDoc[] EMPTY_LONG_OBJECT_ARRAY = new Long JavaDoc[0];
71     /**
72      * An empty immutable <code>int</code> array.
73      */

74     public static final int[] EMPTY_INT_ARRAY = new int[0];
75     /**
76      * An empty immutable <code>Integer</code> array.
77      */

78     public static final Integer JavaDoc[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer JavaDoc[0];
79     /**
80      * An empty immutable <code>short</code> array.
81      */

82     public static final short[] EMPTY_SHORT_ARRAY = new short[0];
83     /**
84      * An empty immutable <code>Short</code> array.
85      */

86     public static final Short JavaDoc[] EMPTY_SHORT_OBJECT_ARRAY = new Short JavaDoc[0];
87     /**
88      * An empty immutable <code>byte</code> array.
89      */

90     public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
91     /**
92      * An empty immutable <code>Byte</code> array.
93      */

94     public static final Byte JavaDoc[] EMPTY_BYTE_OBJECT_ARRAY = new Byte JavaDoc[0];
95     /**
96      * An empty immutable <code>double</code> array.
97      */

98     public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
99     /**
100      * An empty immutable <code>Double</code> array.
101      */

102     public static final Double JavaDoc[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double JavaDoc[0];
103     /**
104      * An empty immutable <code>float</code> array.
105      */

106     public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
107     /**
108      * An empty immutable <code>Float</code> array.
109      */

110     public static final Float JavaDoc[] EMPTY_FLOAT_OBJECT_ARRAY = new Float JavaDoc[0];
111     /**
112      * An empty immutable <code>boolean</code> array.
113      */

114     public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
115     /**
116      * An empty immutable <code>Boolean</code> array.
117      */

118     public static final Boolean JavaDoc[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean JavaDoc[0];
119     /**
120      * An empty immutable <code>char</code> array.
121      */

122     public static final char[] EMPTY_CHAR_ARRAY = new char[0];
123     /**
124      * An empty immutable <code>Character</code> array.
125      */

126     public static final Character JavaDoc[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character JavaDoc[0];
127
128     /**
129      * <p>ArrayUtils instances should NOT be constructed in standard programming.
130      * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>.</p>
131      *
132      * <p>This constructor is public to permit tools that require a JavaBean instance
133      * to operate.</p>
134      */

135     public ArrayUtils() {
136     }
137     
138     // Basic methods handling multi-dimensional arrays
139
//-----------------------------------------------------------------------
140
/**
141      * <p>Outputs an array as a String, treating <code>null</code> as an empty array.</p>
142      *
143      * <p>Multi-dimensional arrays are handled correctly, including
144      * multi-dimensional primitive arrays.</p>
145      *
146      * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
147      *
148      * @param array the array to get a toString for, may be <code>null</code>
149      * @return a String representation of the array, '{}' if null array input
150      */

151     public static String JavaDoc toString(Object JavaDoc array) {
152         return toString(array, "{}");
153     }
154
155     /**
156      * <p>Outputs an array as a String handling <code>null</code>s.</p>
157      *
158      * <p>Multi-dimensional arrays are handled correctly, including
159      * multi-dimensional primitive arrays.</p>
160      *
161      * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
162      *
163      * @param array the array to get a toString for, may be <code>null</code>
164      * @param stringIfNull the String to return if the array is <code>null</code>
165      * @return a String representation of the array
166      */

167     public static String JavaDoc toString(Object JavaDoc array, String JavaDoc stringIfNull) {
168         if (array == null) {
169             return stringIfNull;
170         }
171         return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
172     }
173
174     /**
175      * <p>Get a hashCode for an array handling multi-dimensional arrays correctly.</p>
176      *
177      * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
178      *
179      * @param array the array to get a hashCode for, may be <code>null</code>
180      * @return a hashCode for the array, zero if null array input
181      */

182     public static int hashCode(Object JavaDoc array) {
183         return new HashCodeBuilder().append(array).toHashCode();
184     }
185
186     /**
187      * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
188      * correctly.</p>
189      *
190      * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
191      *
192      * @param array1 the left hand array to compare, may be <code>null</code>
193      * @param array2 the right hand array to compare, may be <code>null</code>
194      * @return <code>true</code> if the arrays are equal
195      */

196     public static boolean isEquals(Object JavaDoc array1, Object JavaDoc array2) {
197         return new EqualsBuilder().append(array1, array2).isEquals();
198     }
199
200     // To map
201
//-----------------------------------------------------------------------
202
/**
203      * <p>Converts the given array into a {@link java.util.Map}. Each element of the array
204      * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
205      * elements, where the first element is used as key and the second as
206      * value.</p>
207      *
208      * <p>This method can be used to initialize:</p>
209      * <pre>
210      * // Create a Map mapping colors.
211      * Map colorMap = MapUtils.toMap(new String[][] {{
212      * {"RED", "#FF0000"},
213      * {"GREEN", "#00FF00"},
214      * {"BLUE", "#0000FF"}});
215      * </pre>
216      *
217      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
218      *
219      * @param array an array whose elements are either a {@link java.util.Map.Entry} or
220      * an Array containing at least two elements, may be <code>null</code>
221      * @return a <code>Map</code> that was created from the array
222      * @throws IllegalArgumentException if one element of this Array is
223      * itself an Array containing less then two elements
224      * @throws IllegalArgumentException if the array contains elements other
225      * than {@link java.util.Map.Entry} and an Array
226      */

227     public static Map JavaDoc toMap(Object JavaDoc[] array) {
228         if (array == null) {
229             return null;
230         }
231         final Map JavaDoc map = new HashMap JavaDoc((int) (array.length * 1.5));
232         for (int i = 0; i < array.length; i++) {
233             Object JavaDoc object = array[i];
234             if (object instanceof Map.Entry JavaDoc) {
235                 Map.Entry JavaDoc entry = (Map.Entry JavaDoc) object;
236                 map.put(entry.getKey(), entry.getValue());
237             } else if (object instanceof Object JavaDoc[]) {
238                 Object JavaDoc[] entry = (Object JavaDoc[]) object;
239                 if (entry.length < 2) {
240                     throw new IllegalArgumentException JavaDoc("Array element " + i + ", '"
241                         + object
242                         + "', has a length less than 2");
243                 }
244                 map.put(entry[0], entry[1]);
245             } else {
246                 throw new IllegalArgumentException JavaDoc("Array element " + i + ", '"
247                         + object
248                         + "', is neither of type Map.Entry nor an Array");
249             }
250         }
251         return map;
252     }
253
254     // Clone
255
//-----------------------------------------------------------------------
256
/**
257      * <p>Shallow clones an array returning a typecast result and handling
258      * <code>null</code>.</p>
259      *
260      * <p>The objects in the array are not cloned, thus there is no special
261      * handling for multi-dimensional arrays.</p>
262      *
263      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
264      *
265      * @param array the array to shallow clone, may be <code>null</code>
266      * @return the cloned array, <code>null</code> if <code>null</code> input
267      */

268     public static Object JavaDoc[] clone(Object JavaDoc[] array) {
269         if (array == null) {
270             return null;
271         }
272         return (Object JavaDoc[]) array.clone();
273     }
274
275     /**
276      * <p>Clones an array returning a typecast result and handling
277      * <code>null</code>.</p>
278      *
279      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
280      *
281      * @param array the array to clone, may be <code>null</code>
282      * @return the cloned array, <code>null</code> if <code>null</code> input
283      */

284     public static long[] clone(long[] array) {
285         if (array == null) {
286             return null;
287         }
288         return (long[]) array.clone();
289     }
290
291     /**
292      * <p>Clones an array returning a typecast result and handling
293      * <code>null</code>.</p>
294      *
295      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
296      *
297      * @param array the array to clone, may be <code>null</code>
298      * @return the cloned array, <code>null</code> if <code>null</code> input
299      */

300     public static int[] clone(int[] array) {
301         if (array == null) {
302             return null;
303         }
304         return (int[]) array.clone();
305     }
306
307     /**
308      * <p>Clones an array returning a typecast result and handling
309      * <code>null</code>.</p>
310      *
311      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
312      *
313      * @param array the array to clone, may be <code>null</code>
314      * @return the cloned array, <code>null</code> if <code>null</code> input
315      */

316     public static short[] clone(short[] array) {
317         if (array == null) {
318             return null;
319         }
320         return (short[]) array.clone();
321     }
322
323     /**
324      * <p>Clones an array returning a typecast result and handling
325      * <code>null</code>.</p>
326      *
327      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
328      *
329      * @param array the array to clone, may be <code>null</code>
330      * @return the cloned array, <code>null</code> if <code>null</code> input
331      */

332     public static char[] clone(char[] array) {
333         if (array == null) {
334             return null;
335         }
336         return (char[]) array.clone();
337     }
338
339     /**
340      * <p>Clones an array returning a typecast result and handling
341      * <code>null</code>.</p>
342      *
343      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
344      *
345      * @param array the array to clone, may be <code>null</code>
346      * @return the cloned array, <code>null</code> if <code>null</code> input
347      */

348     public static byte[] clone(byte[] array) {
349         if (array == null) {
350             return null;
351         }
352         return (byte[]) array.clone();
353     }
354
355     /**
356      * <p>Clones an array returning a typecast result and handling
357      * <code>null</code>.</p>
358      *
359      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
360      *
361      * @param array the array to clone, may be <code>null</code>
362      * @return the cloned array, <code>null</code> if <code>null</code> input
363      */

364     public static double[] clone(double[] array) {
365         if (array == null) {
366             return null;
367         }
368         return (double[]) array.clone();
369     }
370
371     /**
372      * <p>Clones an array returning a typecast result and handling
373      * <code>null</code>.</p>
374      *
375      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
376      *
377      * @param array the array to clone, may be <code>null</code>
378      * @return the cloned array, <code>null</code> if <code>null</code> input
379      */

380     public static float[] clone(float[] array) {
381         if (array == null) {
382             return null;
383         }
384         return (float[]) array.clone();
385     }
386
387     /**
388      * <p>Clones an array returning a typecast result and handling
389      * <code>null</code>.</p>
390      *
391      * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
392      *
393      * @param array the array to clone, may be <code>null</code>
394      * @return the cloned array, <code>null</code> if <code>null</code> input
395      */

396     public static boolean[] clone(boolean[] array) {
397         if (array == null) {
398             return null;
399         }
400         return (boolean[]) array.clone();
401     }
402
403     // Subarrays
404
//-----------------------------------------------------------------------
405
/**
406      * <p>Produces a new array containing the elements between
407      * the start and end indices.</p>
408      *
409      * <p>The start index is inclusive, the end index exclusive.
410      * Null array input produces null output.</p>
411      *
412      * <p>The component type of the subarray is always the same as
413      * that of the input array. Thus, if the input is an array of type
414      * <code>Date</code>, the following usage is envisaged:</p>
415      *
416      * <pre>
417      * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
418      * </pre>
419      *
420      * @param array the array
421      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
422      * is promoted to 0, overvalue (&gt;array.length) results
423      * in an empty array.
424      * @param endIndexExclusive elements up to endIndex-1 are present in the
425      * returned subarray. Undervalue (&lt; startIndex) produces
426      * empty array, overvalue (&gt;array.length) is demoted to
427      * array length.
428      * @return a new array containing the elements between
429      * the start and end indices.
430      * @since 2.1
431      */

432     public static Object JavaDoc[] subarray(Object JavaDoc[] array, int startIndexInclusive, int endIndexExclusive) {
433         if (array == null) {
434             return null;
435         }
436         if (startIndexInclusive < 0) {
437             startIndexInclusive = 0;
438         }
439         if (endIndexExclusive > array.length) {
440             endIndexExclusive = array.length;
441         }
442         int newSize = endIndexExclusive - startIndexInclusive;
443         Class JavaDoc type = array.getClass().getComponentType();
444         if (newSize <= 0) {
445             return (Object JavaDoc[]) Array.newInstance(type, 0);
446         }
447         Object JavaDoc[] subarray = (Object JavaDoc[]) Array.newInstance(type, newSize);
448         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
449         return subarray;
450     }
451
452     /**
453      * <p>Produces a new <code>long</code> array containing the elements
454      * between the start and end indices.</p>
455      *
456      * <p>The start index is inclusive, the end index exclusive.
457      * Null array input produces null output.</p>
458      *
459      * @param array the array
460      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
461      * is promoted to 0, overvalue (&gt;array.length) results
462      * in an empty array.
463      * @param endIndexExclusive elements up to endIndex-1 are present in the
464      * returned subarray. Undervalue (&lt; startIndex) produces
465      * empty array, overvalue (&gt;array.length) is demoted to
466      * array length.
467      * @return a new array containing the elements between
468      * the start and end indices.
469      * @since 2.1
470      */

471     public static long[] subarray(long[] array, int startIndexInclusive, int endIndexExclusive) {
472         if (array == null) {
473             return null;
474         }
475         if (startIndexInclusive < 0) {
476             startIndexInclusive = 0;
477         }
478         if (endIndexExclusive > array.length) {
479             endIndexExclusive = array.length;
480         }
481         int newSize = endIndexExclusive - startIndexInclusive;
482         if (newSize <= 0) {
483             return EMPTY_LONG_ARRAY;
484         }
485
486         long[] subarray = new long[newSize];
487         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
488         return subarray;
489     }
490
491     /**
492      * <p>Produces a new <code>int</code> array containing the elements
493      * between the start and end indices.</p>
494      *
495      * <p>The start index is inclusive, the end index exclusive.
496      * Null array input produces null output.</p>
497      *
498      * @param array the array
499      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
500      * is promoted to 0, overvalue (&gt;array.length) results
501      * in an empty array.
502      * @param endIndexExclusive elements up to endIndex-1 are present in the
503      * returned subarray. Undervalue (&lt; startIndex) produces
504      * empty array, overvalue (&gt;array.length) is demoted to
505      * array length.
506      * @return a new array containing the elements between
507      * the start and end indices.
508      * @since 2.1
509      */

510     public static int[] subarray(int[] array, int startIndexInclusive, int endIndexExclusive) {
511         if (array == null) {
512             return null;
513         }
514         if (startIndexInclusive < 0) {
515             startIndexInclusive = 0;
516         }
517         if (endIndexExclusive > array.length) {
518             endIndexExclusive = array.length;
519         }
520         int newSize = endIndexExclusive - startIndexInclusive;
521         if (newSize <= 0) {
522             return EMPTY_INT_ARRAY;
523         }
524
525         int[] subarray = new int[newSize];
526         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
527         return subarray;
528     }
529
530     /**
531      * <p>Produces a new <code>short</code> array containing the elements
532      * between the start and end indices.</p>
533      *
534      * <p>The start index is inclusive, the end index exclusive.
535      * Null array input produces null output.</p>
536      *
537      * @param array the array
538      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
539      * is promoted to 0, overvalue (&gt;array.length) results
540      * in an empty array.
541      * @param endIndexExclusive elements up to endIndex-1 are present in the
542      * returned subarray. Undervalue (&lt; startIndex) produces
543      * empty array, overvalue (&gt;array.length) is demoted to
544      * array length.
545      * @return a new array containing the elements between
546      * the start and end indices.
547      * @since 2.1
548      */

549     public static short[] subarray(short[] array, int startIndexInclusive, int endIndexExclusive) {
550         if (array == null) {
551             return null;
552         }
553         if (startIndexInclusive < 0) {
554             startIndexInclusive = 0;
555         }
556         if (endIndexExclusive > array.length) {
557             endIndexExclusive = array.length;
558         }
559         int newSize = endIndexExclusive - startIndexInclusive;
560         if (newSize <= 0) {
561             return EMPTY_SHORT_ARRAY;
562         }
563
564         short[] subarray = new short[newSize];
565         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
566         return subarray;
567     }
568
569     /**
570      * <p>Produces a new <code>char</code> array containing the elements
571      * between the start and end indices.</p>
572      *
573      * <p>The start index is inclusive, the end index exclusive.
574      * Null array input produces null output.</p>
575      *
576      * @param array the array
577      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
578      * is promoted to 0, overvalue (&gt;array.length) results
579      * in an empty array.
580      * @param endIndexExclusive elements up to endIndex-1 are present in the
581      * returned subarray. Undervalue (&lt; startIndex) produces
582      * empty array, overvalue (&gt;array.length) is demoted to
583      * array length.
584      * @return a new array containing the elements between
585      * the start and end indices.
586      * @since 2.1
587      */

588     public static char[] subarray(char[] array, int startIndexInclusive, int endIndexExclusive) {
589         if (array == null) {
590             return null;
591         }
592         if (startIndexInclusive < 0) {
593             startIndexInclusive = 0;
594         }
595         if (endIndexExclusive > array.length) {
596             endIndexExclusive = array.length;
597         }
598         int newSize = endIndexExclusive - startIndexInclusive;
599         if (newSize <= 0) {
600             return EMPTY_CHAR_ARRAY;
601         }
602
603         char[] subarray = new char[newSize];
604         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
605         return subarray;
606     }
607
608     /**
609      * <p>Produces a new <code>byte</code> array containing the elements
610      * between the start and end indices.</p>
611      *
612      * <p>The start index is inclusive, the end index exclusive.
613      * Null array input produces null output.</p>
614      *
615      * @param array the array
616      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
617      * is promoted to 0, overvalue (&gt;array.length) results
618      * in an empty array.
619      * @param endIndexExclusive elements up to endIndex-1 are present in the
620      * returned subarray. Undervalue (&lt; startIndex) produces
621      * empty array, overvalue (&gt;array.length) is demoted to
622      * array length.
623      * @return a new array containing the elements between
624      * the start and end indices.
625      * @since 2.1
626      */

627     public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) {
628         if (array == null) {
629             return null;
630         }
631         if (startIndexInclusive < 0) {
632             startIndexInclusive = 0;
633         }
634         if (endIndexExclusive > array.length) {
635             endIndexExclusive = array.length;
636         }
637         int newSize = endIndexExclusive - startIndexInclusive;
638         if (newSize <= 0) {
639             return EMPTY_BYTE_ARRAY;
640         }
641
642         byte[] subarray = new byte[newSize];
643         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
644         return subarray;
645     }
646
647     /**
648      * <p>Produces a new <code>double</code> array containing the elements
649      * between the start and end indices.</p>
650      *
651      * <p>The start index is inclusive, the end index exclusive.
652      * Null array input produces null output.</p>
653      *
654      * @param array the array
655      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
656      * is promoted to 0, overvalue (&gt;array.length) results
657      * in an empty array.
658      * @param endIndexExclusive elements up to endIndex-1 are present in the
659      * returned subarray. Undervalue (&lt; startIndex) produces
660      * empty array, overvalue (&gt;array.length) is demoted to
661      * array length.
662      * @return a new array containing the elements between
663      * the start and end indices.
664      * @since 2.1
665      */

666     public static double[] subarray(double[] array, int startIndexInclusive, int endIndexExclusive) {
667         if (array == null) {
668             return null;
669         }
670         if (startIndexInclusive < 0) {
671             startIndexInclusive = 0;
672         }
673         if (endIndexExclusive > array.length) {
674             endIndexExclusive = array.length;
675         }
676         int newSize = endIndexExclusive - startIndexInclusive;
677         if (newSize <= 0) {
678             return EMPTY_DOUBLE_ARRAY;
679         }
680
681         double[] subarray = new double[newSize];
682         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
683         return subarray;
684     }
685
686     /**
687      * <p>Produces a new <code>float</code> array containing the elements
688      * between the start and end indices.</p>
689      *
690      * <p>The start index is inclusive, the end index exclusive.
691      * Null array input produces null output.</p>
692      *
693      * @param array the array
694      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
695      * is promoted to 0, overvalue (&gt;array.length) results
696      * in an empty array.
697      * @param endIndexExclusive elements up to endIndex-1 are present in the
698      * returned subarray. Undervalue (&lt; startIndex) produces
699      * empty array, overvalue (&gt;array.length) is demoted to
700      * array length.
701      * @return a new array containing the elements between
702      * the start and end indices.
703      * @since 2.1
704      */

705     public static float[] subarray(float[] array, int startIndexInclusive, int endIndexExclusive) {
706         if (array == null) {
707             return null;
708         }
709         if (startIndexInclusive < 0) {
710             startIndexInclusive = 0;
711         }
712         if (endIndexExclusive > array.length) {
713             endIndexExclusive = array.length;
714         }
715         int newSize = endIndexExclusive - startIndexInclusive;
716         if (newSize <= 0) {
717             return EMPTY_FLOAT_ARRAY;
718         }
719
720         float[] subarray = new float[newSize];
721         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
722         return subarray;
723     }
724
725     /**
726      * <p>Produces a new <code>boolean</code> array containing the elements
727      * between the start and end indices.</p>
728      *
729      * <p>The start index is inclusive, the end index exclusive.
730      * Null array input produces null output.</p>
731      *
732      * @param array the array
733      * @param startIndexInclusive the starting index. Undervalue (&lt;0)
734      * is promoted to 0, overvalue (&gt;array.length) results
735      * in an empty array.
736      * @param endIndexExclusive elements up to endIndex-1 are present in the
737      * returned subarray. Undervalue (&lt; startIndex) produces
738      * empty array, overvalue (&gt;array.length) is demoted to
739      * array length.
740      * @return a new array containing the elements between
741      * the start and end indices.
742      * @since 2.1
743      */

744     public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) {
745         if (array == null) {
746             return null;
747         }
748         if (startIndexInclusive < 0) {
749             startIndexInclusive = 0;
750         }
751         if (endIndexExclusive > array.length) {
752             endIndexExclusive = array.length;
753         }
754         int newSize = endIndexExclusive - startIndexInclusive;
755         if (newSize <= 0) {
756             return EMPTY_BOOLEAN_ARRAY;
757         }
758
759         boolean[] subarray = new boolean[newSize];
760         System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
761         return subarray;
762     }
763
764     // Is same length
765
//-----------------------------------------------------------------------
766
/**
767      * <p>Checks whether two arrays are the same length, treating
768      * <code>null</code> arrays as length <code>0</code>.
769      *
770      * <p>Any multi-dimensional aspects of the arrays are ignored.</p>
771      *
772      * @param array1 the first array, may be <code>null</code>
773      * @param array2 the second array, may be <code>null</code>
774      * @return <code>true</code> if length of arrays matches, treating
775      * <code>null</code> as an empty array
776      */

777     public static boolean isSameLength(Object JavaDoc[] array1, Object JavaDoc[] array2) {
778         if ((array1 == null && array2 != null && array2.length > 0) ||
779             (array2 == null && array1 != null && array1.length > 0) ||
780             (array1 != null && array2 != null && array1.length != array2.length)) {
781                 return false;
782         }
783         return true;
784     }
785
786     /**
787      * <p>Checks whether two arrays are the same length, treating
788      * <code>null</code> arrays as length <code>0</code>.</p>
789      *
790      * @param array1 the first array, may be <code>null</code>
791      * @param array2 the second array, may be <code>null</code>
792      * @return <code>true</code> if length of arrays matches, treating
793      * <code>null</code> as an empty array
794      */

795     public static boolean isSameLength(long[] array1, long[] array2) {
796         if ((array1 == null && array2 != null && array2.length > 0) ||
797             (array2 == null && array1 != null && array1.length > 0) ||
798             (array1 != null && array2 != null && array1.length != array2.length)) {
799                 return false;
800         }
801         return true;
802     }
803
804     /**
805      * <p>Checks whether two arrays are the same length, treating
806      * <code>null</code> arrays as length <code>0</code>.</p>
807      *
808      * @param array1 the first array, may be <code>null</code>
809      * @param array2 the second array, may be <code>null</code>
810      * @return <code>true</code> if length of arrays matches, treating
811      * <code>null</code> as an empty array
812      */

813     public static boolean isSameLength(int[] array1, int[] array2) {
814         if ((array1 == null && array2 != null && array2.length > 0) ||
815             (array2 == null && array1 != null && array1.length > 0) ||
816             (array1 != null && array2 != null && array1.length != array2.length)) {
817                 return false;
818         }
819         return true;
820     }
821
822     /**
823      * <p>Checks whether two arrays are the same length, treating
824      * <code>null</code> arrays as length <code>0</code>.</p>
825      *
826      * @param array1 the first array, may be <code>null</code>
827      * @param array2 the second array, may be <code>null</code>
828      * @return <code>true</code> if length of arrays matches, treating
829      * <code>null</code> as an empty array
830      */

831     public static boolean isSameLength(short[] array1, short[] array2) {
832         if ((array1 == null && array2 != null && array2.length > 0) ||
833             (array2 == null && array1 != null && array1.length > 0) ||
834             (array1 != null && array2 != null && array1.length != array2.length)) {
835                 return false;
836         }
837         return true;
838     }
839
840     /**
841      * <p>Checks whether two arrays are the same length, treating
842      * <code>null</code> arrays as length <code>0</code>.</p>
843      *
844      * @param array1 the first array, may be <code>null</code>
845      * @param array2 the second array, may be <code>null</code>
846      * @return <code>true</code> if length of arrays matches, treating
847      * <code>null</code> as an empty array
848      */

849     public static boolean isSameLength(char[] array1, char[] array2) {
850         if ((array1 == null && array2 != null && array2.length > 0) ||
851             (array2 == null && array1 != null && array1.length > 0) ||
852             (array1 != null && array2 != null && array1.length != array2.length)) {
853                 return false;
854         }
855         return true;
856     }
857
858     /**
859      * <p>Checks whether two arrays are the same length, treating
860      * <code>null</code> arrays as length <code>0</code>.</p>
861      *
862      * @param array1 the first array, may be <code>null</code>
863      * @param array2 the second array, may be <code>null</code>
864      * @return <code>true</code> if length of arrays matches, treating
865      * <code>null</code> as an empty array
866      */

867     public static boolean isSameLength(byte[] array1, byte[] array2) {
868         if ((array1 == null && array2 != null && array2.length > 0) ||
869             (array2 == null && array1 != null && array1.length > 0) ||
870             (array1 != null && array2 != null && array1.length != array2.length)) {
871                 return false;
872         }
873         return true;
874     }
875
876     /**
877      * <p>Checks whether two arrays are the same length, treating
878      * <code>null</code> arrays as length <code>0</code>.</p>
879      *
880      * @param array1 the first array, may be <code>null</code>
881      * @param array2 the second array, may be <code>null</code>
882      * @return <code>true</code> if length of arrays matches, treating
883      * <code>null</code> as an empty array
884      */

885     public static boolean isSameLength(double[] array1, double[] array2) {
886         if ((array1 == null && array2 != null && array2.length > 0) ||
887             (array2 == null && array1 != null && array1.length > 0) ||
888             (array1 != null && array2 != null && array1.length != array2.length)) {
889                 return false;
890         }
891         return true;
892     }
893
894     /**
895      * <p>Checks whether two arrays are the same length, treating
896      * <code>null</code> arrays as length <code>0</code>.</p>
897      *
898      * @param array1 the first array, may be <code>null</code>
899      * @param array2 the second array, may be <code>null</code>
900      * @return <code>true</code> if length of arrays matches, treating
901      * <code>null</code> as an empty array
902      */

903     public static boolean isSameLength(float[] array1, float[] array2) {
904         if ((array1 == null && array2 != null && array2.length > 0) ||
905             (array2 == null && array1 != null && array1.length > 0) ||
906             (array1 != null && array2 != null && array1.length != array2.length)) {
907                 return false;
908         }
909         return true;
910     }
911
912     /**
913      * <p>Checks whether two arrays are the same length, treating
914      * <code>null</code> arrays as length <code>0</code>.</p>
915      *
916      * @param array1 the first array, may be <code>null</code>
917      * @param array2 the second array, may be <code>null</code>
918      * @return <code>true</code> if length of arrays matches, treating
919      * <code>null</code> as an empty array
920      */

921     public static boolean isSameLength(boolean[] array1, boolean[] array2) {
922         if ((array1 == null && array2 != null && array2.length > 0) ||
923             (array2 == null && array1 != null && array1.length > 0) ||
924             (array1 != null && array2 != null && array1.length != array2.length)) {
925                 return false;
926         }
927         return true;
928     }
929
930     //-----------------------------------------------------------------------
931
/**
932      * <p>Returns the length of the specified array.
933      * This method can deal with <code>Object</code> arrays and with primitive arrays.</p>
934      *
935      * <p>If the input array is <code>null</code>, <code>0</code> is returned.</p>
936      *
937      * <pre>
938      * ArrayUtils.getLength(null) = 0
939      * ArrayUtils.getLength([]) = 0
940      * ArrayUtils.getLength([null]) = 1
941      * ArrayUtils.getLength([true, false]) = 2
942      * ArrayUtils.getLength([1, 2, 3]) = 3
943      * ArrayUtils.getLength(["a", "b", "c"]) = 3
944      * </pre>
945      *
946      * @param array the array to retrieve the length from, may be null
947      * @return The length of the array, or <code>0</code> if the array is <code>null</code>
948      * @throws IllegalArgumentException if the object arguement is not an array.
949      * @since 2.1
950      */

951     public static int getLength(Object JavaDoc array) {
952         if (array == null) {
953             return 0;
954         } else {
955             return Array.getLength(array);
956         }
957     }
958
959     /**
960      * <p>Checks whether two arrays are the same type taking into account
961      * multi-dimensional arrays.</p>
962      *
963      * @param array1 the first array, must not be <code>null</code>
964      * @param array2 the second array, must not be <code>null</code>
965      * @return <code>true</code> if type of arrays matches
966      * @throws IllegalArgumentException if either array is <code>null</code>
967      */

968     public static boolean isSameType(Object JavaDoc array1, Object JavaDoc array2) {
969         if (array1 == null || array2 == null) {
970             throw new IllegalArgumentException JavaDoc("The Array must not be null");
971         }
972         return array1.getClass().getName().equals(array2.getClass().getName());
973     }
974
975     // Reverse
976
//-----------------------------------------------------------------------
977
/**
978      * <p>Reverses the order of the given array.</p>
979      *
980      * <p>There is no special handling for multi-dimensional arrays.</p>
981      *
982      * <p>This method does nothing if <code>null</code> array input.</p>
983      *
984      * @param array the array to reverse, may be <code>null</code>
985      */

986     public static void reverse(Object JavaDoc[] array) {
987         if (array == null) {
988             return;
989         }
990         int i = 0;
991         int j = array.length - 1;
992         Object JavaDoc tmp;
993         while (j > i) {
994             tmp = array[j];
995             array[j] = array[i];
996             array[i] = tmp;
997             j--;
998             i++;
999         }
1000    }
1001
1002    /**
1003     * <p>Reverses the order of the given array.</p>
1004     *
1005     * <p>This method does nothing if <code>null</code> array input.</p>
1006     *
1007     * @param array the array to reverse, may be <code>null</code>
1008     */

1009    public static void reverse(long[] array) {
1010        if (array == null) {
1011            return;
1012        }
1013        int i = 0;
1014        int j = array.length - 1;
1015        long tmp;
1016        while (j > i) {
1017            tmp = array[j];
1018            array[j] = array[i];
1019            array[i] = tmp;
1020            j--;
1021            i++;
1022        }
1023    }
1024
1025    /**
1026     * <p>Reverses the order of the given array.</p>
1027     *
1028     * <p>This method does nothing if <code>null</code> array input.</p>
1029     *
1030     * @param array the array to reverse, may be <code>null</code>
1031     */

1032    public static void reverse(int[] array) {
1033        if (array == null) {
1034            return;
1035        }
1036        int i = 0;
1037        int j = array.length - 1;
1038        int tmp;
1039        while (j > i) {
1040            tmp = array[j];
1041            array[j] = array[i];
1042            array[i] = tmp;
1043            j--;
1044            i++;
1045        }
1046    }
1047
1048    /**
1049     * <p>Reverses the order of the given array.</p>
1050     *
1051     * <p>This method does nothing if <code>null</code> array input.</p>
1052     *
1053     * @param array the array to reverse, may be <code>null</code>
1054     */

1055    public static void reverse(short[] array) {
1056        if (array == null) {
1057            return;
1058        }
1059        int i = 0;
1060        int j = array.length - 1;
1061        short tmp;
1062        while (j > i) {
1063            tmp = array[j];
1064            array[j] = array[i];
1065            array[i] = tmp;
1066            j--;
1067            i++;
1068        }
1069    }
1070
1071    /**
1072     * <p>Reverses the order of the given array.</p>
1073     *
1074     * <p>This method does nothing if <code>null</code> array input.</p>
1075     *
1076     * @param array the array to reverse, may be <code>null</code>
1077     */

1078    public static void reverse(char[] array) {
1079        if (array == null) {
1080            return;
1081        }
1082        int i = 0;
1083        int j = array.length - 1;
1084        char tmp;
1085        while (j > i) {
1086            tmp = array[j];
1087            array[j] = array[i];
1088            array[i] = tmp;
1089            j--;
1090            i++;
1091        }
1092    }
1093
1094    /**
1095     * <p>Reverses the order of the given array.</p>
1096     *
1097     * <p>This method does nothing if <code>null</code> array input.</p>
1098     *
1099     * @param array the array to reverse, may be <code>null</code>
1100     */

1101    public static void reverse(byte[] array) {
1102        if (array == null) {
1103            return;
1104        }
1105        int i = 0;
1106        int j = array.length - 1;
1107        byte tmp;
1108        while (j > i) {
1109            tmp = array[j];
1110            array[j] = array[i];
1111            array[i] = tmp;
1112            j--;
1113            i++;
1114        }
1115    }
1116
1117    /**
1118     * <p>Reverses the order of the given array.</p>
1119     *
1120     * <p>This method does nothing if <code>null</code> array input.</p>
1121     *
1122     * @param array the array to reverse, may be <code>null</code>
1123     */

1124    public static void reverse(double[] array) {
1125        if (array == null) {
1126            return;
1127        }
1128        int i = 0;
1129        int j = array.length - 1;
1130        double tmp;
1131        while (j > i) {
1132            tmp = array[j];
1133            array[j] = array[i];
1134            array[i] = tmp;
1135            j--;
1136            i++;
1137        }
1138    }
1139
1140    /**
1141     * <p>Reverses the order of the given array.</p>
1142     *
1143     * <p>This method does nothing if <code>null</code> array input.</p>
1144     *
1145     * @param array the array to reverse, may be <code>null</code>
1146     */

1147    public static void reverse(float[] array) {
1148        if (array == null) {
1149            return;
1150        }
1151        int i = 0;
1152        int j = array.length - 1;
1153        float tmp;
1154        while (j > i) {
1155            tmp = array[j];
1156            array[j] = array[i];
1157            array[i] = tmp;
1158            j--;
1159            i++;
1160        }
1161    }
1162
1163    /**
1164     * <p>Reverses the order of the given array.</p>
1165     *
1166     * <p>This method does nothing if <code>null</code> array input.</p>
1167     *
1168     * @param array the array to reverse, may be <code>null</code>
1169     */

1170    public static void reverse(boolean[] array) {
1171        if (array == null) {
1172            return;
1173        }
1174        int i = 0;
1175        int j = array.length - 1;
1176        boolean tmp;
1177        while (j > i) {
1178            tmp = array[j];
1179            array[j] = array[i];
1180            array[i] = tmp;
1181            j--;
1182            i++;
1183        }
1184    }
1185
1186    // IndexOf search
1187
// ----------------------------------------------------------------------
1188

1189    // Object IndexOf
1190
//-----------------------------------------------------------------------
1191
/**
1192     * <p>Find the index of the given object in the array.</p>
1193     *
1194     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1195     *
1196     * @param array the array to search through for the object, may be <code>null</code>
1197     * @param objectToFind the object to find, may be <code>null</code>
1198     * @return the index of the object within the array,
1199     * <code>-1</code> if not found or <code>null</code> array input
1200     */

1201    public static int indexOf(Object JavaDoc[] array, Object JavaDoc objectToFind) {
1202        return indexOf(array, objectToFind, 0);
1203    }
1204
1205    /**
1206     * <p>Find the index of the given object in the array starting at the given index.</p>
1207     *
1208     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1209     *
1210     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1211     * length will return <code>-1</code>.</p>
1212     *
1213     * @param array the array to search through for the object, may be <code>null</code>
1214     * @param objectToFind the object to find, may be <code>null</code>
1215     * @param startIndex the index to start searching at
1216     * @return the index of the object within the array starting at the index,
1217     * <code>-1</code> if not found or <code>null</code> array input
1218     */

1219    public static int indexOf(Object JavaDoc[] array, Object JavaDoc objectToFind, int startIndex) {
1220        if (array == null) {
1221            return -1;
1222        }
1223        if (startIndex < 0) {
1224            startIndex = 0;
1225        }
1226        if (objectToFind == null) {
1227            for (int i = startIndex; i < array.length; i++) {
1228                if (array[i] == null) {
1229                    return i;
1230                }
1231            }
1232        } else {
1233            for (int i = startIndex; i < array.length; i++) {
1234                if (objectToFind.equals(array[i])) {
1235                    return i;
1236                }
1237            }
1238        }
1239        return -1;
1240    }
1241
1242    /**
1243     * <p>Find the last index of the given object within the array.</p>
1244     *
1245     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1246     *
1247     * @param array the array to travers backwords looking for the object, may be <code>null</code>
1248     * @param objectToFind the object to find, may be <code>null</code>
1249     * @return the last index of the object within the array,
1250     * <code>-1</code> if not found or <code>null</code> array input
1251     */

1252    public static int lastIndexOf(Object JavaDoc[] array, Object JavaDoc objectToFind) {
1253        return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
1254    }
1255
1256    /**
1257     * <p>Find the last index of the given object in the array starting at the given index.</p>
1258     *
1259     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1260     *
1261     * <p>A negative startIndex will return <code>-1</code>. A startIndex larger than
1262     * the array length will search from the end of the array.</p>
1263     *
1264     * @param array the array to traverse for looking for the object, may be <code>null</code>
1265     * @param objectToFind the object to find, may be <code>null</code>
1266     * @param startIndex the start index to travers backwards from
1267     * @return the last index of the object within the array,
1268     * <code>-1</code> if not found or <code>null</code> array input
1269     */

1270    public static int lastIndexOf(Object JavaDoc[] array, Object JavaDoc objectToFind, int startIndex) {
1271        if (array == null) {
1272            return -1;
1273        }
1274        if (startIndex < 0) {
1275            return -1;
1276        } else if (startIndex >= array.length) {
1277            startIndex = array.length - 1;
1278        }
1279        if (objectToFind == null) {
1280            for (int i = startIndex; i >= 0; i--) {
1281                if (array[i] == null) {
1282                    return i;
1283                }
1284            }
1285        } else {
1286            for (int i = startIndex; i >= 0; i--) {
1287                if (objectToFind.equals(array[i])) {
1288                    return i;
1289                }
1290            }
1291        }
1292        return -1;
1293    }
1294
1295    /**
1296     * <p>Checks if the object is in the given array.</p>
1297     *
1298     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1299     *
1300     * @param array the array to search through
1301     * @param objectToFind the object to find
1302     * @return <code>true</code> if the array contains the object
1303     */

1304    public static boolean contains(Object JavaDoc[] array, Object JavaDoc objectToFind) {
1305        return indexOf(array, objectToFind) != -1;
1306    }
1307
1308    // long IndexOf
1309
//-----------------------------------------------------------------------
1310
/**
1311     * <p>Find the index of the given value in the array.</p>
1312     *
1313     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1314     *
1315     * @param array the array to search through for the object, may be <code>null</code>
1316     * @param valueToFind the value to find
1317     * @return the index of the value within the array,
1318     * <code>-1</code> if not found or <code>null</code> array input
1319     */

1320    public static int indexOf(long[] array, long valueToFind) {
1321        return indexOf(array, valueToFind, 0);
1322    }
1323
1324    /**
1325     * <p>Find the index of the given value in the array starting at the given index.</p>
1326     *
1327     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1328     *
1329     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1330     * length will return -1.</p>
1331     *
1332     * @param array the array to search through for the object, may be <code>null</code>
1333     * @param valueToFind the value to find
1334     * @param startIndex the index to start searching at
1335     * @return the index of the value within the array,
1336     * <code>-1</code> if not found or <code>null</code> array input
1337     */

1338    public static int indexOf(long[] array, long valueToFind, int startIndex) {
1339        if (array == null) {
1340            return -1;
1341        }
1342        if (startIndex < 0) {
1343            startIndex = 0;
1344        }
1345        for (int i = startIndex; i < array.length; i++) {
1346            if (valueToFind == array[i]) {
1347                return i;
1348            }
1349        }
1350        return -1;
1351    }
1352
1353    /**
1354     * <p>Find the last index of the given value within the array.</p>
1355     *
1356     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1357     *
1358     * @param array the array to travers backwords looking for the object, may be <code>null</code>
1359     * @param valueToFind the object to find
1360     * @return the last index of the value within the array,
1361     * <code>-1</code> if not found or <code>null</code> array input
1362     */

1363    public static int lastIndexOf(long[] array, long valueToFind) {
1364        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1365    }
1366
1367    /**
1368     * <p>Find the last index of the given value in the array starting at the given index.</p>
1369     *
1370     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1371     *
1372     * <p>A negative startIndex will return -1. A startIndex larger than the array
1373     * length will search from the end of the array.</p>
1374     *
1375     * @param array the array to traverse for looking for the object, may be <code>null</code>
1376     * @param valueToFind the value to find
1377     * @param startIndex the start index to travers backwards from
1378     * @return the last index of the value within the array,
1379     * <code>-1</code> if not found or <code>null</code> array input
1380     */

1381    public static int lastIndexOf(long[] array, long valueToFind, int startIndex) {
1382        if (array == null) {
1383            return -1;
1384        }
1385        if (startIndex < 0) {
1386            return -1;
1387        } else if (startIndex >= array.length) {
1388            startIndex = array.length - 1;
1389        }
1390        for (int i = startIndex; i >= 0; i--) {
1391            if (valueToFind == array[i]) {
1392                return i;
1393            }
1394        }
1395        return -1;
1396    }
1397
1398    /**
1399     * <p>Checks if the value is in the given array.</p>
1400     *
1401     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1402     *
1403     * @param array the array to search through
1404     * @param valueToFind the value to find
1405     * @return <code>true</code> if the array contains the object
1406     */

1407    public static boolean contains(long[] array, long valueToFind) {
1408        return indexOf(array, valueToFind) != -1;
1409    }
1410
1411    // int IndexOf
1412
//-----------------------------------------------------------------------
1413
/**
1414     * <p>Find the index of the given value in the array.</p>
1415     *
1416     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1417     *
1418     * @param array the array to search through for the object, may be <code>null</code>
1419     * @param valueToFind the value to find
1420     * @return the index of the value within the array,
1421     * <code>-1</code> if not found or <code>null</code> array input
1422     */

1423    public static int indexOf(int[] array, int valueToFind) {
1424        return indexOf(array, valueToFind, 0);
1425    }
1426
1427    /**
1428     * <p>Find the index of the given value in the array starting at the given index.</p>
1429     *
1430     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1431     *
1432     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1433     * length will return -1.</p>
1434     *
1435     * @param array the array to search through for the object, may be <code>null</code>
1436     * @param valueToFind the value to find
1437     * @param startIndex the index to start searching at
1438     * @return the index of the value within the array,
1439     * <code>-1</code> if not found or <code>null</code> array input
1440     */

1441    public static int indexOf(int[] array, int valueToFind, int startIndex) {
1442        if (array == null) {
1443            return -1;
1444        }
1445        if (startIndex < 0) {
1446            startIndex = 0;
1447        }
1448        for (int i = startIndex; i < array.length; i++) {
1449            if (valueToFind == array[i]) {
1450                return i;
1451            }
1452        }
1453        return -1;
1454    }
1455
1456    /**
1457     * <p>Find the last index of the given value within the array.</p>
1458     *
1459     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1460     *
1461     * @param array the array to travers backwords looking for the object, may be <code>null</code>
1462     * @param valueToFind the object to find
1463     * @return the last index of the value within the array,
1464     * <code>-1</code> if not found or <code>null</code> array input
1465     */

1466    public static int lastIndexOf(int[] array, int valueToFind) {
1467        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1468    }
1469
1470    /**
1471     * <p>Find the last index of the given value in the array starting at the given index.</p>
1472     *
1473     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1474     *
1475     * <p>A negative startIndex will return -1. A startIndex larger than the array
1476     * length will search from the end of the array.</p>
1477     *
1478     * @param array the array to traverse for looking for the object, may be <code>null</code>
1479     * @param valueToFind the value to find
1480     * @param startIndex the start index to travers backwards from
1481     * @return the last index of the value within the array,
1482     * <code>-1</code> if not found or <code>null</code> array input
1483     */

1484    public static int lastIndexOf(int[] array, int valueToFind, int startIndex) {
1485        if (array == null) {
1486            return -1;
1487        }
1488        if (startIndex < 0) {
1489            return -1;
1490        } else if (startIndex >= array.length) {
1491            startIndex = array.length - 1;
1492        }
1493        for (int i = startIndex; i >= 0; i--) {
1494            if (valueToFind == array[i]) {
1495                return i;
1496            }
1497        }
1498        return -1;
1499    }
1500
1501    /**
1502     * <p>Checks if the value is in the given array.</p>
1503     *
1504     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1505     *
1506     * @param array the array to search through
1507     * @param valueToFind the value to find
1508     * @return <code>true</code> if the array contains the object
1509     */

1510    public static boolean contains(int[] array, int valueToFind) {
1511        return indexOf(array, valueToFind) != -1;
1512    }
1513
1514    // short IndexOf
1515
//-----------------------------------------------------------------------
1516
/**
1517     * <p>Find the index of the given value in the array.</p>
1518     *
1519     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1520     *
1521     * @param array the array to search through for the object, may be <code>null</code>
1522     * @param valueToFind the value to find
1523     * @return the index of the value within the array,
1524     * <code>-1</code> if not found or <code>null</code> array input
1525     */

1526    public static int indexOf(short[] array, short valueToFind) {
1527        return indexOf(array, valueToFind, 0);
1528    }
1529
1530    /**
1531     * <p>Find the index of the given value in the array starting at the given index.</p>
1532     *
1533     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1534     *
1535     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1536     * length will return -1.</p>
1537     *
1538     * @param array the array to search through for the object, may be <code>null</code>
1539     * @param valueToFind the value to find
1540     * @param startIndex the index to start searching at
1541     * @return the index of the value within the array,
1542     * <code>-1</code> if not found or <code>null</code> array input
1543     */

1544    public static int indexOf(short[] array, short valueToFind, int startIndex) {
1545        if (array == null) {
1546            return -1;
1547        }
1548        if (startIndex < 0) {
1549            startIndex = 0;
1550        }
1551        for (int i = startIndex; i < array.length; i++) {
1552            if (valueToFind == array[i]) {
1553                return i;
1554            }
1555        }
1556        return -1;
1557    }
1558
1559    /**
1560     * <p>Find the last index of the given value within the array.</p>
1561     *
1562     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1563     *
1564     * @param array the array to travers backwords looking for the object, may be <code>null</code>
1565     * @param valueToFind the object to find
1566     * @return the last index of the value within the array,
1567     * <code>-1</code> if not found or <code>null</code> array input
1568     */

1569    public static int lastIndexOf(short[] array, short valueToFind) {
1570        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1571    }
1572
1573    /**
1574     * <p>Find the last index of the given value in the array starting at the given index.</p>
1575     *
1576     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1577     *
1578     * <p>A negative startIndex will return -1. A startIndex larger than the array
1579     * length will search from the end of the array.</p>
1580     *
1581     * @param array the array to traverse for looking for the object, may be <code>null</code>
1582     * @param valueToFind the value to find
1583     * @param startIndex the start index to travers backwards from
1584     * @return the last index of the value within the array,
1585     * <code>-1</code> if not found or <code>null</code> array input
1586     */

1587    public static int lastIndexOf(short[] array, short valueToFind, int startIndex) {
1588        if (array == null) {
1589            return -1;
1590        }
1591        if (startIndex < 0) {
1592            return -1;
1593        } else if (startIndex >= array.length) {
1594            startIndex = array.length - 1;
1595        }
1596        for (int i = startIndex; i >= 0; i--) {
1597            if (valueToFind == array[i]) {
1598                return i;
1599            }
1600        }
1601        return -1;
1602    }
1603
1604    /**
1605     * <p>Checks if the value is in the given array.</p>
1606     *
1607     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1608     *
1609     * @param array the array to search through
1610     * @param valueToFind the value to find
1611     * @return <code>true</code> if the array contains the object
1612     */

1613    public static boolean contains(short[] array, short valueToFind) {
1614        return indexOf(array, valueToFind) != -1;
1615    }
1616
1617    // char IndexOf
1618
//-----------------------------------------------------------------------
1619
/**
1620     * <p>Find the index of the given value in the array.</p>
1621     *
1622     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1623     *
1624     * @param array the array to search through for the object, may be <code>null</code>
1625     * @param valueToFind the value to find
1626     * @return the index of the value within the array,
1627     * <code>-1</code> if not found or <code>null</code> array input
1628     * @since 2.1
1629     */

1630    public static int indexOf(char[] array, char valueToFind) {
1631        return indexOf(array, valueToFind, 0);
1632    }
1633
1634    /**
1635     * <p>Find the index of the given value in the array starting at the given index.</p>
1636     *
1637     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1638     *
1639     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1640     * length will return -1.</p>
1641     *
1642     * @param array the array to search through for the object, may be <code>null</code>
1643     * @param valueToFind the value to find
1644     * @param startIndex the index to start searching at
1645     * @return the index of the value within the array,
1646     * <code>-1</code> if not found or <code>null</code> array input
1647     * @since 2.1
1648     */

1649    public static int indexOf(char[] array, char valueToFind, int startIndex) {
1650        if (array == null) {
1651            return -1;
1652        }
1653        if (startIndex < 0) {
1654            startIndex = 0;
1655        }
1656        for (int i = startIndex; i < array.length; i++) {
1657            if (valueToFind == array[i]) {
1658                return i;
1659            }
1660        }
1661        return -1;
1662    }
1663
1664    /**
1665     * <p>Find the last index of the given value within the array.</p>
1666     *
1667     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1668     *
1669     * @param array the array to travers backwords looking for the object, may be <code>null</code>
1670     * @param valueToFind the object to find
1671     * @return the last index of the value within the array,
1672     * <code>-1</code> if not found or <code>null</code> array input
1673     * @since 2.1
1674     */

1675    public static int lastIndexOf(char[] array, char valueToFind) {
1676        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1677    }
1678
1679    /**
1680     * <p>Find the last index of the given value in the array starting at the given index.</p>
1681     *
1682     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1683     *
1684     * <p>A negative startIndex will return -1. A startIndex larger than the array
1685     * length will search from the end of the array.</p>
1686     *
1687     * @param array the array to traverse for looking for the object, may be <code>null</code>
1688     * @param valueToFind the value to find
1689     * @param startIndex the start index to travers backwards from
1690     * @return the last index of the value within the array,
1691     * <code>-1</code> if not found or <code>null</code> array input
1692     * @since 2.1
1693     */

1694    public static int lastIndexOf(char[] array, char valueToFind, int startIndex) {
1695        if (array == null) {
1696            return -1;
1697        }
1698        if (startIndex < 0) {
1699            return -1;
1700        } else if (startIndex >= array.length) {
1701            startIndex = array.length - 1;
1702        }
1703        for (int i = startIndex; i >= 0; i--) {
1704            if (valueToFind == array[i]) {
1705                return i;
1706            }
1707        }
1708        return -1;
1709    }
1710
1711    /**
1712     * <p>Checks if the value is in the given array.</p>
1713     *
1714     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1715     *
1716     * @param array the array to search through
1717     * @param valueToFind the value to find
1718     * @return <code>true</code> if the array contains the object
1719     * @since 2.1
1720     */

1721    public static boolean contains(char[] array, char valueToFind) {
1722        return indexOf(array, valueToFind) != -1;
1723    }
1724
1725    // byte IndexOf
1726
//-----------------------------------------------------------------------
1727
/**
1728     * <p>Find the index of the given value in the array.</p>
1729     *
1730     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1731     *
1732     * @param array the array to search through for the object, may be <code>null</code>
1733     * @param valueToFind the value to find
1734     * @return the index of the value within the array,
1735     * <code>-1</code> if not found or <code>null</code> array input
1736     */

1737    public static int indexOf(byte[] array, byte valueToFind) {
1738        return indexOf(array, valueToFind, 0);
1739    }
1740
1741    /**
1742     * <p>Find the index of the given value in the array starting at the given index.</p>
1743     *
1744     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1745     *
1746     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1747     * length will return -1.</p>
1748     *
1749     * @param array the array to search through for the object, may be <code>null</code>
1750     * @param valueToFind the value to find
1751     * @param startIndex the index to start searching at
1752     * @return the index of the value within the array,
1753     * <code>-1</code> if not found or <code>null</code> array input
1754     */

1755    public static int indexOf(byte[] array, byte valueToFind, int startIndex) {
1756        if (array == null) {
1757            return -1;
1758        }
1759        if (startIndex < 0) {
1760            startIndex = 0;
1761        }
1762        for (int i = startIndex; i < array.length; i++) {
1763            if (valueToFind == array[i]) {
1764                return i;
1765            }
1766        }
1767        return -1;
1768    }
1769
1770    /**
1771     * <p>Find the last index of the given value within the array.</p>
1772     *
1773     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1774     *
1775     * @param array the array to travers backwords looking for the object, may be <code>null</code>
1776     * @param valueToFind the object to find
1777     * @return the last index of the value within the array,
1778     * <code>-1</code> if not found or <code>null</code> array input
1779     */

1780    public static int lastIndexOf(byte[] array, byte valueToFind) {
1781        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1782    }
1783
1784    /**
1785     * <p>Find the last index of the given value in the array starting at the given index.</p>
1786     *
1787     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1788     *
1789     * <p>A negative startIndex will return -1. A startIndex larger than the array
1790     * length will search from the end of the array.</p>
1791     *
1792     * @param array the array to traverse for looking for the object, may be <code>null</code>
1793     * @param valueToFind the value to find
1794     * @param startIndex the start index to travers backwards from
1795     * @return the last index of the value within the array,
1796     * <code>-1</code> if not found or <code>null</code> array input
1797     */

1798    public static int lastIndexOf(byte[] array, byte valueToFind, int startIndex) {
1799        if (array == null) {
1800            return -1;
1801        }
1802        if (startIndex < 0) {
1803            return -1;
1804        } else if (startIndex >= array.length) {
1805            startIndex = array.length - 1;
1806        }
1807        for (int i = startIndex; i >= 0; i--) {
1808            if (valueToFind == array[i]) {
1809                return i;
1810            }
1811        }
1812        return -1;
1813    }
1814
1815    /**
1816     * <p>Checks if the value is in the given array.</p>
1817     *
1818     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
1819     *
1820     * @param array the array to search through
1821     * @param valueToFind the value to find
1822     * @return <code>true</code> if the array contains the object
1823     */

1824    public static boolean contains(byte[] array, byte valueToFind) {
1825        return indexOf(array, valueToFind) != -1;
1826    }
1827
1828    // double IndexOf
1829
//-----------------------------------------------------------------------
1830
/**
1831     * <p>Find the index of the given value in the array.</p>
1832     *
1833     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1834     *
1835     * @param array the array to search through for the object, may be <code>null</code>
1836     * @param valueToFind the value to find
1837     * @return the index of the value within the array,
1838     * <code>-1</code> if not found or <code>null</code> array input
1839     */

1840    public static int indexOf(double[] array, double valueToFind) {
1841        return indexOf(array, valueToFind, 0);
1842    }
1843
1844    /**
1845     * <p>Find the index of the given value within a given tolerance in the array.
1846     * This method will return the index of the first value which falls between the region
1847     * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
1848     *
1849     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1850     *
1851     * @param array the array to search through for the object, may be <code>null</code>
1852     * @param valueToFind the value to find
1853     * @param tolerance tolerance of the search
1854     * @return the index of the value within the array,
1855     * <code>-1</code> if not found or <code>null</code> array input
1856     */

1857    public static int indexOf(double[] array, double valueToFind, double tolerance) {
1858        return indexOf(array, valueToFind, 0, tolerance);
1859    }
1860
1861    /**
1862     * <p>Find the index of the given value in the array starting at the given index.</p>
1863     *
1864     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1865     *
1866     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1867     * length will return -1.</p>
1868     *
1869     * @param array the array to search through for the object, may be <code>null</code>
1870     * @param valueToFind the value to find
1871     * @param startIndex the index to start searching at
1872     * @return the index of the value within the array,
1873     * <code>-1</code> if not found or <code>null</code> array input
1874     */

1875    public static int indexOf(double[] array, double valueToFind, int startIndex) {
1876        if (ArrayUtils.isEmpty(array)) {
1877            return -1;
1878        }
1879        if (startIndex < 0) {
1880            startIndex = 0;
1881        }
1882        for (int i = startIndex; i < array.length; i++) {
1883            if (valueToFind == array[i]) {
1884                return i;
1885            }
1886        }
1887        return -1;
1888    }
1889
1890    /**
1891     * <p>Find the index of the given value in the array starting at the given index.
1892     * This method will return the index of the first value which falls between the region
1893     * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
1894     *
1895     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1896     *
1897     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
1898     * length will return -1.</p>
1899     *
1900     * @param array the array to search through for the object, may be <code>null</code>
1901     * @param valueToFind the value to find
1902     * @param startIndex the index to start searching at
1903     * @param tolerance tolerance of the search
1904     * @return the index of the value within the array,
1905     * <code>-1</code> if not found or <code>null</code> array input
1906     */

1907    public static int indexOf(double[] array, double valueToFind, int startIndex, double tolerance) {
1908        if (ArrayUtils.isEmpty(array)) {
1909            return -1;
1910        }
1911        if (startIndex < 0) {
1912            startIndex = 0;
1913        }
1914        double min = valueToFind - tolerance;
1915        double max = valueToFind + tolerance;
1916        for (int i = startIndex; i < array.length; i++) {
1917            if (array[i] >= min && array[i] <= max) {
1918                return i;
1919            }
1920        }
1921        return -1;
1922    }
1923
1924    /**
1925     * <p>Find the last index of the given value within the array.</p>
1926     *
1927     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1928     *
1929     * @param array the array to travers backwords looking for the object, may be <code>null</code>
1930     * @param valueToFind the object to find
1931     * @return the last index of the value within the array,
1932     * <code>-1</code> if not found or <code>null</code> array input
1933     */

1934    public static int lastIndexOf(double[] array, double valueToFind) {
1935        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
1936    }
1937
1938    /**
1939     * <p>Find the last index of the given value within a given tolerance in the array.
1940     * This method will return the index of the last value which falls between the region
1941     * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
1942     *
1943     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1944     *
1945     * @param array the array to search through for the object, may be <code>null</code>
1946     * @param valueToFind the value to find
1947     * @param tolerance tolerance of the search
1948     * @return the index of the value within the array,
1949     * <code>-1</code> if not found or <code>null</code> array input
1950     */

1951    public static int lastIndexOf(double[] array, double valueToFind, double tolerance) {
1952        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance);
1953    }
1954
1955    /**
1956     * <p>Find the last index of the given value in the array starting at the given index.</p>
1957     *
1958     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1959     *
1960     * <p>A negative startIndex will return -1. A startIndex larger than the array
1961     * length will search from the end of the array.</p>
1962     *
1963     * @param array the array to traverse for looking for the object, may be <code>null</code>
1964     * @param valueToFind the value to find
1965     * @param startIndex the start index to travers backwards from
1966     * @return the last index of the value within the array,
1967     * <code>-1</code> if not found or <code>null</code> array input
1968     */

1969    public static int lastIndexOf(double[] array, double valueToFind, int startIndex) {
1970        if (ArrayUtils.isEmpty(array)) {
1971            return -1;
1972        }
1973        if (startIndex < 0) {
1974            return -1;
1975        } else if (startIndex >= array.length) {
1976            startIndex = array.length - 1;
1977        }
1978        for (int i = startIndex; i >= 0; i--) {
1979            if (valueToFind == array[i]) {
1980                return i;
1981            }
1982        }
1983        return -1;
1984    }
1985
1986    /**
1987     * <p>Find the last index of the given value in the array starting at the given index.
1988     * This method will return the index of the last value which falls between the region
1989     * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
1990     *
1991     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
1992     *
1993     * <p>A negative startIndex will return -1. A startIndex larger than the array
1994     * length will search from the end of the array.</p>
1995     *
1996     * @param array the array to traverse for looking for the object, may be <code>null</code>
1997     * @param valueToFind the value to find
1998     * @param startIndex the start index to travers backwards from
1999     * @param tolerance search for value within plus/minus this amount
2000     * @return the last index of the value within the array,
2001     * <code>-1</code> if not found or <code>null</code> array input
2002     */

2003    public static int lastIndexOf(double[] array, double valueToFind, int startIndex, double tolerance) {
2004        if (ArrayUtils.isEmpty(array)) {
2005            return -1;
2006        }
2007        if (startIndex < 0) {
2008            return -1;
2009        } else if (startIndex >= array.length) {
2010            startIndex = array.length - 1;
2011        }
2012        double min = valueToFind - tolerance;
2013        double max = valueToFind + tolerance;
2014        for (int i = startIndex; i >= 0; i--) {
2015            if (array[i] >= min && array[i] <= max) {
2016                return i;
2017            }
2018        }
2019        return -1;
2020    }
2021
2022    /**
2023     * <p>Checks if the value is in the given array.</p>
2024     *
2025     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
2026     *
2027     * @param array the array to search through
2028     * @param valueToFind the value to find
2029     * @return <code>true</code> if the array contains the object
2030     */

2031    public static boolean contains(double[] array, double valueToFind) {
2032        return indexOf(array, valueToFind) != -1;
2033    }
2034
2035    /**
2036     * <p>Checks if a value falling within the given tolerance is in the
2037     * given array. If the array contains a value within the inclusive range
2038     * defined by (value - tolerance) to (value + tolerance).</p>
2039     *
2040     * <p>The method returns <code>false</code> if a <code>null</code> array
2041     * is passed in.</p>
2042     *
2043     * @param array the array to search
2044     * @param valueToFind the value to find
2045     * @param tolerance the array contains the tolerance of the search
2046     * @return true if value falling within tolerance is in array
2047     */

2048    public static boolean contains(double[] array, double valueToFind, double tolerance) {
2049        return indexOf(array, valueToFind, 0, tolerance) != -1;
2050    }
2051
2052    // float IndexOf
2053
//-----------------------------------------------------------------------
2054
/**
2055     * <p>Find the index of the given value in the array.</p>
2056     *
2057     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2058     *
2059     * @param array the array to search through for the object, may be <code>null</code>
2060     * @param valueToFind the value to find
2061     * @return the index of the value within the array,
2062     * <code>-1</code> if not found or <code>null</code> array input
2063     */

2064    public static int indexOf(float[] array, float valueToFind) {
2065        return indexOf(array, valueToFind, 0);
2066    }
2067
2068    /**
2069     * <p>Find the index of the given value in the array starting at the given index.</p>
2070     *
2071     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2072     *
2073     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
2074     * length will return -1.</p>
2075     *
2076     * @param array the array to search through for the object, may be <code>null</code>
2077     * @param valueToFind the value to find
2078     * @param startIndex the index to start searching at
2079     * @return the index of the value within the array,
2080     * <code>-1</code> if not found or <code>null</code> array input
2081     */

2082    public static int indexOf(float[] array, float valueToFind, int startIndex) {
2083        if (ArrayUtils.isEmpty(array)) {
2084            return -1;
2085        }
2086        if (startIndex < 0) {
2087            startIndex = 0;
2088        }
2089        for (int i = startIndex; i < array.length; i++) {
2090            if (valueToFind == array[i]) {
2091                return i;
2092            }
2093        }
2094        return -1;
2095    }
2096
2097    /**
2098     * <p>Find the last index of the given value within the array.</p>
2099     *
2100     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2101     *
2102     * @param array the array to travers backwords looking for the object, may be <code>null</code>
2103     * @param valueToFind the object to find
2104     * @return the last index of the value within the array,
2105     * <code>-1</code> if not found or <code>null</code> array input
2106     */

2107    public static int lastIndexOf(float[] array, float valueToFind) {
2108        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
2109    }
2110
2111    /**
2112     * <p>Find the last index of the given value in the array starting at the given index.</p>
2113     *
2114     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2115     *
2116     * <p>A negative startIndex will return -1. A startIndex larger than the array
2117     * length will search from the end of the array.</p>
2118     *
2119     * @param array the array to traverse for looking for the object, may be <code>null</code>
2120     * @param valueToFind the value to find
2121     * @param startIndex the start index to travers backwards from
2122     * @return the last index of the value within the array,
2123     * <code>-1</code> if not found or <code>null</code> array input
2124     */

2125    public static int lastIndexOf(float[] array, float valueToFind, int startIndex) {
2126        if (ArrayUtils.isEmpty(array)) {
2127            return -1;
2128        }
2129        if (startIndex < 0) {
2130            return -1;
2131        } else if (startIndex >= array.length) {
2132            startIndex = array.length - 1;
2133        }
2134        for (int i = startIndex; i >= 0; i--) {
2135            if (valueToFind == array[i]) {
2136                return i;
2137            }
2138        }
2139        return -1;
2140    }
2141
2142    /**
2143     * <p>Checks if the value is in the given array.</p>
2144     *
2145     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
2146     *
2147     * @param array the array to search through
2148     * @param valueToFind the value to find
2149     * @return <code>true</code> if the array contains the object
2150     */

2151    public static boolean contains(float[] array, float valueToFind) {
2152        return indexOf(array, valueToFind) != -1;
2153    }
2154
2155    // boolean IndexOf
2156
//-----------------------------------------------------------------------
2157
/**
2158     * <p>Find the index of the given value in the array.</p>
2159     *
2160     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2161     *
2162     * @param array the array to search through for the object, may be <code>null</code>
2163     * @param valueToFind the value to find
2164     * @return the index of the value within the array,
2165     * <code>-1</code> if not found or <code>null</code> array input
2166     */

2167    public static int indexOf(boolean[] array, boolean valueToFind) {
2168        return indexOf(array, valueToFind, 0);
2169    }
2170
2171    /**
2172     * <p>Find the index of the given value in the array starting at the given index.</p>
2173     *
2174     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2175     *
2176     * <p>A negative startIndex is treated as zero. A startIndex larger than the array
2177     * length will return -1.</p>
2178     *
2179     * @param array the array to search through for the object, may be <code>null</code>
2180     * @param valueToFind the value to find
2181     * @param startIndex the index to start searching at
2182     * @return the index of the value within the array,
2183     * <code>-1</code> if not found or <code>null</code> array input
2184     */

2185    public static int indexOf(boolean[] array, boolean valueToFind, int startIndex) {
2186        if (ArrayUtils.isEmpty(array)) {
2187            return -1;
2188        }
2189        if (startIndex < 0) {
2190            startIndex = 0;
2191        }
2192        for (int i = startIndex; i < array.length; i++) {
2193            if (valueToFind == array[i]) {
2194                return i;
2195            }
2196        }
2197        return -1;
2198    }
2199
2200    /**
2201     * <p>Find the last index of the given value within the array.</p>
2202     *
2203     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2204     *
2205     * @param array the array to travers backwords looking for the object, may be <code>null</code>
2206     * @param valueToFind the object to find
2207     * @return the last index of the value within the array,
2208     * <code>-1</code> if not found or <code>null</code> array input
2209     */

2210    public static int lastIndexOf(boolean[] array, boolean valueToFind) {
2211        return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
2212    }
2213
2214    /**
2215     * <p>Find the last index of the given value in the array starting at the given index.</p>
2216     *
2217     * <p>This method returns <code>-1</code> if <code>null</code> array input.</p>
2218     *
2219     * <p>A negative startIndex will return -1. A startIndex larger than the array
2220     * length will search from the end of the array.</p>
2221     *
2222     * @param array the array to traverse for looking for the object, may be <code>null</code>
2223     * @param valueToFind the value to find
2224     * @param startIndex the start index to travers backwards from
2225     * @return the last index of the value within the array,
2226     * <code>-1</code> if not found or <code>null</code> array input
2227     */

2228    public static int lastIndexOf(boolean[] array, boolean valueToFind, int startIndex) {
2229        if (ArrayUtils.isEmpty(array)) {
2230            return -1;
2231        }
2232        if (startIndex < 0) {
2233            return -1;
2234        } else if (startIndex >= array.length) {
2235            startIndex = array.length - 1;
2236        }
2237        for (int i = startIndex; i >= 0; i--) {
2238            if (valueToFind == array[i]) {
2239                return i;
2240            }
2241        }
2242        return -1;
2243    }
2244
2245    /**
2246     * <p>Checks if the value is in the given array.</p>
2247     *
2248     * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
2249     *
2250     * @param array the array to search through
2251     * @param valueToFind the value to find
2252     * @return <code>true</code> if the array contains the object
2253     */

2254    public static boolean contains(boolean[] array, boolean valueToFind) {
2255        return indexOf(array, valueToFind) != -1;
2256    }
2257
2258    // Primitive/Object array converters
2259
// ----------------------------------------------------------------------
2260

2261    // Long array converters
2262
// ----------------------------------------------------------------------
2263
/**
2264     * <p>Converts an array of object Longs to primitives.</p>
2265     *
2266     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2267     *
2268     * @param array a <code>Long</code> array, may be <code>null</code>
2269     * @return a <code>long</code> array, <code>null</code> if null array input
2270     * @throws NullPointerException if array content is <code>null</code>
2271     */

2272    public static long[] toPrimitive(Long JavaDoc[] array) {
2273        if (array == null) {
2274            return null;
2275        } else if (array.length == 0) {
2276            return EMPTY_LONG_ARRAY;
2277        }
2278        final long[] result = new long[array.length];
2279        for (int i = 0; i < array.length; i++) {
2280            result[i] = array[i].longValue();
2281        }
2282        return result;
2283    }
2284    
2285    /**
2286     * <p>Converts an array of object Long to primitives handling <code>null</code>.</p>
2287     *
2288     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2289     *
2290     * @param array a <code>Long</code> array, may be <code>null</code>
2291     * @param valueForNull the value to insert if <code>null</code> found
2292     * @return a <code>long</code> array, <code>null</code> if null array input
2293     */

2294    public static long[] toPrimitive(Long JavaDoc[] array, long valueForNull) {
2295        if (array == null) {
2296            return null;
2297        } else if (array.length == 0) {
2298            return EMPTY_LONG_ARRAY;
2299        }
2300        final long[] result = new long[array.length];
2301        for (int i = 0; i < array.length; i++) {
2302            Long JavaDoc b = array[i];
2303            result[i] = (b == null ? valueForNull : b.longValue());
2304        }
2305        return result;
2306    }
2307    
2308    /**
2309     * <p>Converts an array of primitive longs to objects.</p>
2310     *
2311     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2312     *
2313     * @param array a <code>long</code> array
2314     * @return a <code>Long</code> array, <code>null</code> if null array input
2315     */

2316    public static Long JavaDoc[] toObject(long[] array) {
2317        if (array == null) {
2318            return null;
2319        } else if (array.length == 0) {
2320            return EMPTY_LONG_OBJECT_ARRAY;
2321        }
2322        final Long JavaDoc[] result = new Long JavaDoc[array.length];
2323        for (int i = 0; i < array.length; i++) {
2324            result[i] = new Long JavaDoc(array[i]);
2325        }
2326        return result;
2327    }
2328
2329    // Int array converters
2330
// ----------------------------------------------------------------------
2331
/**
2332     * <p>Converts an array of object Integers to primitives.</p>
2333     *
2334     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2335     *
2336     * @param array a <code>Integer</code> array, may be <code>null</code>
2337     * @return an <code>int</code> array, <code>null</code> if null array input
2338     * @throws NullPointerException if array content is <code>null</code>
2339     */

2340    public static int[] toPrimitive(Integer JavaDoc[] array) {
2341        if (array == null) {
2342            return null;
2343        } else if (array.length == 0) {
2344            return EMPTY_INT_ARRAY;
2345        }
2346        final int[] result = new int[array.length];
2347        for (int i = 0; i < array.length; i++) {
2348            result[i] = array[i].intValue();
2349        }
2350        return result;
2351    }
2352
2353    /**
2354     * <p>Converts an array of object Integer to primitives handling <code>null</code>.</p>
2355     *
2356     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2357     *
2358     * @param array a <code>Integer</code> array, may be <code>null</code>
2359     * @param valueForNull the value to insert if <code>null</code> found
2360     * @return an <code>int</code> array, <code>null</code> if null array input
2361     */

2362    public static int[] toPrimitive(Integer JavaDoc[] array, int valueForNull) {
2363        if (array == null) {
2364            return null;
2365        } else if (array.length == 0) {
2366            return EMPTY_INT_ARRAY;
2367        }
2368        final int[] result = new int[array.length];
2369        for (int i = 0; i < array.length; i++) {
2370            Integer JavaDoc b = array[i];
2371            result[i] = (b == null ? valueForNull : b.intValue());
2372        }
2373        return result;
2374    }
2375
2376    /**
2377     * <p>Converts an array of primitive ints to objects.</p>
2378     *
2379     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2380     *
2381     * @param array an <code>int</code> array
2382     * @return an <code>Integer</code> array, <code>null</code> if null array input
2383     */

2384    public static Integer JavaDoc[] toObject(int[] array) {
2385        if (array == null) {
2386            return null;
2387        } else if (array.length == 0) {
2388            return EMPTY_INTEGER_OBJECT_ARRAY;
2389        }
2390        final Integer JavaDoc[] result = new Integer JavaDoc[array.length];
2391        for (int i = 0; i < array.length; i++) {
2392            result[i] = new Integer JavaDoc(array[i]);
2393        }
2394        return result;
2395    }
2396    
2397    // Short array converters
2398
// ----------------------------------------------------------------------
2399
/**
2400     * <p>Converts an array of object Shorts to primitives.</p>
2401     *
2402     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2403     *
2404     * @param array a <code>Short</code> array, may be <code>null</code>
2405     * @return a <code>byte</code> array, <code>null</code> if null array input
2406     * @throws NullPointerException if array content is <code>null</code>
2407     */

2408    public static short[] toPrimitive(Short JavaDoc[] array) {
2409        if (array == null) {
2410            return null;
2411        } else if (array.length == 0) {
2412            return EMPTY_SHORT_ARRAY;
2413        }
2414        final short[] result = new short[array.length];
2415        for (int i = 0; i < array.length; i++) {
2416            result[i] = array[i].shortValue();
2417        }
2418        return result;
2419    }
2420
2421    /**
2422     * <p>Converts an array of object Short to primitives handling <code>null</code>.</p>
2423     *
2424     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2425     *
2426     * @param array a <code>Short</code> array, may be <code>null</code>
2427     * @param valueForNull the value to insert if <code>null</code> found
2428     * @return a <code>byte</code> array, <code>null</code> if null array input
2429     */

2430    public static short[] toPrimitive(Short JavaDoc[] array, short valueForNull) {
2431        if (array == null) {
2432            return null;
2433        } else if (array.length == 0) {
2434            return EMPTY_SHORT_ARRAY;
2435        }
2436        final short[] result = new short[array.length];
2437        for (int i = 0; i < array.length; i++) {
2438            Short JavaDoc b = array[i];
2439            result[i] = (b == null ? valueForNull : b.shortValue());
2440        }
2441        return result;
2442    }
2443
2444    /**
2445     * <p>Converts an array of primitive shorts to objects.</p>
2446     *
2447     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2448     *
2449     * @param array a <code>short</code> array
2450     * @return a <code>Short</code> array, <code>null</code> if null array input
2451     */

2452    public static Short JavaDoc[] toObject(short[] array) {
2453        if (array == null) {
2454            return null;
2455        } else if (array.length == 0) {
2456            return EMPTY_SHORT_OBJECT_ARRAY;
2457        }
2458        final Short JavaDoc[] result = new Short JavaDoc[array.length];
2459        for (int i = 0; i < array.length; i++) {
2460            result[i] = new Short JavaDoc(array[i]);
2461        }
2462        return result;
2463    }
2464
2465    // Byte array converters
2466
// ----------------------------------------------------------------------
2467
/**
2468     * <p>Converts an array of object Bytes to primitives.</p>
2469     *
2470     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2471     *
2472     * @param array a <code>Byte</code> array, may be <code>null</code>
2473     * @return a <code>byte</code> array, <code>null</code> if null array input
2474     * @throws NullPointerException if array content is <code>null</code>
2475     */

2476    public static byte[] toPrimitive(Byte JavaDoc[] array) {
2477        if (array == null) {
2478            return null;
2479        } else if (array.length == 0) {
2480            return EMPTY_BYTE_ARRAY;
2481        }
2482        final byte[] result = new byte[array.length];
2483        for (int i = 0; i < array.length; i++) {
2484            result[i] = array[i].byteValue();
2485        }
2486        return result;
2487    }
2488
2489    /**
2490     * <p>Converts an array of object Bytes to primitives handling <code>null</code>.</p>
2491     *
2492     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2493     *
2494     * @param array a <code>Byte</code> array, may be <code>null</code>
2495     * @param valueForNull the value to insert if <code>null</code> found
2496     * @return a <code>byte</code> array, <code>null</code> if null array input
2497     */

2498    public static byte[] toPrimitive(Byte JavaDoc[] array, byte valueForNull) {
2499        if (array == null) {
2500            return null;
2501        } else if (array.length == 0) {
2502            return EMPTY_BYTE_ARRAY;
2503        }
2504        final byte[] result = new byte[array.length];
2505        for (int i = 0; i < array.length; i++) {
2506            Byte JavaDoc b = array[i];
2507            result[i] = (b == null ? valueForNull : b.byteValue());
2508        }
2509        return result;
2510    }
2511
2512    /**
2513     * <p>Converts an array of primitive bytes to objects.</p>
2514     *
2515     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2516     *
2517     * @param array a <code>byte</code> array
2518     * @return a <code>Byte</code> array, <code>null</code> if null array input
2519     */

2520    public static Byte JavaDoc[] toObject(byte[] array) {
2521        if (array == null) {
2522            return null;
2523        } else if (array.length == 0) {
2524            return EMPTY_BYTE_OBJECT_ARRAY;
2525        }
2526        final Byte JavaDoc[] result = new Byte JavaDoc[array.length];
2527        for (int i = 0; i < array.length; i++) {
2528            result[i] = new Byte JavaDoc(array[i]);
2529        }
2530        return result;
2531    }
2532    
2533    // Double array converters
2534
// ----------------------------------------------------------------------
2535
/**
2536     * <p>Converts an array of object Doubles to primitives.</p>
2537     *
2538     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2539     *
2540     * @param array a <code>Double</code> array, may be <code>null</code>
2541     * @return a <code>double</code> array, <code>null</code> if null array input
2542     * @throws NullPointerException if array content is <code>null</code>
2543     */

2544    public static double[] toPrimitive(Double JavaDoc[] array) {
2545        if (array == null) {
2546            return null;
2547        } else if (array.length == 0) {
2548            return EMPTY_DOUBLE_ARRAY;
2549        }
2550        final double[] result = new double[array.length];
2551        for (int i = 0; i < array.length; i++) {
2552            result[i] = array[i].doubleValue();
2553        }
2554        return result;
2555    }
2556
2557    /**
2558     * <p>Converts an array of object Doubles to primitives handling <code>null</code>.</p>
2559     *
2560     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2561     *
2562     * @param array a <code>Double</code> array, may be <code>null</code>
2563     * @param valueForNull the value to insert if <code>null</code> found
2564     * @return a <code>double</code> array, <code>null</code> if null array input
2565     */

2566    public static double[] toPrimitive(Double JavaDoc[] array, double valueForNull) {
2567        if (array == null) {
2568            return null;
2569        } else if (array.length == 0) {
2570            return EMPTY_DOUBLE_ARRAY;
2571        }
2572        final double[] result = new double[array.length];
2573        for (int i = 0; i < array.length; i++) {
2574            Double JavaDoc b = array[i];
2575            result[i] = (b == null ? valueForNull : b.doubleValue());
2576        }
2577        return result;
2578    }
2579
2580    /**
2581     * <p>Converts an array of primitive doubles to objects.</p>
2582     *
2583     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2584     *
2585     * @param array a <code>double</code> array
2586     * @return a <code>Double</code> array, <code>null</code> if null array input
2587     */

2588    public static Double JavaDoc[] toObject(double[] array) {
2589        if (array == null) {
2590            return null;
2591        } else if (array.length == 0) {
2592            return EMPTY_DOUBLE_OBJECT_ARRAY;
2593        }
2594        final Double JavaDoc[] result = new Double JavaDoc[array.length];
2595        for (int i = 0; i < array.length; i++) {
2596            result[i] = new Double JavaDoc(array[i]);
2597        }
2598        return result;
2599    }
2600
2601    // Float array converters
2602
// ----------------------------------------------------------------------
2603
/**
2604     * <p>Converts an array of object Floats to primitives.</p>
2605     *
2606     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2607     *
2608     * @param array a <code>Float</code> array, may be <code>null</code>
2609     * @return a <code>float</code> array, <code>null</code> if null array input
2610     * @throws NullPointerException if array content is <code>null</code>
2611     */

2612    public static float[] toPrimitive(Float JavaDoc[] array) {
2613        if (array == null) {
2614            return null;
2615        } else if (array.length == 0) {
2616            return EMPTY_FLOAT_ARRAY;
2617        }
2618        final float[] result = new float[array.length];
2619        for (int i = 0; i < array.length; i++) {
2620            result[i] = array[i].floatValue();
2621        }
2622        return result;
2623    }
2624
2625    /**
2626     * <p>Converts an array of object Floats to primitives handling <code>null</code>.</p>
2627     *
2628     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2629     *
2630     * @param array a <code>Float</code> array, may be <code>null</code>
2631     * @param valueForNull the value to insert if <code>null</code> found
2632     * @return a <code>float</code> array, <code>null</code> if null array input
2633     */

2634    public static float[] toPrimitive(Float JavaDoc[] array, float valueForNull) {
2635        if (array == null) {
2636            return null;
2637        } else if (array.length == 0) {
2638            return EMPTY_FLOAT_ARRAY;
2639        }
2640        final float[] result = new float[array.length];
2641        for (int i = 0; i < array.length; i++) {
2642            Float JavaDoc b = array[i];
2643            result[i] = (b == null ? valueForNull : b.floatValue());
2644        }
2645        return result;
2646    }
2647
2648    /**
2649     * <p>Converts an array of primitive floats to objects.</p>
2650     *
2651     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2652     *
2653     * @param array a <code>float</code> array
2654     * @return a <code>Float</code> array, <code>null</code> if null array input
2655     */

2656    public static Float JavaDoc[] toObject(float[] array) {
2657        if (array == null) {
2658            return null;
2659        } else if (array.length == 0) {
2660            return EMPTY_FLOAT_OBJECT_ARRAY;
2661        }
2662        final Float JavaDoc[] result = new Float JavaDoc[array.length];
2663        for (int i = 0; i < array.length; i++) {
2664            result[i] = new Float JavaDoc(array[i]);
2665        }
2666        return result;
2667    }
2668
2669    // Boolean array converters
2670
// ----------------------------------------------------------------------
2671
/**
2672     * <p>Converts an array of object Booleans to primitives.</p>
2673     *
2674     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2675     *
2676     * @param array a <code>Boolean</code> array, may be <code>null</code>
2677     * @return a <code>boolean</code> array, <code>null</code> if null array input
2678     * @throws NullPointerException if array content is <code>null</code>
2679     */

2680    public static boolean[] toPrimitive(Boolean JavaDoc[] array) {
2681        if (array == null) {
2682            return null;
2683        } else if (array.length == 0) {
2684            return EMPTY_BOOLEAN_ARRAY;
2685        }
2686        final boolean[] result = new boolean[array.length];
2687        for (int i = 0; i < array.length; i++) {
2688            result[i] = array[i].booleanValue();
2689        }
2690        return result;
2691    }
2692
2693    /**
2694     * <p>Converts an array of object Booleans to primitives handling <code>null</code>.</p>
2695     *
2696     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2697     *
2698     * @param array a <code>Boolean</code> array, may be <code>null</code>
2699     * @param valueForNull the value to insert if <code>null</code> found
2700     * @return a <code>boolean</code> array, <code>null</code> if null array input
2701     */

2702    public static boolean[] toPrimitive(Boolean JavaDoc[] array, boolean valueForNull) {
2703        if (array == null) {
2704            return null;
2705        } else if (array.length == 0) {
2706            return EMPTY_BOOLEAN_ARRAY;
2707        }
2708        final boolean[] result = new boolean[array.length];
2709        for (int i = 0; i < array.length; i++) {
2710            Boolean JavaDoc b = array[i];
2711            result[i] = (b == null ? valueForNull : b.booleanValue());
2712        }
2713        return result;
2714    }
2715
2716    /**
2717     * <p>Converts an array of primitive booleans to objects.</p>
2718     *
2719     * <p>This method returns <code>null</code> if <code>null</code> array input.</p>
2720     *
2721     * @param array a <code>boolean</code> array
2722     * @return a <code>Boolean</code> array, <code>null</code> if null array input
2723     */

2724    public static Boolean JavaDoc[] toObject(boolean[] array) {
2725        if (array == null) {
2726            return null;
2727        } else if (array.length == 0) {
2728            return EMPTY_BOOLEAN_OBJECT_ARRAY;
2729        }
2730        final Boolean JavaDoc[] result = new Boolean JavaDoc[array.length];
2731        for (int i = 0; i < array.length; i++) {
2732            result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
2733        }
2734        return result;
2735    }
2736
2737    // ----------------------------------------------------------------------
2738
/**
2739     * <p>Checks if an array of Objects is empty or <code>null</code>.</p>
2740     *
2741     * @param array the array to test
2742     * @return <code>true</code> if the array is empty or <code>null</code>
2743     * @since 2.1
2744     */

2745    public static boolean isEmpty(Object JavaDoc[] array) {
2746        if (array == null || array.length == 0) {
2747            return true;
2748        }
2749        return false;
2750    }
2751
2752    /**
2753     * <p>Checks if an array of primitive longs is empty or <code>null</code>.</p>
2754     *
2755     * @param array the array to test
2756     * @return <code>true</code> if the array is empty or <code>null</code>
2757     * @since 2.1
2758     */

2759    public static boolean isEmpty(long[] array) {
2760        if (array == null || array.length == 0) {
2761            return true;
2762        }
2763        return false;
2764    }
2765
2766    /**
2767     * <p>Checks if an array of primitive ints is empty or <code>null</code>.</p>
2768     *
2769     * @param array the array to test
2770     * @return <code>true</code> if the array is empty or <code>null</code>
2771     * @since 2.1
2772     */

2773    public static boolean isEmpty(int[] array) {
2774        if (array == null || array.length == 0) {
2775            return true;
2776        }
2777        return false;
2778    }
2779
2780    /**
2781     * <p>Checks if an array of primitive shorts is empty or <code>null</code>.</p>
2782     *
2783     * @param array the array to test
2784     * @return <code>true</code> if the array is empty or <code>null</code>
2785     * @since 2.1
2786     */

2787    public static boolean isEmpty(short[] array) {
2788        if (array == null || array.length == 0) {
2789            return true;
2790        }
2791        return false;
2792    }
2793
2794    /**
2795     * <p>Checks if an array of primitive chars is empty or <code>null</code>.</p>
2796     *
2797     * @param array the array to test
2798     * @return <code>true</code> if the array is empty or <code>null</code>
2799     * @since 2.1
2800     */

2801    public static boolean isEmpty(char[] array) {
2802        if (array == null || array.length == 0) {
2803            return true;
2804        }
2805        return false;
2806    }
2807
2808    /**
2809     * <p>Checks if an array of primitive bytes is empty or <code>null</code>.</p>
2810     *
2811     * @param array the array to test
2812     * @return <code>true</code> if the array is empty or <code>null</code>
2813     * @since 2.1
2814     */

2815    public static boolean isEmpty(byte[] array) {
2816        if (array == null || array.length == 0) {
2817            return true;
2818        }
2819        return false;
2820    }
2821
2822    /**
2823     * <p>Checks if an array of primitive doubles is empty or <code>null</code>.</p>
2824     *
2825     * @param array the array to test
2826     * @return <code>true</code> if the array is empty or <code>null</code>
2827     * @since 2.1
2828     */

2829    public static boolean isEmpty(double[] array) {
2830        if (array == null || array.length == 0) {
2831            return true;
2832        }
2833        return false;
2834    }
2835
2836    /**
2837     * <p>Checks if an array of primitive floats is empty or <code>null</code>.</p>
2838     *
2839     * @param array the array to test
2840     * @return <code>true</code> if the array is empty or <code>null</code>
2841     * @since 2.1
2842     */

2843    public static boolean isEmpty(float[] array) {
2844        if (array == null || array.length == 0) {
2845            return true;
2846        }
2847        return false;
2848    }
2849
2850    /**
2851     * <p>Checks if an array of primitive booleans is empty or <code>null</code>.</p>
2852     *
2853     * @param array the array to test
2854     * @return <code>true</code> if the array is empty or <code>null</code>
2855     * @since 2.1
2856     */

2857    public static boolean isEmpty(boolean[] array) {
2858        if (array == null || array.length == 0) {
2859            return true;
2860        }
2861        return false;
2862    }
2863
2864    /**
2865     * <p>Adds all the elements of the given arrays into a new array.</p>
2866     * <p>The new array contains all of the element of <code>array1</code> followed
2867     * by all of the elements <code>array2</code>. When an array is returned, it is always
2868     * a new array.</p>
2869     *
2870     * <pre>
2871     * ArrayUtils.addAll(null, null) = null
2872     * ArrayUtils.addAll(array1, null) = cloned copy of array1
2873     * ArrayUtils.addAll(null, array2) = cloned copy of array2
2874     * ArrayUtils.addAll([], []) = []
2875     * ArrayUtils.addAll([null], [null]) = [null, null]
2876     * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
2877     * </pre>
2878     *
2879     * @param array1 the first array whose elements are added to the new array, may be <code>null</code>
2880     * @param array2 the second array whose elements are added to the new array, may be <code>null</code>
2881     * @return The new array, <code>null</code> if <code>null</code> array inputs.
2882     * The type of the new array is the type of the first array.
2883     * @since 2.1
2884     */

2885    public static Object JavaDoc[] addAll(Object JavaDoc[] array1, Object JavaDoc[] array2) {
2886        if (array1 == null) {
2887            return clone(array2);
2888        } else if (array2 == null) {
2889            return clone(array1);
2890        }
2891        Object JavaDoc[] joinedArray = (Object JavaDoc[]) Array.newInstance(array1.getClass().getComponentType(),
2892                                                            array1.length + array2.length);
2893        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
2894        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
2895        return joinedArray;
2896    }
2897
2898    /**
2899     * <p>Adds all the elements of the given arrays into a new array.</p>
2900     * <p>The new array contains all of the element of <code>array1</code> followed
2901     * by all of the elements <code>array2</code>. When an array is returned, it is always
2902     * a new array.</p>
2903     *
2904     * <pre>
2905     * ArrayUtils.addAll(array1, null) = cloned copy of array1
2906     * ArrayUtils.addAll(null, array2) = cloned copy of array2
2907     * ArrayUtils.addAll([], []) = []
2908     * </pre>
2909     *
2910     * @param array1 the first array whose elements are added to the new array.
2911     * @param array2 the second array whose elements are added to the new array.
2912     * @return The new boolean[] array.
2913     * @since 2.1
2914     */

2915    public static boolean[] addAll(boolean[] array1, boolean[] array2) {
2916        if (array1 == null) {
2917            return clone(array2);
2918        } else if (array2 == null) {
2919            return clone(array1);
2920        }
2921        boolean[] joinedArray = new boolean[array1.length + array2.length];
2922        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
2923        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
2924        return joinedArray;
2925    }
2926
2927    /**
2928     * <p>Adds all the elements of the given arrays into a new array.</p>
2929     * <p>The new array contains all of the element of <code>array1</code> followed
2930     * by all of the elements <code>array2</code>. When an array is returned, it is always
2931     * a new array.</p>
2932     *
2933     * <pre>
2934     * ArrayUtils.addAll(array1, null) = cloned copy of array1
2935     * ArrayUtils.addAll(null, array2) = cloned copy of array2
2936     * ArrayUtils.addAll([], []) = []
2937     * </pre>
2938     *
2939     * @param array1 the first array whose elements are added to the new array.
2940     * @param array2 the second array whose elements are added to the new array.
2941     * @return The new char[] array.
2942     * @since 2.1
2943     */

2944    public static char[] addAll(char[] array1, char[] array2) {
2945        if (array1 == null) {
2946            return clone(array2);
2947        } else if (array2 == null) {
2948            return clone(array1);
2949        }
2950        char[] joinedArray = new char[array1.length + array2.length];
2951        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
2952        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
2953        return joinedArray;
2954    }
2955
2956    /**
2957     * <p>Adds all the elements of the given arrays into a new array.</p>
2958     * <p>The new array contains all of the element of <code>array1</code> followed
2959     * by all of the elements <code>array2</code>. When an array is returned, it is always
2960     * a new array.</p>
2961     *
2962     * <pre>
2963     * ArrayUtils.addAll(array1, null) = cloned copy of array1
2964     * ArrayUtils.addAll(null, array2) = cloned copy of array2
2965     * ArrayUtils.addAll([], []) = []
2966     * </pre>
2967     *
2968     * @param array1 the first array whose elements are added to the new array.
2969     * @param array2 the second array whose elements are added to the new array.
2970     * @return The new byte[] array.
2971     * @since 2.1
2972     */

2973    public static byte[] addAll(byte[] array1, byte[] array2) {
2974        if (array1 == null) {
2975            return clone(array2);
2976        } else if (array2 == null) {
2977            return clone(array1);
2978        }
2979        byte[] joinedArray = new byte[array1.length + array2.length];
2980        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
2981        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
2982        return joinedArray;
2983    }
2984
2985    /**
2986     * <p>Adds all the elements of the given arrays into a new array.</p>
2987     * <p>The new array contains all of the element of <code>array1</code> followed
2988     * by all of the elements <code>array2</code>. When an array is returned, it is always
2989     * a new array.</p>
2990     *
2991     * <pre>
2992     * ArrayUtils.addAll(array1, null) = cloned copy of array1
2993     * ArrayUtils.addAll(null, array2) = cloned copy of array2
2994     * ArrayUtils.addAll([], []) = []
2995     * </pre>
2996     *
2997     * @param array1 the first array whose elements are added to the new array.
2998     * @param array2 the second array whose elements are added to the new array.
2999     * @return The new short[] array.
3000     * @since 2.1
3001     */

3002    public static short[] addAll(short[] array1, short[] array2) {
3003        if (array1 == null) {
3004            return clone(array2);
3005        } else if (array2 == null) {
3006            return clone(array1);
3007        }
3008        short[] joinedArray = new short[array1.length + array2.length];
3009        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3010        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
3011        return joinedArray;
3012    }
3013
3014    /**
3015     * <p>Adds all the elements of the given arrays into a new array.</p>
3016     * <p>The new array contains all of the element of <code>array1</code> followed
3017     * by all of the elements <code>array2</code>. When an array is returned, it is always
3018     * a new array.</p>
3019     *
3020     * <pre>
3021     * ArrayUtils.addAll(array1, null) = cloned copy of array1
3022     * ArrayUtils.addAll(null, array2) = cloned copy of array2
3023     * ArrayUtils.addAll([], []) = []
3024     * </pre>
3025     *
3026     * @param array1 the first array whose elements are added to the new array.
3027     * @param array2 the second array whose elements are added to the new array.
3028     * @return The new int[] array.
3029     * @since 2.1
3030     */

3031    public static int[] addAll(int[] array1, int[] array2) {
3032        if (array1 == null) {
3033            return clone(array2);
3034        } else if (array2 == null) {
3035            return clone(array1);
3036        }
3037        int[] joinedArray = new int[array1.length + array2.length];
3038        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3039        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
3040        return joinedArray;
3041    }
3042
3043    /**
3044     * <p>Adds all the elements of the given arrays into a new array.</p>
3045     * <p>The new array contains all of the element of <code>array1</code> followed
3046     * by all of the elements <code>array2</code>. When an array is returned, it is always
3047     * a new array.</p>
3048     *
3049     * <pre>
3050     * ArrayUtils.addAll(array1, null) = cloned copy of array1
3051     * ArrayUtils.addAll(null, array2) = cloned copy of array2
3052     * ArrayUtils.addAll([], []) = []
3053     * </pre>
3054     *
3055     * @param array1 the first array whose elements are added to the new array.
3056     * @param array2 the second array whose elements are added to the new array.
3057     * @return The new long[] array.
3058     * @since 2.1
3059     */

3060    public static long[] addAll(long[] array1, long[] array2) {
3061        if (array1 == null) {
3062            return clone(array2);
3063        } else if (array2 == null) {
3064            return clone(array1);
3065        }
3066        long[] joinedArray = new long[array1.length + array2.length];
3067        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3068        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
3069        return joinedArray;
3070    }
3071
3072    /**
3073     * <p>Adds all the elements of the given arrays into a new array.</p>
3074     * <p>The new array contains all of the element of <code>array1</code> followed
3075     * by all of the elements <code>array2</code>. When an array is returned, it is always
3076     * a new array.</p>
3077     *
3078     * <pre>
3079     * ArrayUtils.addAll(array1, null) = cloned copy of array1
3080     * ArrayUtils.addAll(null, array2) = cloned copy of array2
3081     * ArrayUtils.addAll([], []) = []
3082     * </pre>
3083     *
3084     * @param array1 the first array whose elements are added to the new array.
3085     * @param array2 the second array whose elements are added to the new array.
3086     * @return The new float[] array.
3087     * @since 2.1
3088     */

3089    public static float[] addAll(float[] array1, float[] array2) {
3090        if (array1 == null) {
3091            return clone(array2);
3092        } else if (array2 == null) {
3093            return clone(array1);
3094        }
3095        float[] joinedArray = new float[array1.length + array2.length];
3096        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3097        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
3098        return joinedArray;
3099    }
3100
3101    /**
3102     * <p>Adds all the elements of the given arrays into a new array.</p>
3103     * <p>The new array contains all of the element of <code>array1</code> followed
3104     * by all of the elements <code>array2</code>. When an array is returned, it is always
3105     * a new array.</p>
3106     *
3107     * <pre>
3108     * ArrayUtils.addAll(array1, null) = cloned copy of array1
3109     * ArrayUtils.addAll(null, array2) = cloned copy of array2
3110     * ArrayUtils.addAll([], []) = []
3111     * </pre>
3112     *
3113     * @param array1 the first array whose elements are added to the new array.
3114     * @param array2 the second array whose elements are added to the new array.
3115     * @return The new double[] array.
3116     * @since 2.1
3117     */

3118    public static double[] addAll(double[] array1, double[] array2) {
3119        if (array1 == null) {
3120            return clone(array2);
3121        } else if (array2 == null) {
3122            return clone(array1);
3123        }
3124        double[] joinedArray = new double[array1.length + array2.length];
3125        System.arraycopy(array1, 0, joinedArray, 0, array1.length);
3126        System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
3127        return joinedArray;
3128    }
3129
3130    /**
3131     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3132     *
3133     * <p>The new array contains the same elements of the input
3134     * array plus the given element in the last position. The component type of
3135     * the new array is the same as that of the input array.</p>
3136     *
3137     * <p>If the input array is <code>null</code>, a new one element array is returned
3138     * whose component type is the same as the element.</p>
3139     *
3140     * <pre>
3141     * ArrayUtils.add(null, null) = [null]
3142     * ArrayUtils.add(null, "a") = ["a"]
3143     * ArrayUtils.add(["a"], null) = ["a", null]
3144     * ArrayUtils.add(["a"], "b") = ["a", "b"]
3145     * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
3146     * </pre>
3147     *
3148     * @param array the array to "add" the element to, may be <code>null</code>
3149     * @param element the object to add
3150     * @return A new array containing the existing elements plus the new element
3151     * @since 2.1
3152     */

3153    public static Object JavaDoc[] add(Object JavaDoc[] array, Object JavaDoc element) {
3154        Class JavaDoc type = (array != null ? array.getClass() : (element != null ? element.getClass() : Object JavaDoc.class));
3155        Object JavaDoc[] newArray = (Object JavaDoc[]) copyArrayGrow1(array, type);
3156        newArray[newArray.length - 1] = element;
3157        return newArray;
3158    }
3159    
3160    /**
3161     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3162     *
3163     * <p>The new array contains the same elements of the input
3164     * array plus the given element in the last position. The component type of
3165     * the new array is the same as that of the input array.</p>
3166     *
3167     * <p>If the input array is <code>null</code>, a new one element array is returned
3168     * whose component type is the same as the element.</p>
3169     *
3170     * <pre>
3171     * ArrayUtils.add(null, true) = [true]
3172     * ArrayUtils.add([true], false) = [true, false]
3173     * ArrayUtils.add([true, false], true) = [true, false, true]
3174     * </pre>
3175     *
3176     * @param array the array to copy and add the element to, may be <code>null</code>
3177     * @param element the object to add at the last index of the new array
3178     * @return A new array containing the existing elements plus the new element
3179     * @since 2.1
3180     */

3181    public static boolean[] add(boolean[] array, boolean element) {
3182        boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE);
3183        newArray[newArray.length - 1] = element;
3184        return newArray;
3185    }
3186    
3187    /**
3188     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3189     *
3190     * <p>The new array contains the same elements of the input
3191     * array plus the given element in the last position. The component type of
3192     * the new array is the same as that of the input array.</p>
3193     *
3194     * <p>If the input array is <code>null</code>, a new one element array is returned
3195     * whose component type is the same as the element.</p>
3196     *
3197     * <pre>
3198     * ArrayUtils.add(null, 0) = [0]
3199     * ArrayUtils.add([1], 0) = [1, 0]
3200     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3201     * </pre>
3202     *
3203     * @param array the array to copy and add the element to, may be <code>null</code>
3204     * @param element the object to add at the last index of the new array
3205     * @return A new array containing the existing elements plus the new element
3206     * @since 2.1
3207     */

3208    public static byte[] add(byte[] array, byte element) {
3209        byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE);
3210        newArray[newArray.length - 1] = element;
3211        return newArray;
3212    }
3213    
3214    /**
3215     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3216     *
3217     * <p>The new array contains the same elements of the input
3218     * array plus the given element in the last position. The component type of
3219     * the new array is the same as that of the input array.</p>
3220     *
3221     * <p>If the input array is <code>null</code>, a new one element array is returned
3222     * whose component type is the same as the element.</p>
3223     *
3224     * <pre>
3225     * ArrayUtils.add(null, '0') = ['0']
3226     * ArrayUtils.add(['1'], '0') = ['1', '0']
3227     * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
3228     * </pre>
3229     *
3230     * @param array the array to copy and add the element to, may be <code>null</code>
3231     * @param element the object to add at the last index of the new array
3232     * @return A new array containing the existing elements plus the new element
3233     * @since 2.1
3234     */

3235    public static char[] add(char[] array, char element) {
3236        char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE);
3237        newArray[newArray.length - 1] = element;
3238        return newArray;
3239    }
3240    
3241    /**
3242     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3243     *
3244     * <p>The new array contains the same elements of the input
3245     * array plus the given element in the last position. The component type of
3246     * the new array is the same as that of the input array.</p>
3247     *
3248     * <p>If the input array is <code>null</code>, a new one element array is returned
3249     * whose component type is the same as the element.</p>
3250     *
3251     * <pre>
3252     * ArrayUtils.add(null, 0) = [0]
3253     * ArrayUtils.add([1], 0) = [1, 0]
3254     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3255     * </pre>
3256     *
3257     * @param array the array to copy and add the element to, may be <code>null</code>
3258     * @param element the object to add at the last index of the new array
3259     * @return A new array containing the existing elements plus the new element
3260     * @since 2.1
3261     */

3262    public static double[] add(double[] array, double element) {
3263        double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE);
3264        newArray[newArray.length - 1] = element;
3265        return newArray;
3266    }
3267    
3268    /**
3269     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3270     *
3271     * <p>The new array contains the same elements of the input
3272     * array plus the given element in the last position. The component type of
3273     * the new array is the same as that of the input array.</p>
3274     *
3275     * <p>If the input array is <code>null</code>, a new one element array is returned
3276     * whose component type is the same as the element.</p>
3277     *
3278     * <pre>
3279     * ArrayUtils.add(null, 0) = [0]
3280     * ArrayUtils.add([1], 0) = [1, 0]
3281     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3282     * </pre>
3283     *
3284     * @param array the array to copy and add the element to, may be <code>null</code>
3285     * @param element the object to add at the last index of the new array
3286     * @return A new array containing the existing elements plus the new element
3287     * @since 2.1
3288     */

3289    public static float[] add(float[] array, float element) {
3290        float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE);
3291        newArray[newArray.length - 1] = element;
3292        return newArray;
3293    }
3294    
3295    /**
3296     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3297     *
3298     * <p>The new array contains the same elements of the input
3299     * array plus the given element in the last position. The component type of
3300     * the new array is the same as that of the input array.</p>
3301     *
3302     * <p>If the input array is <code>null</code>, a new one element array is returned
3303     * whose component type is the same as the element.</p>
3304     *
3305     * <pre>
3306     * ArrayUtils.add(null, 0) = [0]
3307     * ArrayUtils.add([1], 0) = [1, 0]
3308     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3309     * </pre>
3310     *
3311     * @param array the array to copy and add the element to, may be <code>null</code>
3312     * @param element the object to add at the last index of the new array
3313     * @return A new array containing the existing elements plus the new element
3314     * @since 2.1
3315     */

3316    public static int[] add(int[] array, int element) {
3317        int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE);
3318        newArray[newArray.length - 1] = element;
3319        return newArray;
3320    }
3321    
3322    /**
3323     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3324     *
3325     * <p>The new array contains the same elements of the input
3326     * array plus the given element in the last position. The component type of
3327     * the new array is the same as that of the input array.</p>
3328     *
3329     * <p>If the input array is <code>null</code>, a new one element array is returned
3330     * whose component type is the same as the element.</p>
3331     *
3332     * <pre>
3333     * ArrayUtils.add(null, 0) = [0]
3334     * ArrayUtils.add([1], 0) = [1, 0]
3335     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3336     * </pre>
3337     *
3338     * @param array the array to copy and add the element to, may be <code>null</code>
3339     * @param element the object to add at the last index of the new array
3340     * @return A new array containing the existing elements plus the new element
3341     * @since 2.1
3342     */

3343    public static long[] add(long[] array, long element) {
3344        long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);
3345        newArray[newArray.length - 1] = element;
3346        return newArray;
3347    }
3348    
3349    /**
3350     * <p>Copies the given array and adds the given element at the end of the new array.</p>
3351     *
3352     * <p>The new array contains the same elements of the input
3353     * array plus the given element in the last position. The component type of
3354     * the new array is the same as that of the input array.</p>
3355     *
3356     * <p>If the input array is <code>null</code>, a new one element array is returned
3357     * whose component type is the same as the element.</p>
3358     *
3359     * <pre>
3360     * ArrayUtils.add(null, 0) = [0]
3361     * ArrayUtils.add([1], 0) = [1, 0]
3362     * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
3363     * </pre>
3364     *
3365     * @param array the array to copy and add the element to, may be <code>null</code>
3366     * @param element the object to add at the last index of the new array
3367     * @return A new array containing the existing elements plus the new element
3368     * @since 2.1
3369     */

3370    public static short[] add(short[] array, short element) {
3371        short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE);
3372        newArray[newArray.length - 1] = element;
3373        return newArray;
3374    }
3375    
3376    /**
3377     * Returns a copy of the given array of size 1 greater than the argument.
3378     * The last value of the array is left to the default value.
3379     *
3380     * @param array The array to copy, must not be <code>null</code>.
3381     * @param newArrayComponentType If <code>array</code> is <code>null</code>, create a
3382     * size 1 array of this type.
3383     * @return A new copy of the array of size 1 greater than the input.
3384     */

3385    private static Object JavaDoc copyArrayGrow1(Object JavaDoc array, Class JavaDoc newArrayComponentType) {
3386        if (array != null) {
3387            int arrayLength = Array.getLength(array);
3388            Object JavaDoc newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
3389            System.arraycopy(array, 0, newArray, 0, arrayLength);
3390            return newArray;
3391        } else {
3392            return Array.newInstance(newArrayComponentType, 1);
3393        }
3394    }
3395    
3396    /**
3397     * <p>Inserts the specified element at the specified position in the array.
3398     * Shifts the element currently at that position (if any) and any subsequent
3399     * elements to the right (adds one to their indices).</p>
3400     *
3401     * <p>This method returns a new array with the same elements of the input
3402     * array plus the given element on the specified position. The component
3403     * type of the returned array is always the same as that of the input
3404     * array.</p>
3405     *
3406     * <p>If the input array is <code>null</code>, a new one element array is returned
3407     * whose component type is the same as the element.</p>
3408     *
3409     * <pre>
3410     * ArrayUtils.add(null, 0, null) = [null]
3411     * ArrayUtils.add(null, 0, "a") = ["a"]
3412     * ArrayUtils.add(["a"], 1, null) = ["a", null]
3413     * ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
3414     * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
3415     * </pre>
3416     *
3417     * @param array the array to add the element to, may be <code>null</code>
3418     * @param index the position of the new object
3419     * @param element the object to add
3420     * @return A new array containing the existing elements and the new element
3421     * @throws IndexOutOfBoundsException if the index is out of range
3422     * (index < 0 || index > array.length).
3423     */

3424    public static Object JavaDoc[] add(Object JavaDoc[] array, int index, Object JavaDoc element) {
3425        Class JavaDoc clss = null;
3426        if(array != null) {
3427            clss = array.getClass().getComponentType();
3428        } else
3429        if(element != null) {
3430            clss = element.getClass();
3431        } else {
3432            return new Object JavaDoc[] { null };
3433        }
3434        return (Object JavaDoc[]) add( array, index, element, clss );
3435    }
3436    
3437    /**
3438     * <p>Inserts the specified element at the specified position in the array.
3439     * Shifts the element currently at that position (if any) and any subsequent
3440     * elements to the right (adds one to their indices).</p>
3441     *
3442     * <p>This method returns a new array with the same elements of the input
3443     * array plus the given element on the specified position. The component
3444     * type of the returned array is always the same as that of the input
3445     * array.</p>
3446     *
3447     * <p>If the input array is <code>null</code>, a new one element array is returned
3448     * whose component type is the same as the element.</p>
3449     *
3450     * <pre>
3451     * ArrayUtils.add(null, 0, true) = [true]
3452     * ArrayUtils.add([true], 0, false) = [false, true]
3453     * ArrayUtils.add([false], 1, true) = [false, true]
3454     * ArrayUtils.add([true, false], 1, true) = [true, true, false]
3455     * </pre>
3456     *
3457     * @param array the array to add the element to, may be <code>null</code>
3458     * @param index the position of the new object
3459     * @param element the object to add
3460     * @return A new array containing the existing elements and the new element
3461     * @throws IndexOutOfBoundsException if the index is out of range
3462     * (index < 0 || index > array.length).
3463     */

3464    public static boolean[] add(boolean[] array, int index, boolean element) {
3465        return (boolean[]) add( array, index, new Boolean JavaDoc(element), Boolean.TYPE );
3466    }
3467    
3468    /**
3469     * <p>Inserts the specified element at the specified position in the array.
3470     * Shifts the element currently at that position (if any) and any subsequent
3471     * elements to the right (adds one to their indices).</p>
3472     *
3473     * <p>This method returns a new array with the same elements of the input
3474     * array plus the given element on the specified position. The component
3475     * type of the returned array is always the same as that of the input
3476     * array.</p>
3477     *
3478     * <p>If the input array is <code>null</code>, a new one element array is returned
3479     * whose component type is the same as the element.</p>
3480     *
3481     * <pre>
3482     * ArrayUtils.add(null, 0, 'a') = ['a']
3483     * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a']
3484     * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b']
3485     * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b']
3486     * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c']
3487     * </pre>
3488     *
3489     * @param array the array to add the element to, may be <code>null</code>
3490     * @param index the position of the new object
3491     * @param element the object to add
3492     * @return A new array containing the existing elements and the new element
3493     * @throws IndexOutOfBoundsException if the index is out of range
3494     * (index < 0 || index > array.length).
3495     */

3496    public static char[] add(char[] array, int index, char element) {
3497        return (char[]) add( array, index, new Character JavaDoc(element), Character.TYPE );
3498    }
3499    
3500    /**
3501     * <p>Inserts the specified element at the specified position in the array.
3502     * Shifts the element currently at that position (if any) and any subsequent
3503     * elements to the right (adds one to their indices).</p>
3504     *
3505     * <p>This method returns a new array with the same elements of the input
3506     * array plus the given element on the specified position. The component
3507     * type of the returned array is always the same as that of the input
3508     * array.</p>
3509     *
3510     * <p>If the input array is <code>null</code>, a new one element array is returned
3511     * whose component type is the same as the element.</p>
3512     *
3513     * <pre>
3514     * ArrayUtils.add([1], 0, 2) = [2, 1]
3515     * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3]
3516     * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6]
3517     * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
3518     * </pre>
3519     *
3520     * @param array the array to add the element to, may be <code>null</code>
3521     * @param index the position of the new object
3522     * @param element the object to add
3523     * @return A new array containing the existing elements and the new element
3524     * @throws IndexOutOfBoundsException if the index is out of range
3525     * (index < 0 || index > array.length).
3526     */

3527    public static byte[] add(byte[] array, int index, byte element) {
3528        return (byte[]) add( array, index, new Byte JavaDoc(element), Byte.TYPE );
3529    }
3530    
3531    /**
3532     * <p>Inserts the specified element at the specified position in the array.
3533     * Shifts the element currently at that position (if any) and any subsequent
3534     * elements to the right (adds one to their indices).</p>
3535     *
3536     * <p>This method returns a new array with the same elements of the input
3537     * array plus the given element on the specified position. The component
3538     * type of the returned array is always the same as that of the input
3539     * array.</p>
3540     *
3541     * <p>If the input array is <code>null</code>, a new one element array is returned
3542     * whose component type is the same as the element.</p>
3543     *
3544     * <pre>
3545     * ArrayUtils.add([1], 0, 2) = [2, 1]
3546     * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
3547     * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
3548     * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
3549     * </pre>
3550     *
3551     * @param array the array to add the element to, may be <code>null</code>
3552     * @param index the position of the new object
3553     * @param element the object to add
3554     * @return A new array containing the existing elements and the new element
3555     * @throws IndexOutOfBoundsException if the index is out of range
3556     * (index < 0 || index > array.length).
3557     */

3558    public static short[] add(short[] array, int index, short element) {
3559        return (short[]) add( array, index, new Short JavaDoc(element), Short.TYPE );
3560    }
3561    
3562    /**
3563     * <p>Inserts the specified element at the specified position in the array.
3564     * Shifts the element currently at that position (if any) and any subsequent
3565     * elements to the right (adds one to their indices).</p>
3566     *
3567     * <p>This method returns a new array with the same elements of the input
3568     * array plus the given element on the specified position. The component
3569     * type of the returned array is always the same as that of the input
3570     * array.</p>
3571     *
3572     * <p>If the input array is <code>null</code>, a new one element array is returned
3573     * whose component type is the same as the element.</p>
3574     *
3575     * <pre>
3576     * ArrayUtils.add([1], 0, 2) = [2, 1]
3577     * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
3578     * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
3579     * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
3580     * </pre>
3581     *
3582     * @param array the array to add the element to, may be <code>null</code>
3583     * @param index the position of the new object
3584     * @param element the object to add
3585     * @return A new array containing the existing elements and the new element
3586     * @throws IndexOutOfBoundsException if the index is out of range
3587     * (index < 0 || index > array.length).
3588     */

3589    public static int[] add(int[] array, int index, int element) {
3590        return (int[]) add( array, index, new Integer JavaDoc(element), Integer.TYPE );
3591    }
3592    
3593    /**
3594     * <p>Inserts the specified element at the specified position in the array.
3595     * Shifts the element currently at that position (if any) and any subsequent
3596     * elements to the right (adds one to their indices).</p>
3597     *
3598     * <p>This method returns a new array with the same elements of the input
3599     * array plus the given element on the specified position. The component
3600     * type of the returned array is always the same as that of the input
3601     * array.</p>
3602     *
3603     * <p>If the input array is <code>null</code>, a new one element array is returned
3604     * whose component type is the same as the element.</p>
3605     *
3606     * <pre>
3607     * ArrayUtils.add([1L], 0, 2L) = [2L, 1L]
3608     * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L]
3609     * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L]
3610     * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L]
3611     * </pre>
3612     *
3613     * @param array the array to add the element to, may be <code>null</code>
3614     * @param index the position of the new object
3615     * @param element the object to add
3616     * @return A new array containing the existing elements and the new element
3617     * @throws IndexOutOfBoundsException if the index is out of range
3618     * (index < 0 || index > array.length).
3619     */

3620    public static long[] add(long[] array, int index, long element) {
3621        return (long[]) add( array, index, new Long JavaDoc(element), Long.TYPE );
3622    }
3623    
3624    /**
3625     * <p>Inserts the specified element at the specified position in the array.
3626     * Shifts the element currently at that position (if any) and any subsequent
3627     * elements to the right (adds one to their indices).</p>
3628     *
3629     * <p>This method returns a new array with the same elements of the input
3630     * array plus the given element on the specified position. The component
3631     * type of the returned array is always the same as that of the input
3632     * array.</p>
3633     *
3634     * <p>If the input array is <code>null</code>, a new one element array is returned
3635     * whose component type is the same as the element.</p>
3636     *
3637     * <pre>
3638     * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f]
3639     * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f]
3640     * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f]
3641     * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f, 0.3f]
3642     * </pre>
3643     *
3644     * @param array the array to add the element to, may be <code>null</code>
3645     * @param index the position of the new object
3646     * @param element the object to add
3647     * @return A new array containing the existing elements and the new element
3648     * @throws IndexOutOfBoundsException if the index is out of range
3649     * (index < 0 || index > array.length).
3650     */

3651    public static float[] add(float[] array, int index, float element) {
3652        return (float[]) add( array, index, new Float JavaDoc(element), Float.TYPE );
3653    }
3654    
3655    /**
3656     * <p>Inserts the specified element at the specified position in the array.
3657     * Shifts the element currently at that position (if any) and any subsequent
3658     * elements to the right (adds one to their indices).</p>
3659     *
3660     * <p>This method returns a new array with the same elements of the input
3661     * array plus the given element on the specified position. The component
3662     * type of the returned array is always the same as that of the input
3663     * array.</p>
3664     *
3665     * <p>If the input array is <code>null</code>, a new one element array is returned
3666     * whose component type is the same as the element.</p>
3667     *
3668     * <pre>
3669     * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1]
3670     * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5]
3671     * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7]
3672     * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3]
3673     * </pre>
3674     *
3675     * @param array the array to add the element to, may be <code>null</code>
3676     * @param index the position of the new object
3677     * @param element the object to add
3678     * @return A new array containing the existing elements and the new element
3679     * @throws IndexOutOfBoundsException if the index is out of range
3680     * (index < 0 || index > array.length).
3681     */

3682    public static double[] add(double[] array, int index, double element) {
3683        return (double[]) add( array, index, new Double JavaDoc(element), Double.TYPE );
3684    }
3685    
3686    /**
3687     * Underlying implementation of add(array, index, element) methods.
3688     * The last parameter is the class, which may not equal element.getClass
3689     * for primitives.
3690     *
3691     * @param array the array to add the element to, may be <code>null</code>
3692     * @param index the position of the new object
3693     * @param element the object to add
3694     * @param clss the type of the element being added
3695     * @return A new array containing the existing elements and the new element
3696     */

3697    private static Object JavaDoc add(Object JavaDoc array, int index, Object JavaDoc element, Class JavaDoc clss) {
3698        if (array == null) {
3699            if (index != 0) {
3700                throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Length: 0");
3701            }
3702            Object JavaDoc joinedArray = Array.newInstance(clss, 1);
3703            Array.set(joinedArray, 0, element);
3704            return joinedArray;
3705        }
3706        int length = Array.getLength(array);
3707        if (index > length || index < 0) {
3708            throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Length: " + length);
3709        }
3710        Object JavaDoc result = Array.newInstance(clss, length + 1);
3711        System.arraycopy(array, 0, result, 0, index);
3712        Array.set(result, index, element);
3713        if (index < length) {
3714            System.arraycopy(array, index, result, index + 1, length - index);
3715        }
3716        return result;
3717    }
3718    
3719    /**
3720     * <p>Removes the element at the specified position from the specified array.
3721     * All subsequent elements are shifted to the left (substracts one from
3722     * their indices).</p>
3723     *
3724     * <p>This method returns a new array with the same elements of the input
3725     * array except the element on the specified position. The component
3726     * type of the returned array is always the same as that of the input
3727     * array.</p>
3728     *
3729     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
3730     * will be thrown, because in that case no valid index can be specified.</p>
3731     *
3732     * <pre>
3733     * ArrayUtils.remove(["a"], 0) = []
3734     * ArrayUtils.remove(["a", "b"], 0) = ["b"]
3735     * ArrayUtils.remove(["a", "b"], 1) = ["a"]
3736     * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
3737     * </pre>
3738     *
3739     * @param array the array to remove the element from, may not be <code>null</code>
3740     * @param index the position of the element to be removed
3741     * @return A new array containing the existing elements except the element
3742     * at the specified position.
3743     * @throws IndexOutOfBoundsException if the index is out of range
3744     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
3745     * @since 2.1
3746     */

3747    public static Object JavaDoc[] remove(Object JavaDoc[] array, int index) {
3748        return (Object JavaDoc[]) remove((Object JavaDoc) array, index);
3749    }
3750    
3751    /**
3752     * <p>Removes the first occurrence of the specified element from the
3753     * specified array. All subsequent elements are shifted to the left
3754     * (substracts one from their indices). If the array doesn't contains
3755     * such an element, no elements are removed from the array.</p>
3756     *
3757     * <p>This method returns a new array with the same elements of the input
3758     * array except the first occurrence of the specified element. The component
3759     * type of the returned array is always the same as that of the input
3760     * array.</p>
3761     *
3762     * <pre>
3763     * ArrayUtils.removeElement(null, "a") = null
3764     * ArrayUtils.removeElement([], "a") = []
3765     * ArrayUtils.removeElement(["a"], "b") = ["a"]
3766     * ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
3767     * ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
3768     * </pre>
3769     *
3770     * @param array the array to remove the element from, may be <code>null</code>
3771     * @param element the element to be removed
3772     * @return A new array containing the existing elements except the first
3773     * occurrence of the specified element.
3774     * @since 2.1
3775     */

3776    public static Object JavaDoc[] removeElement(Object JavaDoc[] array, Object JavaDoc element) {
3777        int index = indexOf(array, element);
3778        if (index == -1) {
3779            return clone(array);
3780        }
3781        return remove(array, index);
3782    }
3783    
3784    /**
3785     * <p>Removes the element at the specified position from the specified array.
3786     * All subsequent elements are shifted to the left (substracts one from
3787     * their indices).</p>
3788     *
3789     * <p>This method returns a new array with the same elements of the input
3790     * array except the element on the specified position. The component
3791     * type of the returned array is always the same as that of the input
3792     * array.</p>
3793     *
3794     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
3795     * will be thrown, because in that case no valid index can be specified.</p>
3796     *
3797     * <pre>
3798     * ArrayUtils.remove([true], 0) = []
3799     * ArrayUtils.remove([true, false], 0) = [false]
3800     * ArrayUtils.remove([true, false], 1) = [true]
3801     * ArrayUtils.remove([true, true, false], 1) = [true, false]
3802     * </pre>
3803     *
3804     * @param array the array to remove the element from, may not be <code>null</code>
3805     * @param index the position of the element to be removed
3806     * @return A new array containing the existing elements except the element
3807     * at the specified position.
3808     * @throws IndexOutOfBoundsException if the index is out of range
3809     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
3810     * @since 2.1
3811     */

3812    public static boolean[] remove(boolean[] array, int index) {
3813        return (boolean[]) remove((Object JavaDoc) array, index);
3814    }
3815    
3816    /**
3817     * <p>Removes the first occurrence of the specified element from the
3818     * specified array. All subsequent elements are shifted to the left
3819     * (substracts one from their indices). If the array doesn't contains
3820     * such an element, no elements are removed from the array.</p>
3821     *
3822     * <p>This method returns a new array with the same elements of the input
3823     * array except the first occurrence of the specified element. The component
3824     * type of the returned array is always the same as that of the input
3825     * array.</p>
3826     *
3827     * <pre>
3828     * ArrayUtils.removeElement(null, true) = null
3829     * ArrayUtils.removeElement([], true) = []
3830     * ArrayUtils.removeElement([true], false) = [true]
3831     * ArrayUtils.removeElement([true, false], false) = [true]
3832     * ArrayUtils.removeElement([true, false, true], true) = [false, true]
3833     * </pre>
3834     *
3835     * @param array the array to remove the element from, may be <code>null</code>
3836     * @param element the element to be removed
3837     * @return A new array containing the existing elements except the first
3838     * occurrence of the specified element.
3839     * @since 2.1
3840     */

3841    public static boolean[] removeElement(boolean[] array, boolean element) {
3842        int index = indexOf(array, element);
3843        if (index == -1) {
3844            return clone(array);
3845        }
3846        return remove(array, index);
3847    }
3848    
3849    /**
3850     * <p>Removes the element at the specified position from the specified array.
3851     * All subsequent elements are shifted to the left (substracts one from
3852     * their indices).</p>
3853     *
3854     * <p>This method returns a new array with the same elements of the input
3855     * array except the element on the specified position. The component
3856     * type of the returned array is always the same as that of the input
3857     * array.</p>
3858     *
3859     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
3860     * will be thrown, because in that case no valid index can be specified.</p>
3861     *
3862     * <pre>
3863     * ArrayUtils.remove([1], 0) = []
3864     * ArrayUtils.remove([1, 0], 0) = [0]
3865     * ArrayUtils.remove([1, 0], 1) = [1]
3866     * ArrayUtils.remove([1, 0, 1], 1) = [1, 1]
3867     * </pre>
3868     *
3869     * @param array the array to remove the element from, may not be <code>null</code>
3870     * @param index the position of the element to be removed
3871     * @return A new array containing the existing elements except the element
3872     * at the specified position.
3873     * @throws IndexOutOfBoundsException if the index is out of range
3874     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
3875     * @since 2.1
3876     */

3877    public static byte[] remove(byte[] array, int index) {
3878        return (byte[]) remove((Object JavaDoc) array, index);
3879    }
3880    
3881    /**
3882     * <p>Removes the first occurrence of the specified element from the
3883     * specified array. All subsequent elements are shifted to the left
3884     * (substracts one from their indices). If the array doesn't contains
3885     * such an element, no elements are removed from the array.</p>
3886     *
3887     * <p>This method returns a new array with the same elements of the input
3888     * array except the first occurrence of the specified element. The component
3889     * type of the returned array is always the same as that of the input
3890     * array.</p>
3891     *
3892     * <pre>
3893     * ArrayUtils.removeElement(null, 1) = null
3894     * ArrayUtils.removeElement([], 1) = []
3895     * ArrayUtils.removeElement([1], 0) = [1]
3896     * ArrayUtils.removeElement([1, 0], 0) = [1]
3897     * ArrayUtils.removeElement([1, 0, 1], 1) = [0, 1]
3898     * </pre>
3899     *
3900     * @param array the array to remove the element from, may be <code>null</code>
3901     * @param element the element to be removed
3902     * @return A new array containing the existing elements except the first
3903     * occurrence of the specified element.
3904     * @since 2.1
3905     */

3906    public static byte[] removeElement(byte[] array, byte element) {
3907        int index = indexOf(array, element);
3908        if (index == -1) {
3909            return clone(array);
3910        }
3911        return remove(array, index);
3912    }
3913    
3914    /**
3915     * <p>Removes the element at the specified position from the specified array.
3916     * All subsequent elements are shifted to the left (substracts one from
3917     * their indices).</p>
3918     *
3919     * <p>This method returns a new array with the same elements of the input
3920     * array except the element on the specified position. The component
3921     * type of the returned array is always the same as that of the input
3922     * array.</p>
3923     *
3924     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
3925     * will be thrown, because in that case no valid index can be specified.</p>
3926     *
3927     * <pre>
3928     * ArrayUtils.remove(['a'], 0) = []
3929     * ArrayUtils.remove(['a', 'b'], 0) = ['b']
3930     * ArrayUtils.remove(['a', 'b'], 1) = ['a']
3931     * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c']
3932     * </pre>
3933     *
3934     * @param array the array to remove the element from, may not be <code>null</code>
3935     * @param index the position of the element to be removed
3936     * @return A new array containing the existing elements except the element
3937     * at the specified position.
3938     * @throws IndexOutOfBoundsException if the index is out of range
3939     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
3940     * @since 2.1
3941     */

3942    public static char[] remove(char[] array, int index) {
3943        return (char[]) remove((Object JavaDoc) array, index);
3944    }
3945    
3946    /**
3947     * <p>Removes the first occurrence of the specified element from the
3948     * specified array. All subsequent elements are shifted to the left
3949     * (substracts one from their indices). If the array doesn't contains
3950     * such an element, no elements are removed from the array.</p>
3951     *
3952     * <p>This method returns a new array with the same elements of the input
3953     * array except the first occurrence of the specified element. The component
3954     * type of the returned array is always the same as that of the input
3955     * array.</p>
3956     *
3957     * <pre>
3958     * ArrayUtils.removeElement(null, 'a') = null
3959     * ArrayUtils.removeElement([], 'a') = []
3960     * ArrayUtils.removeElement(['a'], 'b') = ['a']
3961     * ArrayUtils.removeElement(['a', 'b'], 'a') = ['b']
3962     * ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
3963     * </pre>
3964     *
3965     * @param array the array to remove the element from, may be <code>null</code>
3966     * @param element the element to be removed
3967     * @return A new array containing the existing elements except the first
3968     * occurrence of the specified element.
3969     * @since 2.1
3970     */

3971    public static char[] removeElement(char[] array, char element) {
3972        int index = indexOf(array, element);
3973        if (index == -1) {
3974            return clone(array);
3975        }
3976        return remove(array, index);
3977    }
3978    
3979    /**
3980     * <p>Removes the element at the specified position from the specified array.
3981     * All subsequent elements are shifted to the left (substracts one from
3982     * their indices).</p>
3983     *
3984     * <p>This method returns a new array with the same elements of the input
3985     * array except the element on the specified position. The component
3986     * type of the returned array is always the same as that of the input
3987     * array.</p>
3988     *
3989     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
3990     * will be thrown, because in that case no valid index can be specified.</p>
3991     *
3992     * <pre>
3993     * ArrayUtils.remove([1.1], 0) = []
3994     * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
3995     * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
3996     * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
3997     * </pre>
3998     *
3999     * @param array the array to remove the element from, may not be <code>null</code>
4000     * @param index the position of the element to be removed
4001     * @return A new array containing the existing elements except the element
4002     * at the specified position.
4003     * @throws IndexOutOfBoundsException if the index is out of range
4004     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4005     * @since 2.1
4006     */

4007    public static double[] remove(double[] array, int index) {
4008        return (double[]) remove((Object JavaDoc) array, index);
4009    }
4010    
4011    /**
4012     * <p>Removes the first occurrence of the specified element from the
4013     * specified array. All subsequent elements are shifted to the left
4014     * (substracts one from their indices). If the array doesn't contains
4015     * such an element, no elements are removed from the array.</p>
4016     *
4017     * <p>This method returns a new array with the same elements of the input
4018     * array except the first occurrence of the specified element. The component
4019     * type of the returned array is always the same as that of the input
4020     * array.</p>
4021     *
4022     * <pre>
4023     * ArrayUtils.removeElement(null, 1.1) = null
4024     * ArrayUtils.removeElement([], 1.1) = []
4025     * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
4026     * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
4027     * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
4028     * </pre>
4029     *
4030     * @param array the array to remove the element from, may be <code>null</code>
4031     * @param element the element to be removed
4032     * @return A new array containing the existing elements except the first
4033     * occurrence of the specified element.
4034     * @since 2.1
4035     */

4036    public static double[] removeElement(double[] array, double element) {
4037        int index = indexOf(array, element);
4038        if (index == -1) {
4039            return clone(array);
4040        }
4041        return remove(array, index);
4042    }
4043    
4044    /**
4045     * <p>Removes the element at the specified position from the specified array.
4046     * All subsequent elements are shifted to the left (substracts one from
4047     * their indices).</p>
4048     *
4049     * <p>This method returns a new array with the same elements of the input
4050     * array except the element on the specified position. The component
4051     * type of the returned array is always the same as that of the input
4052     * array.</p>
4053     *
4054     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4055     * will be thrown, because in that case no valid index can be specified.</p>
4056     *
4057     * <pre>
4058     * ArrayUtils.remove([1.1], 0) = []
4059     * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
4060     * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
4061     * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
4062     * </pre>
4063     *
4064     * @param array the array to remove the element from, may not be <code>null</code>
4065     * @param index the position of the element to be removed
4066     * @return A new array containing the existing elements except the element
4067     * at the specified position.
4068     * @throws IndexOutOfBoundsException if the index is out of range
4069     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4070     * @since 2.1
4071     */

4072    public static float[] remove(float[] array, int index) {
4073        return (float[]) remove((Object JavaDoc) array, index);
4074    }
4075    
4076    /**
4077     * <p>Removes the first occurrence of the specified element from the
4078     * specified array. All subsequent elements are shifted to the left
4079     * (substracts one from their indices). If the array doesn't contains
4080     * such an element, no elements are removed from the array.</p>
4081     *
4082     * <p>This method returns a new array with the same elements of the input
4083     * array except the first occurrence of the specified element. The component
4084     * type of the returned array is always the same as that of the input
4085     * array.</p>
4086     *
4087     * <pre>
4088     * ArrayUtils.removeElement(null, 1.1) = null
4089     * ArrayUtils.removeElement([], 1.1) = []
4090     * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
4091     * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
4092     * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
4093     * </pre>
4094     *
4095     * @param array the array to remove the element from, may be <code>null</code>
4096     * @param element the element to be removed
4097     * @return A new array containing the existing elements except the first
4098     * occurrence of the specified element.
4099     * @since 2.1
4100     */

4101    public static float[] removeElement(float[] array, float element) {
4102        int index = indexOf(array, element);
4103        if (index == -1) {
4104            return clone(array);
4105        }
4106        return remove(array, index);
4107    }
4108    
4109    /**
4110     * <p>Removes the element at the specified position from the specified array.
4111     * All subsequent elements are shifted to the left (substracts one from
4112     * their indices).</p>
4113     *
4114     * <p>This method returns a new array with the same elements of the input
4115     * array except the element on the specified position. The component
4116     * type of the returned array is always the same as that of the input
4117     * array.</p>
4118     *
4119     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4120     * will be thrown, because in that case no valid index can be specified.</p>
4121     *
4122     * <pre>
4123     * ArrayUtils.remove([1], 0) = []
4124     * ArrayUtils.remove([2, 6], 0) = [6]
4125     * ArrayUtils.remove([2, 6], 1) = [2]
4126     * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
4127     * </pre>
4128     *
4129     * @param array the array to remove the element from, may not be <code>null</code>
4130     * @param index the position of the element to be removed
4131     * @return A new array containing the existing elements except the element
4132     * at the specified position.
4133     * @throws IndexOutOfBoundsException if the index is out of range
4134     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4135     * @since 2.1
4136     */

4137    public static int[] remove(int[] array, int index) {
4138        return (int[]) remove((Object JavaDoc) array, index);
4139    }
4140    
4141    /**
4142     * <p>Removes the first occurrence of the specified element from the
4143     * specified array. All subsequent elements are shifted to the left
4144     * (substracts one from their indices). If the array doesn't contains
4145     * such an element, no elements are removed from the array.</p>
4146     *
4147     * <p>This method returns a new array with the same elements of the input
4148     * array except the first occurrence of the specified element. The component
4149     * type of the returned array is always the same as that of the input
4150     * array.</p>
4151     *
4152     * <pre>
4153     * ArrayUtils.removeElement(null, 1) = null
4154     * ArrayUtils.removeElement([], 1) = []
4155     * ArrayUtils.removeElement([1], 2) = [1]
4156     * ArrayUtils.removeElement([1, 3], 1) = [3]
4157     * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
4158     * </pre>
4159     *
4160     * @param array the array to remove the element from, may be <code>null</code>
4161     * @param element the element to be removed
4162     * @return A new array containing the existing elements except the first
4163     * occurrence of the specified element.
4164     * @since 2.1
4165     */

4166    public static int[] removeElement(int[] array, int element) {
4167        int index = indexOf(array, element);
4168        if (index == -1) {
4169            return clone(array);
4170        }
4171        return remove(array, index);
4172    }
4173    
4174    /**
4175     * <p>Removes the element at the specified position from the specified array.
4176     * All subsequent elements are shifted to the left (substracts one from
4177     * their indices).</p>
4178     *
4179     * <p>This method returns a new array with the same elements of the input
4180     * array except the element on the specified position. The component
4181     * type of the returned array is always the same as that of the input
4182     * array.</p>
4183     *
4184     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4185     * will be thrown, because in that case no valid index can be specified.</p>
4186     *
4187     * <pre>
4188     * ArrayUtils.remove([1], 0) = []
4189     * ArrayUtils.remove([2, 6], 0) = [6]
4190     * ArrayUtils.remove([2, 6], 1) = [2]
4191     * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
4192     * </pre>
4193     *
4194     * @param array the array to remove the element from, may not be <code>null</code>
4195     * @param index the position of the element to be removed
4196     * @return A new array containing the existing elements except the element
4197     * at the specified position.
4198     * @throws IndexOutOfBoundsException if the index is out of range
4199     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4200     * @since 2.1
4201     */

4202    public static long[] remove(long[] array, int index) {
4203        return (long[]) remove((Object JavaDoc) array, index);
4204    }
4205    
4206    /**
4207     * <p>Removes the first occurrence of the specified element from the
4208     * specified array. All subsequent elements are shifted to the left
4209     * (substracts one from their indices). If the array doesn't contains
4210     * such an element, no elements are removed from the array.</p>
4211     *
4212     * <p>This method returns a new array with the same elements of the input
4213     * array except the first occurrence of the specified element. The component
4214     * type of the returned array is always the same as that of the input
4215     * array.</p>
4216     *
4217     * <pre>
4218     * ArrayUtils.removeElement(null, 1) = null
4219     * ArrayUtils.removeElement([], 1) = []
4220     * ArrayUtils.removeElement([1], 2) = [1]
4221     * ArrayUtils.removeElement([1, 3], 1) = [3]
4222     * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
4223     * </pre>
4224     *
4225     * @param array the array to remove the element from, may be <code>null</code>
4226     * @param element the element to be removed
4227     * @return A new array containing the existing elements except the first
4228     * occurrence of the specified element.
4229     * @since 2.1
4230     */

4231    public static long[] removeElement(long[] array, long element) {
4232        int index = indexOf(array, element);
4233        if (index == -1) {
4234            return clone(array);
4235        }
4236        return remove(array, index);
4237    }
4238    
4239    /**
4240     * <p>Removes the element at the specified position from the specified array.
4241     * All subsequent elements are shifted to the left (substracts one from
4242     * their indices).</p>
4243     *
4244     * <p>This method returns a new array with the same elements of the input
4245     * array except the element on the specified position. The component
4246     * type of the returned array is always the same as that of the input
4247     * array.</p>
4248     *
4249     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4250     * will be thrown, because in that case no valid index can be specified.</p>
4251     *
4252     * <pre>
4253     * ArrayUtils.remove([1], 0) = []
4254     * ArrayUtils.remove([2, 6], 0) = [6]
4255     * ArrayUtils.remove([2, 6], 1) = [2]
4256     * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
4257     * </pre>
4258     *
4259     * @param array the array to remove the element from, may not be <code>null</code>
4260     * @param index the position of the element to be removed
4261     * @return A new array containing the existing elements except the element
4262     * at the specified position.
4263     * @throws IndexOutOfBoundsException if the index is out of range
4264     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4265     * @since 2.1
4266     */

4267    public static short[] remove(short[] array, int index) {
4268        return (short[]) remove((Object JavaDoc) array, index);
4269    }
4270    
4271    /**
4272     * <p>Removes the first occurrence of the specified element from the
4273     * specified array. All subsequent elements are shifted to the left
4274     * (substracts one from their indices). If the array doesn't contains
4275     * such an element, no elements are removed from the array.</p>
4276     *
4277     * <p>This method returns a new array with the same elements of the input
4278     * array except the first occurrence of the specified element. The component
4279     * type of the returned array is always the same as that of the input
4280     * array.</p>
4281     *
4282     * <pre>
4283     * ArrayUtils.removeElement(null, 1) = null
4284     * ArrayUtils.removeElement([], 1) = []
4285     * ArrayUtils.removeElement([1], 2) = [1]
4286     * ArrayUtils.removeElement([1, 3], 1) = [3]
4287     * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
4288     * </pre>
4289     *
4290     * @param array the array to remove the element from, may be <code>null</code>
4291     * @param element the element to be removed
4292     * @return A new array containing the existing elements except the first
4293     * occurrence of the specified element.
4294     * @since 2.1
4295     */

4296    public static short[] removeElement(short[] array, short element) {
4297        int index = indexOf(array, element);
4298        if (index == -1) {
4299            return clone(array);
4300        }
4301        return remove(array, index);
4302    }
4303    
4304    /**
4305     * <p>Removes the element at the specified position from the specified array.
4306     * All subsequent elements are shifted to the left (substracts one from
4307     * their indices).</p>
4308     *
4309     * <p>This method returns a new array with the same elements of the input
4310     * array except the element on the specified position. The component
4311     * type of the returned array is always the same as that of the input
4312     * array.</p>
4313     *
4314     * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
4315     * will be thrown, because in that case no valid index can be specified.</p>
4316     *
4317     * @param array the array to remove the element from, may not be <code>null</code>
4318     * @param index the position of the element to be removed
4319     * @return A new array containing the existing elements except the element
4320     * at the specified position.
4321     * @throws IndexOutOfBoundsException if the index is out of range
4322     * (index < 0 || index >= array.length), or if the array is <code>null</code>.
4323     * @since 2.1
4324     */

4325    private static Object JavaDoc remove(Object JavaDoc array, int index) {
4326        int length = getLength(array);
4327        if (index < 0 || index >= length) {
4328            throw new IndexOutOfBoundsException JavaDoc("Index: " + index + ", Length: " + length);
4329        }
4330        
4331        Object JavaDoc result = Array.newInstance(array.getClass().getComponentType(), length - 1);
4332        System.arraycopy(array, 0, result, 0, index);
4333        if (index < length - 1) {
4334            System.arraycopy(array, index + 1, result, index, length - index - 1);
4335        }
4336        
4337        return result;
4338    }
4339    
4340}
4341
Popular Tags