-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathArrayUtils.java
More file actions
2000 lines (1772 loc) · 65.1 KB
/
ArrayUtils.java
File metadata and controls
2000 lines (1772 loc) · 65.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package com.xzy.utils.array;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
import java.util.List;
/**
* 数组相关工具类。
* 参考 https://github.com/Blankj/AndroidUtilCode/blob/master/lib/utilcode/src/main/
* java/com/blankj/utilcode/util/ArrayUtils.java
*
* @author xzy
*/
@SuppressWarnings("unused")
public class ArrayUtils {
public static final int INDEX_NOT_FOUND = -1;
private ArrayUtils() {
throw new UnsupportedOperationException("u can't instantiate me...");
}
/**
* Returns a new array only of those given elements.
*
* @param array The array.
* @return a new array only of those given elements.
*/
@SafeVarargs
public static <T> T[] newArray(T... array) {
return array;
}
public static long[] newLongArray(long... array) {
return array;
}
public static int[] newIntArray(int... array) {
return array;
}
public static short[] newShortArray(short... array) {
return array;
}
public static char[] newCharArray(char... array) {
return array;
}
public static byte[] newByteArray(byte... array) {
return array;
}
public static double[] newDoubleArray(double... array) {
return array;
}
public static float[] newFloatArray(float... array) {
return array;
}
public static boolean[] newBooleanArray(boolean... array) {
return array;
}
/**
* Return the array is empty.
*
* @param array The array.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean isEmpty(Object array) {
return getLength(array) == 0;
}
/**
* Return the size of array.
*
* @param array The array.
* @return the size of array
*/
public static int getLength(Object array) {
if (array == null) return 0;
return Array.getLength(array);
}
public static boolean isSameLength(Object array1, Object array2) {
return getLength(array1) == getLength(array2);
}
/**
* Get the value of the specified index of the array.
*
* @param array The array.
* @param index The index into the array.
* @return the value of the specified index of the array
*/
public static Object get(Object array, int index) {
return get(array, index, null);
}
/**
* Get the value of the specified index of the array.
*
* @param array The array.
* @param index The index into the array.
* @param defaultValue The default value.
* @return the value of the specified index of the array
*/
public static Object get(Object array, int index, Object defaultValue) {
if (array == null) return defaultValue;
try {
return Array.get(array, index);
} catch (Exception ignore) {
return defaultValue;
}
}
/**
* Set the value of the specified index of the array.
*
* @param array The array.
* @param index The index into the array.
* @param value The new value of the indexed component.
*/
public static void set(Object array, int index, Object value) {
if (array == null) return;
Array.set(array, index, value);
}
/**
* Return whether the two arrays are equals.
*
* @param a One array.
* @param a2 The other array.
* @return {@code true}: yes<br>{@code false}: no
*/
public static boolean equals(Object[] a, Object[] a2) {
return Arrays.deepEquals(a, a2);
}
public static boolean equals(boolean[] a, boolean[] a2) {
return Arrays.equals(a, a2);
}
public static boolean equals(byte[] a, byte[] a2) {
return Arrays.equals(a, a2);
}
public static boolean equals(char[] a, char[] a2) {
return Arrays.equals(a, a2);
}
public static boolean equals(double[] a, double[] a2) {
return Arrays.equals(a, a2);
}
public static boolean equals(float[] a, float[] a2) {
return Arrays.equals(a, a2);
}
public static boolean equals(int[] a, int[] a2) {
return Arrays.equals(a, a2);
}
public static boolean equals(short[] a, short[] a2) {
return Arrays.equals(a, a2);
}
///////////////////////////////////////////////////////////////////////////
// reverse
///////////////////////////////////////////////////////////////////////////
/**
* <p>Reverses the order of the given array.</p>
*
* <p>There is no special handling for multi-dimensional arrays.</p>
*
* <p>This method does nothing for a <code>null</code> input array.</p>
*
* @param array the array to reverse, may be <code>null</code>
*/
public static <T> void reverse(T[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
T tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(long[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
long tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(int[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
int tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(short[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
short tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(char[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
char tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(byte[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
byte tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(double[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
double tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(float[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
float tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
public static void reverse(boolean[] array) {
if (array == null) {
return;
}
int i = 0;
int j = array.length - 1;
boolean tmp;
while (j > i) {
tmp = array[j];
array[j] = array[i];
array[i] = tmp;
j--;
i++;
}
}
///////////////////////////////////////////////////////////////////////////
// copy
///////////////////////////////////////////////////////////////////////////
/**
* <p>Copies the specified array and handling
* <code>null</code>.</p>
*
* <p>The objects in the array are not cloned, thus there is no special
* handling for multi-dimensional arrays.</p>
*
* <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
*
* @param array the array to shallow clone, may be <code>null</code>
* @return the cloned array, <code>null</code> if <code>null</code> input
*/
public static <T> T[] copy(T[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static long[] copy(long[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static int[] copy(int[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static short[] copy(short[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static char[] copy(char[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static byte[] copy(byte[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static double[] copy(double[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static float[] copy(float[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
public static boolean[] copy(boolean[] array) {
if (array == null) return null;
return subArray(array, 0, array.length);
}
private static Object realCopy(Object array) {
if (array == null) return null;
return realSubArray(array, 0, getLength(array));
}
///////////////////////////////////////////////////////////////////////////
// subArray
///////////////////////////////////////////////////////////////////////////
public static <T> T[] subArray(T[] array, int startIndexInclusive, int endIndexExclusive) {
//noinspection unchecked
return (T[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static long[] subArray(long[] array, int startIndexInclusive, int endIndexExclusive) {
return (long[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static int[] subArray(int[] array, int startIndexInclusive, int endIndexExclusive) {
return (int[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static short[] subArray(short[] array, int startIndexInclusive, int endIndexExclusive) {
return (short[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static char[] subArray(char[] array, int startIndexInclusive, int endIndexExclusive) {
return (char[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static byte[] subArray(byte[] array, int startIndexInclusive, int endIndexExclusive) {
return (byte[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static double[] subArray(double[] array, int startIndexInclusive, int endIndexExclusive) {
return (double[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static float[] subArray(float[] array, int startIndexInclusive, int endIndexExclusive) {
return (float[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
public static boolean[] subArray(boolean[] array, int startIndexInclusive, int endIndexExclusive) {
return (boolean[]) realSubArray(array, startIndexInclusive, endIndexExclusive);
}
private static Object realSubArray(Object array, int startIndexInclusive, int endIndexExclusive) {
if (array == null) {
return null;
}
if (startIndexInclusive < 0) {
startIndexInclusive = 0;
}
int length = getLength(array);
if (endIndexExclusive > length) {
endIndexExclusive = length;
}
int newSize = endIndexExclusive - startIndexInclusive;
Class type = array.getClass().getComponentType();
if (newSize <= 0) {
return Array.newInstance(type, 0);
}
Object subArray = Array.newInstance(type, newSize);
System.arraycopy(array, startIndexInclusive, subArray, 0, newSize);
return subArray;
}
///////////////////////////////////////////////////////////////////////////
// add
///////////////////////////////////////////////////////////////////////////
/**
* <p>Copies the given array and adds the given element at the end of the new array.</p>
*
* <p>The new array contains the same elements of the input
* array plus the given element in the last position. The component type of
* the new array is the same as that of the input array.</p>
*
* <p>If the input array is <code>null</code>, a new one element array is returned
* whose component type is the same as the element.</p>
*
* <pre>
* ArrayUtils.realAdd(null, null) = [null]
* ArrayUtils.realAdd(null, "a") = ["a"]
* ArrayUtils.realAdd(["a"], null) = ["a", null]
* ArrayUtils.realAdd(["a"], "b") = ["a", "b"]
* ArrayUtils.realAdd(["a", "b"], "c") = ["a", "b", "c"]
* </pre>
*
* @param array the array to "realAdd" the element to, may be <code>null</code>
* @param element the object to realAdd
* @return A new array containing the existing elements plus the new element
*/
public static <T> T[] add(T[] array, T element) {
Class type = array != null ? array.getClass() : (element != null ? element.getClass() : Object.class);
return (T[]) realAddOne(array, element, type);
}
public static boolean[] add(boolean[] array, boolean element) {
return (boolean[]) realAddOne(array, element, Boolean.TYPE);
}
public static byte[] add(byte[] array, byte element) {
return (byte[]) realAddOne(array, element, Byte.TYPE);
}
public static char[] add(char[] array, char element) {
return (char[]) realAddOne(array, element, Character.TYPE);
}
public static double[] add(double[] array, double element) {
return (double[]) realAddOne(array, element, Double.TYPE);
}
public static float[] add(float[] array, float element) {
return (float[]) realAddOne(array, element, Float.TYPE);
}
public static int[] add(int[] array, int element) {
return (int[]) realAddOne(array, element, Integer.TYPE);
}
public static long[] add(long[] array, long element) {
return (long[]) realAddOne(array, element, Long.TYPE);
}
public static short[] add(short[] array, short element) {
return (short[]) realAddOne(array, element, Short.TYPE);
}
private static Object realAddOne(Object array, Object element, Class newArrayComponentType) {
Object newArray;
int arrayLength = 0;
if (array != null) {
arrayLength = getLength(array);
newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
System.arraycopy(array, 0, newArray, 0, arrayLength);
} else {
newArray = Array.newInstance(newArrayComponentType, 1);
}
Array.set(newArray, arrayLength, element);
return newArray;
}
/**
* <p>Adds all the elements of the given arrays into a new array.</p>
* <p>The new array contains all of the element of <code>array1</code> followed
* by all of the elements <code>array2</code>. When an array is returned, it is always
* a new array.</p>
*
* <pre>
* ArrayUtils.add(null, null) = null
* ArrayUtils.add(array1, null) = copy of array1
* ArrayUtils.add(null, array2) = copy of array2
* ArrayUtils.add([], []) = []
* ArrayUtils.add([null], [null]) = [null, null]
* ArrayUtils.add(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
* </pre>
*
* @param array1 the first array whose elements are added to the new array, may be <code>null</code>
* @param array2 the second array whose elements are added to the new array, may be <code>null</code>
* @return The new array, <code>null</code> if <code>null</code> array inputs.
* The type of the new array is the type of the first array.
*/
public static <T> T[] add(T[] array1, T[] array2) {
return (T[]) realAddArr(array1, array2);
}
public static boolean[] add(boolean[] array1, boolean[] array2) {
return (boolean[]) realAddArr(array1, array2);
}
public static char[] add(char[] array1, char[] array2) {
return (char[]) realAddArr(array1, array2);
}
public static byte[] add(byte[] array1, byte[] array2) {
return (byte[]) realAddArr(array1, array2);
}
public static short[] add(short[] array1, short[] array2) {
return (short[]) realAddArr(array1, array2);
}
public static int[] add(int[] array1, int[] array2) {
return (int[]) realAddArr(array1, array2);
}
public static long[] add(long[] array1, long[] array2) {
return (long[]) realAddArr(array1, array2);
}
public static float[] add(float[] array1, float[] array2) {
return (float[]) realAddArr(array1, array2);
}
public static double[] add(double[] array1, double[] array2) {
return (double[]) realAddArr(array1, array2);
}
private static Object realAddArr(Object array1, Object array2) {
if (array1 == null && array2 == null) return null;
if (array1 == null) {
return realCopy(array2);
}
if (array2 == null) {
return realCopy(array1);
}
int len1 = getLength(array1);
int len2 = getLength(array2);
Object joinedArray = Array.newInstance(array1.getClass().getComponentType(), len1 + len2);
System.arraycopy(array1, 0, joinedArray, 0, len1);
System.arraycopy(array2, 0, joinedArray, len1, len2);
return joinedArray;
}
/**
* <p>Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).</p>
*
* <p>This method returns a new array with the same elements of the input
* array plus the given element on the specified position. The component
* type of the returned array is always the same as that of the input
* array.</p>
*
* <p>If the input array is <code>null</code>, a new one element array is returned
* whose component type is the same as the element.</p>
*
* <pre>
* ArrayUtils.add(null, 0, null) = [null]
* ArrayUtils.add(null, 0, "a") = ["a"]
* ArrayUtils.add(["a"], 1, null) = ["a", null]
* ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
* ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
* </pre>
*
* @param array1 the array to realAdd the element to, may be <code>null</code>
* @param index the position of the new object
* @param array2 the array to realAdd
* @return A new array containing the existing elements and the new element
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index > array.length).
*/
public static <T> T[] add(T[] array1, int index, T[] array2) {
Class clss;
if (array1 != null) {
clss = array1.getClass().getComponentType();
} else if (array2 != null) {
clss = array2.getClass().getComponentType();
} else {
return (T[]) new Object[]{null};
}
return (T[]) realAddArr(array1, index, array2, clss);
}
public static boolean[] add(boolean[] array1, int index, boolean[] array2) {
return (boolean[]) realAddArr(array1, index, array2, Boolean.TYPE);
}
public static char[] add(char[] array1, int index, char[] array2) {
return (char[]) realAddArr(array1, index, array2, Character.TYPE);
}
public static byte[] add(byte[] array1, int index, byte[] array2) {
return (byte[]) realAddArr(array1, index, array2, Byte.TYPE);
}
public static short[] add(short[] array1, int index, short[] array2) {
return (short[]) realAddArr(array1, index, array2, Short.TYPE);
}
public static int[] add(int[] array1, int index, int[] array2) {
return (int[]) realAddArr(array1, index, array2, Integer.TYPE);
}
public static long[] add(long[] array1, int index, long[] array2) {
return (long[]) realAddArr(array1, index, array2, Long.TYPE);
}
public static float[] add(float[] array1, int index, float[] array2) {
return (float[]) realAddArr(array1, index, array2, Float.TYPE);
}
public static double[] add(double[] array1, int index, double[] array2) {
return (double[]) realAddArr(array1, index, array2, Double.TYPE);
}
private static Object realAddArr(Object array1, int index, Object array2, Class clss) {
if (array1 == null && array2 == null) return null;
int len1 = getLength(array1);
int len2 = getLength(array2);
if (len1 == 0) {
if (index != 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", array1 Length: 0");
}
return realCopy(array2);
}
if (len2 == 0) {
return realCopy(array1);
}
if (index > len1 || index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", array1 Length: " + len1);
}
Object joinedArray = Array.newInstance(array1.getClass().getComponentType(), len1 + len2);
if (index == len1) {
System.arraycopy(array1, 0, joinedArray, 0, len1);
System.arraycopy(array2, 0, joinedArray, len1, len2);
} else if (index == 0) {
System.arraycopy(array2, 0, joinedArray, 0, len2);
System.arraycopy(array1, 0, joinedArray, len2, len1);
} else {
System.arraycopy(array1, 0, joinedArray, 0, index);
System.arraycopy(array2, 0, joinedArray, index, len2);
System.arraycopy(array1, index, joinedArray, index + len2, len1 - index);
}
return joinedArray;
}
/**
* <p>Inserts the specified element at the specified position in the array.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).</p>
*
* <p>This method returns a new array with the same elements of the input
* array plus the given element on the specified position. The component
* type of the returned array is always the same as that of the input
* array.</p>
*
* <p>If the input array is <code>null</code>, a new one element array is returned
* whose component type is the same as the element.</p>
*
* <pre>
* ArrayUtils.add(null, 0, null) = [null]
* ArrayUtils.add(null, 0, "a") = ["a"]
* ArrayUtils.add(["a"], 1, null) = ["a", null]
* ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
* ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
* </pre>
*
* @param array the array to realAdd the element to, may be <code>null</code>
* @param index the position of the new object
* @param element the object to realAdd
* @return A new array containing the existing elements and the new element
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index > array.length).
*/
public static <T> T[] add(T[] array, int index, T element) {
Class clss;
if (array != null) {
clss = array.getClass().getComponentType();
} else if (element != null) {
clss = element.getClass();
} else {
return (T[]) new Object[]{null};
}
return (T[]) realAdd(array, index, element, clss);
}
public static boolean[] add(boolean[] array, int index, boolean element) {
return (boolean[]) realAdd(array, index, element, Boolean.TYPE);
}
public static char[] add(char[] array, int index, char element) {
return (char[]) realAdd(array, index, element, Character.TYPE);
}
public static byte[] add(byte[] array, int index, byte element) {
return (byte[]) realAdd(array, index, element, Byte.TYPE);
}
public static short[] add(short[] array, int index, short element) {
return (short[]) realAdd(array, index, element, Short.TYPE);
}
public static int[] add(int[] array, int index, int element) {
return (int[]) realAdd(array, index, element, Integer.TYPE);
}
public static long[] add(long[] array, int index, long element) {
return (long[]) realAdd(array, index, element, Long.TYPE);
}
public static float[] add(float[] array, int index, float element) {
return (float[]) realAdd(array, index, element, Float.TYPE);
}
public static double[] add(double[] array, int index, double element) {
return (double[]) realAdd(array, index, element, Double.TYPE);
}
private static Object realAdd(Object array, int index, Object element, Class clss) {
if (array == null) {
if (index != 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0");
}
Object joinedArray = Array.newInstance(clss, 1);
Array.set(joinedArray, 0, element);
return joinedArray;
}
int length = Array.getLength(array);
if (index > length || index < 0) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
}
Object result = Array.newInstance(clss, length + 1);
System.arraycopy(array, 0, result, 0, index);
Array.set(result, index, element);
if (index < length) {
System.arraycopy(array, index, result, index + 1, length - index);
}
return result;
}
///////////////////////////////////////////////////////////////////////////
// remove
///////////////////////////////////////////////////////////////////////////
/**
* <p>Removes the element at the specified position from the specified array.
* All subsequent elements are shifted to the left (substracts one from
* their indices).</p>
*
* <p>This method returns a new array with the same elements of the input
* array except the element on the specified position. The component
* type of the returned array is always the same as that of the input
* array.</p>
*
* <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
* will be thrown, because in that case no valid index can be specified.</p>
*
* <pre>
* ArrayUtils.remove(["a"], 0) = []
* ArrayUtils.remove(["a", "b"], 0) = ["b"]
* ArrayUtils.remove(["a", "b"], 1) = ["a"]
* ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
* </pre>
*
* @param array the array to remove the element from, may not be <code>null</code>
* @param index the position of the element to be removed
* @return A new array containing the existing elements except the element
* at the specified position.
* @throws IndexOutOfBoundsException if the index is out of range
* (index < 0 || index >= array.length), or if the array is <code>null</code>.
* @since 2.1
*/
public static Object[] remove(Object[] array, int index) {
return (Object[]) remove((Object) array, index);
}
/**
* <p>Removes the first occurrence of the specified element from the
* specified array. All subsequent elements are shifted to the left
* (substracts one from their indices). If the array doesn't contains
* such an element, no elements are removed from the array.</p>
*
* <p>This method returns a new array with the same elements of the input
* array except the first occurrence of the specified element. The component
* type of the returned array is always the same as that of the input
* array.</p>
*
* <pre>
* ArrayUtils.removeElement(null, "a") = null
* ArrayUtils.removeElement([], "a") = []
* ArrayUtils.removeElement(["a"], "b") = ["a"]
* ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
* ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
* </pre>
*
* @param array the array to remove the element from, may be <code>null</code>
* @param element the element to be removed
* @return A new array containing the existing elements except the first
* occurrence of the specified element.
* @since 2.1
*/
public static Object[] removeElement(Object[] array, Object element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static boolean[] remove(boolean[] array, int index) {
return (boolean[]) remove((Object) array, index);
}
public static boolean[] removeElement(boolean[] array, boolean element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static byte[] remove(byte[] array, int index) {
return (byte[]) remove((Object) array, index);
}
public static byte[] removeElement(byte[] array, byte element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static char[] remove(char[] array, int index) {
return (char[]) remove((Object) array, index);
}
public static char[] removeElement(char[] array, char element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static double[] remove(double[] array, int index) {
return (double[]) remove((Object) array, index);
}
public static double[] removeElement(double[] array, double element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static float[] remove(float[] array, int index) {
return (float[]) remove((Object) array, index);
}
public static float[] removeElement(float[] array, float element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static int[] remove(int[] array, int index) {
return (int[]) remove((Object) array, index);
}
public static int[] removeElement(int[] array, int element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static long[] remove(long[] array, int index) {
return (long[]) remove((Object) array, index);
}
public static long[] removeElement(long[] array, long element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
public static short[] remove(short[] array, int index) {
return (short[]) remove((Object) array, index);
}
public static short[] removeElement(short[] array, short element) {
int index = indexOf(array, element);
if (index == INDEX_NOT_FOUND) {
return copy(array);
}
return remove(array, index);
}
private static Object remove(Object array, int index) {
int length = getLength(array);
if (index < 0 || index >= length) {
throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
}
Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
System.arraycopy(array, 0, result, 0, index);
if (index < length - 1) {
System.arraycopy(array, index + 1, result, index, length - index - 1);
}
return result;
}
///////////////////////////////////////////////////////////////////////////
// object indexOf
///////////////////////////////////////////////////////////////////////////
public static int indexOf(Object[] array, Object objectToFind) {
return indexOf(array, objectToFind, 0);
}
public static int indexOf(Object[] array, final Object objectToFind, int startIndex) {
if (array == null) {
return INDEX_NOT_FOUND;
}
if (startIndex < 0) {
startIndex = 0;
}
if (objectToFind == null) {