KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > jodd > util > ArraysUtil


1
2 // Copyright (c) 2003-2007, Jodd Team (jodd.sf.net). All Rights Reserved.
3

4 package jodd.util;
5
6 import java.lang.reflect.Array JavaDoc;
7
8 /**
9  * More array utilities.
10  */

11 public class ArraysUtil {
12
13
14
15     // ---------------------------------------------------------------- join
16

17     /**
18      * Joins two arrays.
19      */

20     public static Object JavaDoc[] join(Object JavaDoc[] first, Object JavaDoc[] second) {
21         return join(first, second, null);
22     }
23
24     /**
25      * Joins two arrays.
26      */

27     public static Object JavaDoc[] join(Object JavaDoc[] first, Object JavaDoc[] second, Class JavaDoc elementType) {
28         if (elementType == null) {
29             elementType = first.getClass().getComponentType();
30         }
31         Object JavaDoc[] temp = (Object JavaDoc[]) Array.newInstance(elementType, first.length + second.length);
32         System.arraycopy(first, 0, temp, 0, first.length);
33         System.arraycopy(second, 0, temp, first.length, second.length);
34         return temp;
35     }
36
37
38     /**
39      * Joins two arrays.
40      */

41     public static String JavaDoc[] join(String JavaDoc[] first, String JavaDoc[] second) {
42         String JavaDoc[] temp = new String JavaDoc[first.length + second.length];
43         System.arraycopy(first, 0, temp, 0, first.length);
44         System.arraycopy(second, 0, temp, first.length, second.length);
45         return temp;
46     }
47
48     /**
49      * Joins two arrays.
50      */

51     public static byte[] join(byte[] first, byte[] second) {
52         byte[] temp = new byte[first.length + second.length];
53         System.arraycopy(first, 0, temp, 0, first.length);
54         System.arraycopy(second, 0, temp, first.length, second.length);
55         return temp;
56     }
57
58     /**
59      * Joins two arrays.
60      */

61     public static char[] join(char[] first, char[] second) {
62         char[] temp = new char[first.length + second.length];
63         System.arraycopy(first, 0, temp, 0, first.length);
64         System.arraycopy(second, 0, temp, first.length, second.length);
65         return temp;
66     }
67
68     /**
69      * Joins two arrays.
70      */

71     public static short[] join(short[] first, short[] second) {
72         short[] temp = new short[first.length + second.length];
73         System.arraycopy(first, 0, temp, 0, first.length);
74         System.arraycopy(second, 0, temp, first.length, second.length);
75         return temp;
76     }
77
78     /**
79      * Joins two arrays.
80      */

81     public static int[] join(int[] first, int[] second) {
82         int[] temp = new int[first.length + second.length];
83         System.arraycopy(first, 0, temp, 0, first.length);
84         System.arraycopy(second, 0, temp, first.length, second.length);
85         return temp;
86     }
87
88     /**
89      * Joins two arrays.
90      */

91     public static long[] join(long[] first, long[] second) {
92         long[] temp = new long[first.length + second.length];
93         System.arraycopy(first, 0, temp, 0, first.length);
94         System.arraycopy(second, 0, temp, first.length, second.length);
95         return temp;
96     }
97
98     /**
99      * Joins two arrays.
100      */

101     public static float[] join(float[] first, float[] second) {
102         float[] temp = new float[first.length + second.length];
103         System.arraycopy(first, 0, temp, 0, first.length);
104         System.arraycopy(second, 0, temp, first.length, second.length);
105         return temp;
106     }
107
108     /**
109      * Joins two arrays.
110      */

111     public static double[] join(double[] first, double[] second) {
112         double[] temp = new double[first.length + second.length];
113         System.arraycopy(first, 0, temp, 0, first.length);
114         System.arraycopy(second, 0, temp, first.length, second.length);
115         return temp;
116     }
117
118     /**
119      * Joins two arrays.
120      */

121     public static boolean[] join(boolean[] first, boolean[] second) {
122         boolean[] temp = new boolean[first.length + second.length];
123         System.arraycopy(first, 0, temp, 0, first.length);
124         System.arraycopy(second, 0, temp, first.length, second.length);
125         return temp;
126     }
127
128
129     // ---------------------------------------------------------------- resize
130

131     /**
132      * Resizes an array.
133      */

134     public static Object JavaDoc[] resize(Object JavaDoc buffer[], int newSize) {
135         return resize(buffer, newSize, null);
136     }
137         
138     /**
139      * Resizes an array.
140      */

141     public static Object JavaDoc[] resize(Object JavaDoc buffer[], int newSize, Class JavaDoc elementType) {
142         if (elementType == null) {
143             elementType = buffer.getClass().getComponentType();
144         }
145         Object JavaDoc[] temp = (Object JavaDoc[]) Array.newInstance(elementType, newSize);
146         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
147         return temp;
148     }
149
150     /**
151      * Resizes an array.
152      */

153     public static String JavaDoc[] resize(String JavaDoc buffer[], int newSize) {
154         String JavaDoc temp[] = new String JavaDoc[newSize];
155         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
156         return temp;
157     }
158
159     /**
160      * Resizes an array.
161      */

162     public static byte[] resize(byte buffer[], int newSize) {
163         byte temp[] = new byte[newSize];
164         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
165         return temp;
166     }
167
168     /**
169      * Resizes an array.
170      */

171     public static char[] resize(char buffer[], int newSize) {
172         char temp[] = new char[newSize];
173         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
174         return temp;
175     }
176
177     /**
178      * Resizes an array.
179      */

180     public static short[] resize(short buffer[], int newSize) {
181         short temp[] = new short[newSize];
182         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
183         return temp;
184     }
185
186     /**
187      * Resizes an array.
188      */

189     public static int[] resize(int buffer[], int newSize) {
190         int temp[] = new int[newSize];
191         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
192         return temp;
193     }
194
195     /**
196      * Resizes an array.
197      */

198     public static long[] resize(long buffer[], int newSize) {
199         long temp[] = new long[newSize];
200         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
201         return temp;
202     }
203
204     /**
205      * Resizes an array.
206      */

207     public static float[] resize(float buffer[], int newSize) {
208         float temp[] = new float[newSize];
209         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
210         return temp;
211     }
212
213     /**
214      * Resizes an array.
215      */

216     public static double[] resize(double buffer[], int newSize) {
217         double temp[] = new double[newSize];
218         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
219         return temp;
220     }
221
222     /**
223      * Resizes an array.
224      */

225     public static boolean[] resize(boolean buffer[], int newSize) {
226         boolean temp[] = new boolean[newSize];
227         System.arraycopy(buffer, 0, temp, 0, buffer.length >= newSize ? newSize : buffer.length);
228         return temp;
229     }
230
231
232     // ---------------------------------------------------------------- subarray
233

234     /**
235      * Returns subarray.
236      */

237     public static Object JavaDoc[] subarray(Object JavaDoc buffer[], int offset, int length) {
238         return subarray(buffer, offset, length, null);
239     }
240
241     /**
242      * Returns subarray.
243      */

244     public static Object JavaDoc[] subarray(Object JavaDoc buffer[], int offset, int length, Class JavaDoc elementType) {
245         if (elementType == null) {
246             elementType = buffer.getClass().getComponentType();
247         }
248         Object JavaDoc[] temp = (Object JavaDoc[]) Array.newInstance(elementType, length);
249         System.arraycopy(buffer, offset, temp, 0, length);
250         return temp;
251     }
252
253     /**
254      * Returns subarray.
255      */

256     public static String JavaDoc[] subarray(String JavaDoc buffer[], int offset, int length) {
257         String JavaDoc temp[] = new String JavaDoc[length];
258         System.arraycopy(buffer, offset, temp, 0, length);
259         return temp;
260     }
261
262     /**
263      * Returns subarray.
264      */

265     public static byte[] subarray(byte buffer[], int offset, int length) {
266         byte temp[] = new byte[length];
267         System.arraycopy(buffer, offset, temp, 0, length);
268         return temp;
269     }
270
271     /**
272      * Returns subarray.
273      */

274     public static char[] subarray(char buffer[], int offset, int length) {
275         char temp[] = new char[length];
276         System.arraycopy(buffer, offset, temp, 0, length);
277         return temp;
278     }
279
280     /**
281      * Returns subarray.
282      */

283     public static short[] subarray(short buffer[], int offset, int length) {
284         short temp[] = new short[length];
285         System.arraycopy(buffer, offset, temp, 0, length);
286         return temp;
287     }
288
289     /**
290      * Returns subarray.
291      */

292     public static int[] subarray(int buffer[], int offset, int length) {
293         int temp[] = new int[length];
294         System.arraycopy(buffer, offset, temp, 0, length);
295         return temp;
296     }
297
298     /**
299      * Returns subarray.
300      */

301     public static long[] subarray(long buffer[], int offset, int length) {
302         long temp[] = new long[length];
303         System.arraycopy(buffer, offset, temp, 0, length);
304         return temp;
305     }
306
307     /**
308      * Returns subarray.
309      */

310     public static float[] subarray(float buffer[], int offset, int length) {
311         float temp[] = new float[length];
312         System.arraycopy(buffer, offset, temp, 0, length);
313         return temp;
314     }
315
316     /**
317      * Returns subarray.
318      */

319     public static double[] subarray(double buffer[], int offset, int length) {
320         double temp[] = new double[length];
321         System.arraycopy(buffer, offset, temp, 0, length);
322         return temp;
323     }
324
325     /**
326      * Returns subarray.
327      */

328     public static boolean[] subarray(boolean buffer[], int offset, int length) {
329         boolean temp[] = new boolean[length];
330         System.arraycopy(buffer, offset, temp, 0, length);
331         return temp;
332     }
333
334
335     // ---------------------------------------------------------------- insert
336

337     /**
338      * Inserts one array into another.
339      */

340     public static Object JavaDoc[] insert(Object JavaDoc[] dest, Object JavaDoc[] src, int offset) {
341         return insert(dest, src, offset, null);
342     }
343         
344     /**
345      * Inserts one array into another.
346      */

347     public static Object JavaDoc[] insert(Object JavaDoc[] dest, Object JavaDoc[] src, int offset, Class JavaDoc elementType) {
348         if (elementType == null) {
349             elementType = dest.getClass().getComponentType();
350         }
351         Object JavaDoc[] temp = (Object JavaDoc[]) Array.newInstance(elementType, dest.length + src.length);
352         System.arraycopy(dest, 0, temp, 0, offset);
353         System.arraycopy(src, 0, temp, offset, src.length);
354         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
355         return temp;
356     }
357
358     /**
359      * Inserts one array into another.
360      */

361     public static String JavaDoc[] insert(String JavaDoc[] dest, String JavaDoc[] src, int offset) {
362         String JavaDoc[] temp = new String JavaDoc[dest.length + src.length];
363         System.arraycopy(dest, 0, temp, 0, offset);
364         System.arraycopy(src, 0, temp, offset, src.length);
365         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
366         return temp;
367     }
368
369     /**
370      * Inserts one array into another.
371      */

372     public static byte[] insert(byte[] dest, byte[] src, int offset) {
373         byte[] temp = new byte[dest.length + src.length];
374         System.arraycopy(dest, 0, temp, 0, offset);
375         System.arraycopy(src, 0, temp, offset, src.length);
376         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
377         return temp;
378     }
379
380     /**
381      * Inserts one array into another.
382      */

383     public static char[] insert(char[] dest, char[] src, int offset) {
384         char[] temp = new char[dest.length + src.length];
385         System.arraycopy(dest, 0, temp, 0, offset);
386         System.arraycopy(src, 0, temp, offset, src.length);
387         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
388         return temp;
389     }
390
391     /**
392      * Inserts one array into another.
393      */

394     public static short[] insert(short[] dest, short[] src, int offset) {
395         short[] temp = new short[dest.length + src.length];
396         System.arraycopy(dest, 0, temp, 0, offset);
397         System.arraycopy(src, 0, temp, offset, src.length);
398         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
399         return temp;
400     }
401
402     /**
403      * Inserts one array into another.
404      */

405     public static int[] insert(int[] dest, int[] src, int offset) {
406         int[] temp = new int[dest.length + src.length];
407         System.arraycopy(dest, 0, temp, 0, offset);
408         System.arraycopy(src, 0, temp, offset, src.length);
409         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
410         return temp;
411     }
412
413     /**
414      * Inserts one array into another.
415      */

416     public static long[] insert(long[] dest, long[] src, int offset) {
417         long[] temp = new long[dest.length + src.length];
418         System.arraycopy(dest, 0, temp, 0, offset);
419         System.arraycopy(src, 0, temp, offset, src.length);
420         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
421         return temp;
422     }
423
424     /**
425      * Inserts one array into another.
426      */

427     public static float[] insert(float[] dest, float[] src, int offset) {
428         float[] temp = new float[dest.length + src.length];
429         System.arraycopy(dest, 0, temp, 0, offset);
430         System.arraycopy(src, 0, temp, offset, src.length);
431         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
432         return temp;
433     }
434
435     /**
436      * Inserts one array into another.
437      */

438     public static double[] insert(double[] dest, double[] src, int offset) {
439         double[] temp = new double[dest.length + src.length];
440         System.arraycopy(dest, 0, temp, 0, offset);
441         System.arraycopy(src, 0, temp, offset, src.length);
442         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
443         return temp;
444     }
445
446     /**
447      * Inserts one array into another.
448      */

449     public static boolean[] insert(boolean[] dest, boolean[] src, int offset) {
450         boolean[] temp = new boolean[dest.length + src.length];
451         System.arraycopy(dest, 0, temp, 0, offset);
452         System.arraycopy(src, 0, temp, offset, src.length);
453         System.arraycopy(dest, offset, temp, src.length + offset, dest.length - offset);
454         return temp;
455     }
456
457
458     // ---------------------------------------------------------------- insertAt
459

460     /**
461      * Inserts one array into another by replacing specified offset.
462      */

463     public static Object JavaDoc[] insertAt(Object JavaDoc[] dest, Object JavaDoc[] src, int offset) {
464         return insertAt(dest, src, offset, null);
465     }
466
467     /**
468      * Inserts one array into another by replacing specified offset.
469      */

470     public static Object JavaDoc[] insertAt(Object JavaDoc[] dest, Object JavaDoc[] src, int offset, Class JavaDoc elementType) {
471         if (elementType == null) {
472             elementType = dest.getClass().getComponentType();
473         }
474         Object JavaDoc[] temp = (Object JavaDoc[]) Array.newInstance(elementType, dest.length + src.length - 1);
475         System.arraycopy(dest, 0, temp, 0, offset);
476         System.arraycopy(src, 0, temp, offset, src.length);
477         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
478         return temp;
479     }
480
481     /**
482      * Inserts one array into another by replacing specified offset.
483      */

484     public static String JavaDoc[] insertAt(String JavaDoc[] dest, String JavaDoc[] src, int offset) {
485         String JavaDoc[] temp = new String JavaDoc[dest.length + src.length - 1];
486         System.arraycopy(dest, 0, temp, 0, offset);
487         System.arraycopy(src, 0, temp, offset, src.length);
488         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
489         return temp;
490     }
491
492     /**
493      * Inserts one array into another by replacing specified offset.
494      */

495     public static byte[] insertAt(byte[] dest, byte[] src, int offset) {
496         byte[] temp = new byte[dest.length + src.length - 1];
497         System.arraycopy(dest, 0, temp, 0, offset);
498         System.arraycopy(src, 0, temp, offset, src.length);
499         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
500         return temp;
501     }
502
503     /**
504      * Inserts one array into another by replacing specified offset.
505      */

506     public static char[] insertAt(char[] dest, char[] src, int offset) {
507         char[] temp = new char[dest.length + src.length - 1];
508         System.arraycopy(dest, 0, temp, 0, offset);
509         System.arraycopy(src, 0, temp, offset, src.length);
510         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
511         return temp;
512     }
513
514     /**
515      * Inserts one array into another by replacing specified offset.
516      */

517     public static short[] insertAt(short[] dest, short[] src, int offset) {
518         short[] temp = new short[dest.length + src.length - 1];
519         System.arraycopy(dest, 0, temp, 0, offset);
520         System.arraycopy(src, 0, temp, offset, src.length);
521         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
522         return temp;
523     }
524
525     /**
526      * Inserts one array into another by replacing specified offset.
527      */

528     public static int[] insertAt(int[] dest, int[] src, int offset) {
529         int[] temp = new int[dest.length + src.length - 1];
530         System.arraycopy(dest, 0, temp, 0, offset);
531         System.arraycopy(src, 0, temp, offset, src.length);
532         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
533         return temp;
534     }
535
536     /**
537      * Inserts one array into another by replacing specified offset.
538      */

539     public static long[] insertAt(long[] dest, long[] src, int offset) {
540         long[] temp = new long[dest.length + src.length - 1];
541         System.arraycopy(dest, 0, temp, 0, offset);
542         System.arraycopy(src, 0, temp, offset, src.length);
543         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
544         return temp;
545     }
546
547     /**
548      * Inserts one array into another by replacing specified offset.
549      */

550     public static float[] insertAt(float[] dest, float[] src, int offset) {
551         float[] temp = new float[dest.length + src.length - 1];
552         System.arraycopy(dest, 0, temp, 0, offset);
553         System.arraycopy(src, 0, temp, offset, src.length);
554         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
555         return temp;
556     }
557
558     /**
559      * Inserts one array into another by replacing specified offset.
560      */

561     public static double[] insertAt(double[] dest, double[] src, int offset) {
562         double[] temp = new double[dest.length + src.length - 1];
563         System.arraycopy(dest, 0, temp, 0, offset);
564         System.arraycopy(src, 0, temp, offset, src.length);
565         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
566         return temp;
567     }
568
569     /**
570      * Inserts one array into another by replacing specified offset.
571      */

572     public static boolean[] insertAt(boolean[] dest, boolean[] src, int offset) {
573         boolean[] temp = new boolean[dest.length + src.length - 1];
574         System.arraycopy(dest, 0, temp, 0, offset);
575         System.arraycopy(src, 0, temp, offset, src.length);
576         System.arraycopy(dest, offset + 1, temp, src.length + offset, dest.length - offset - 1);
577         return temp;
578     }
579
580
581     // ---------------------------------------------------------------- indexof
582

583
584     /**
585      * Finds the first occurence in an array.
586      */

587     static public int indexOf(byte[] array, byte value) {
588         for (int i = 0; i < array.length; i++) {
589             if (array[i] == value) {
590                 return i;
591             }
592         }
593         return -1;
594     }
595     /**
596      * Finds the first occurence in an array from specified given position.
597      */

598     public static int indexOf(byte[] array, byte value, int startIndex) {
599         for (int i = startIndex; i < array.length; i++) {
600             if (array[i] == value) {
601                 return i;
602             }
603         }
604         return -1;
605     }
606     /**
607      * Finds the first occurence in an array from specified given position and upto given length.
608      */

609     public static int indexOf(byte[] array, byte value, int startIndex, int endIndex) {
610         for (int i = startIndex; i < endIndex; i++) {
611             if (array[i] == value) {
612                 return i;
613             }
614         }
615         return -1;
616     }
617
618     /**
619      * Finds the first occurence in an array.
620      */

621     static public int indexOf(char[] array, char value) {
622         for (int i = 0; i < array.length; i++) {
623             if (array[i] == value) {
624                 return i;
625             }
626         }
627         return -1;
628     }
629     /**
630      * Finds the first occurence in an array from specified given position.
631      */

632     public static int indexOf(char[] array, char value, int startIndex) {
633         for (int i = startIndex; i < array.length; i++) {
634             if (array[i] == value) {
635                 return i;
636             }
637         }
638         return -1;
639     }
640     /**
641      * Finds the first occurence in an array from specified given position and upto given length.
642      */

643     public static int indexOf(char[] array, char value, int startIndex, int endIndex) {
644         for (int i = startIndex; i < endIndex; i++) {
645             if (array[i] == value) {
646                 return i;
647             }
648         }
649         return -1;
650     }
651
652     /**
653      * Finds the first occurence in an array.
654      */

655     static public int indexOf(short[] array, short value) {
656         for (int i = 0; i < array.length; i++) {
657             if (array[i] == value) {
658                 return i;
659             }
660         }
661         return -1;
662     }
663     /**
664      * Finds the first occurence in an array from specified given position.
665      */

666     public static int indexOf(short[] array, short value, int startIndex) {
667         for (int i = startIndex; i < array.length; i++) {
668             if (array[i] == value) {
669                 return i;
670             }
671         }
672         return -1;
673     }
674     /**
675      * Finds the first occurence in an array from specified given position and upto given length.
676      */

677     public static int indexOf(short[] array, short value, int startIndex, int endIndex) {
678         for (int i = startIndex; i < endIndex; i++) {
679             if (array[i] == value) {
680                 return i;
681             }
682         }
683         return -1;
684     }
685
686     /**
687      * Finds the first occurence in an array.
688      */

689     static public int indexOf(int[] array, int value) {
690         for (int i = 0; i < array.length; i++) {
691             if (array[i] == value) {
692                 return i;
693             }
694         }
695         return -1;
696     }
697     /**
698      * Finds the first occurence in an array from specified given position.
699      */

700     public static int indexOf(int[] array, int value, int startIndex) {
701         for (int i = startIndex; i < array.length; i++) {
702             if (array[i] == value) {
703                 return i;
704             }
705         }
706         return -1;
707     }
708     /**
709      * Finds the first occurence in an array from specified given position and upto given length.
710      */

711     public static int indexOf(int[] array, int value, int startIndex, int endIndex) {
712         for (int i = startIndex; i < endIndex; i++) {
713             if (array[i] == value) {
714                 return i;
715             }
716         }
717         return -1;
718     }
719
720     /**
721      * Finds the first occurence in an array.
722      */

723     static public int indexOf(long[] array, long value) {
724         for (int i = 0; i < array.length; i++) {
725             if (array[i] == value) {
726                 return i;
727             }
728         }
729         return -1;
730     }
731     /**
732      * Finds the first occurence in an array from specified given position.
733      */

734     public static int indexOf(long[] array, long value, int startIndex) {
735         for (int i = startIndex; i < array.length; i++) {
736             if (array[i] == value) {
737                 return i;
738             }
739         }
740         return -1;
741     }
742     /**
743      * Finds the first occurence in an array from specified given position and upto given length.
744      */

745     public static int indexOf(long[] array, long value, int startIndex, int endIndex) {
746         for (int i = startIndex; i < endIndex; i++) {
747             if (array[i] == value) {
748                 return i;
749             }
750         }
751         return -1;
752     }
753
754     /**
755      * Finds the first occurence in an array.
756      */

757     static public int indexOf(float[] array, float value) {
758         for (int i = 0; i < array.length; i++) {
759             if (array[i] == value) {
760                 return i;
761             }
762         }
763         return -1;
764     }
765     /**
766      * Finds the first occurence in an array from specified given position.
767      */

768     public static int indexOf(float[] array, float value, int startIndex) {
769         for (int i = startIndex; i < array.length; i++) {
770             if (array[i] == value) {
771                 return i;
772             }
773         }
774         return -1;
775     }
776     /**
777      * Finds the first occurence in an array from specified given position and upto given length.
778      */

779     public static int indexOf(float[] array, float value, int startIndex, int endIndex) {
780         for (int i = startIndex; i < endIndex; i++) {
781             if (array[i] == value) {
782                 return i;
783             }
784         }
785         return -1;
786     }
787
788     /**
789      * Finds the first occurence in an array.
790      */

791     static public int indexOf(double[] array, double value) {
792         for (int i = 0; i < array.length; i++) {
793             if (array[i] == value) {
794                 return i;
795             }
796         }
797         return -1;
798     }
799     /**
800      * Finds the first occurence in an array from specified given position.
801      */

802     public static int indexOf(double[] array, double value, int startIndex) {
803         for (int i = startIndex; i < array.length; i++) {
804             if (array[i] == value) {
805                 return i;
806             }
807         }
808         return -1;
809     }
810     /**
811      * Finds the first occurence in an array from specified given position and upto given length.
812      */

813     public static int indexOf(double[] array, double value, int startIndex, int endIndex) {
814         for (int i = startIndex; i < endIndex; i++) {
815             if (array[i] == value) {
816                 return i;
817             }
818         }
819         return -1;
820     }
821
822     /**
823      * Finds the first occurence in an array.
824      */

825     static public int indexOf(boolean[] array, boolean value) {
826         for (int i = 0; i < array.length; i++) {
827             if (array[i] == value) {
828                 return i;
829             }
830         }
831         return -1;
832     }
833     /**
834      * Finds the first occurence in an array from specified given position.
835      */

836     public static int indexOf(boolean[] array, boolean value, int startIndex) {
837         for (int i = startIndex; i < array.length; i++) {
838             if (array[i] == value) {
839                 return i;
840             }
841         }
842         return -1;
843     }
844     /**
845      * Finds the first occurence in an array from specified given position and upto given length.
846      */

847     public static int indexOf(boolean[] array, boolean value, int startIndex, int endIndex) {
848         for (int i = startIndex; i < endIndex; i++) {
849             if (array[i] == value) {
850                 return i;
851             }
852         }
853         return -1;
854     }
855
856
857     // ---------------------------------------------------------------- indexof 2
858

859
860     /**
861      * Finds the first occurence in an array.
862      */

863     public static int indexOf(byte[] array, byte[] sub) {
864         return indexOf(array, sub, 0, array.length);
865     }
866
867     /**
868      * Finds the first occurence in an array from specified given position.
869      */

870     public static int indexOf(byte[] array, byte[] sub, int startIndex) {
871         return indexOf(array, sub, startIndex, array.length);
872     }
873
874     /**
875      * Finds the first occurence in an array from specified given position and upto given length.
876      */

877     public static int indexOf(byte[] array, byte[] sub, int startIndex, int endIndex) {
878         int sublen = sub.length;
879         if (sublen == 0) {
880             return startIndex;
881         }
882         int total = endIndex - sublen + 1;
883         byte c = sub[0];
884     mainloop:
885         for (int i = startIndex; i < total; i++) {
886             if (array[i] != c) {
887                 continue;
888             }
889             int j = 1;
890             int k = i + 1;
891             while (j < sublen) {
892                 if (sub[j] != array[k]) {
893                     continue mainloop;
894                 }
895                 j++; k++;
896             }
897             return i;
898         }
899         return -1;
900     }
901
902     /**
903      * Finds the first occurence in an array.
904      */

905     public static int indexOf(char[] array, char[] sub) {
906         return indexOf(array, sub, 0, array.length);
907     }
908
909     /**
910      * Finds the first occurence in an array from specified given position.
911      */

912     public static int indexOf(char[] array, char[] sub, int startIndex) {
913         return indexOf(array, sub, startIndex, array.length);
914     }
915
916     /**
917      * Finds the first occurence in an array from specified given position and upto given length.
918      */

919     public static int indexOf(char[] array, char[] sub, int startIndex, int endIndex) {
920         int sublen = sub.length;
921         if (sublen == 0) {
922             return startIndex;
923         }
924         int total = endIndex - sublen + 1;
925         char c = sub[0];
926     mainloop:
927         for (int i = startIndex; i < total; i++) {
928             if (array[i] != c) {
929                 continue;
930             }
931             int j = 1;
932             int k = i + 1;
933             while (j < sublen) {
934                 if (sub[j] != array[k]) {
935                     continue mainloop;
936                 }
937                 j++; k++;
938             }
939             return i;
940         }
941         return -1;
942     }
943
944     /**
945      * Finds the first occurence in an array.
946      */

947     public static int indexOf(short[] array, short[] sub) {
948         return indexOf(array, sub, 0, array.length);
949     }
950
951     /**
952      * Finds the first occurence in an array from specified given position.
953      */

954     public static int indexOf(short[] array, short[] sub, int startIndex) {
955         return indexOf(array, sub, startIndex, array.length);
956     }
957
958     /**
959      * Finds the first occurence in an array from specified given position and upto given length.
960      */

961     public static int indexOf(short[] array, short[] sub, int startIndex, int endIndex) {
962         int sublen = sub.length;
963         if (sublen == 0) {
964             return startIndex;
965         }
966         int total = endIndex - sublen + 1;
967         short c = sub[0];
968     mainloop:
969         for (int i = startIndex; i < total; i++) {
970             if (array[i] != c) {
971                 continue;
972             }
973             int j = 1;
974             int k = i + 1;
975             while (j < sublen) {
976                 if (sub[j] != array[k]) {
977                     continue mainloop;
978                 }
979                 j++; k++;
980             }
981             return i;
982         }
983         return -1;
984     }
985
986     /**
987      * Finds the first occurence in an array.
988      */

989     public static int indexOf(int[] array, int[] sub) {
990         return indexOf(array, sub, 0, array.length);
991     }
992
993     /**
994      * Finds the first occurence in an array from specified given position.
995      */

996     public static int indexOf(int[] array, int[] sub, int startIndex) {
997         return indexOf(array, sub, startIndex, array.length);
998     }
999
1000    /**
1001     * Finds the first occurence in an array from specified given position and upto given length.
1002     */

1003    public static int indexOf(int[] array, int[] sub, int startIndex, int endIndex) {
1004        int sublen = sub.length;
1005        if (sublen == 0) {
1006            return startIndex;
1007        }
1008        int total = endIndex - sublen + 1;
1009        int c = sub[0];
1010    mainloop:
1011        for (int i = startIndex; i < total; i++) {
1012            if (array[i] != c) {
1013                continue;
1014            }
1015            int j = 1;
1016            int k = i + 1;
1017            while (j < sublen) {
1018                if (sub[j] != array[k]) {
1019                    continue mainloop;
1020                }
1021                j++; k++;
1022            }
1023            return i;
1024        }
1025        return -1;
1026    }
1027
1028    /**
1029     * Finds the first occurence in an array.
1030     */

1031    public static int indexOf(long[] array, long[] sub) {
1032        return indexOf(array, sub, 0, array.length);
1033    }
1034
1035    /**
1036     * Finds the first occurence in an array from specified given position.
1037     */

1038    public static int indexOf(long[] array, long[] sub, int startIndex) {
1039        return indexOf(array, sub, startIndex, array.length);
1040    }
1041
1042    /**
1043     * Finds the first occurence in an array from specified given position and upto given length.
1044     */

1045    public static int indexOf(long[] array, long[] sub, int startIndex, int endIndex) {
1046        int sublen = sub.length;
1047        if (sublen == 0) {
1048            return startIndex;
1049        }
1050        int total = endIndex - sublen + 1;
1051        long c = sub[0];
1052    mainloop:
1053        for (int i = startIndex; i < total; i++) {
1054            if (array[i] != c) {
1055                continue;
1056            }
1057            int j = 1;
1058            int k = i + 1;
1059            while (j < sublen) {
1060                if (sub[j] != array[k]) {
1061                    continue mainloop;
1062                }
1063                j++; k++;
1064            }
1065            return i;
1066        }
1067        return -1;
1068    }
1069
1070    /**
1071     * Finds the first occurence in an array.
1072     */

1073    public static int indexOf(float[] array, float[] sub) {
1074        return indexOf(array, sub, 0, array.length);
1075    }
1076
1077    /**
1078     * Finds the first occurence in an array from specified given position.
1079     */

1080    public static int indexOf(float[] array, float[] sub, int startIndex) {
1081        return indexOf(array, sub, startIndex, array.length);
1082    }
1083
1084    /**
1085     * Finds the first occurence in an array from specified given position and upto given length.
1086     */

1087    public static int indexOf(float[] array, float[] sub, int startIndex, int endIndex) {
1088        int sublen = sub.length;
1089        if (sublen == 0) {
1090            return startIndex;
1091        }
1092        int total = endIndex - sublen + 1;
1093        float c = sub[0];
1094    mainloop:
1095        for (int i = startIndex; i < total; i++) {
1096            if (array[i] != c) {
1097                continue;
1098            }
1099            int j = 1;
1100            int k = i + 1;
1101            while (j < sublen) {
1102                if (sub[j] != array[k]) {
1103                    continue mainloop;
1104                }
1105                j++; k++;
1106            }
1107            return i;
1108        }
1109        return -1;
1110    }
1111
1112    /**
1113     * Finds the first occurence in an array.
1114     */

1115    public static int indexOf(double[] array, double[] sub) {
1116        return indexOf(array, sub, 0, array.length);
1117    }
1118
1119    /**
1120     * Finds the first occurence in an array from specified given position.
1121     */

1122    public static int indexOf(double[] array, double[] sub, int startIndex) {
1123        return indexOf(array, sub, startIndex, array.length);
1124    }
1125
1126    /**
1127     * Finds the first occurence in an array from specified given position and upto given length.
1128     */

1129    public static int indexOf(double[] array, double[] sub, int startIndex, int endIndex) {
1130        int sublen = sub.length;
1131        if (sublen == 0) {
1132            return startIndex;
1133        }
1134        int total = endIndex - sublen + 1;
1135        double c = sub[0];
1136    mainloop:
1137        for (int i = startIndex; i < total; i++) {
1138            if (array[i] != c) {
1139                continue;
1140            }
1141            int j = 1;
1142            int k = i + 1;
1143            while (j < sublen) {
1144                if (sub[j] != array[k]) {
1145                    continue mainloop;
1146                }
1147                j++; k++;
1148            }
1149            return i;
1150        }
1151        return -1;
1152    }
1153
1154    /**
1155     * Finds the first occurence in an array.
1156     */

1157    public static int indexOf(boolean[] array, boolean[] sub) {
1158        return indexOf(array, sub, 0, array.length);
1159    }
1160
1161    /**
1162     * Finds the first occurence in an array from specified given position.
1163     */

1164    public static int indexOf(boolean[] array, boolean[] sub, int startIndex) {
1165        return indexOf(array, sub, startIndex, array.length);
1166    }
1167
1168    /**
1169     * Finds the first occurence in an array from specified given position and upto given length.
1170     */

1171    public static int indexOf(boolean[] array, boolean[] sub, int startIndex, int endIndex) {
1172        int sublen = sub.length;
1173        if (sublen == 0) {
1174            return startIndex;
1175        }
1176        int total = endIndex - sublen + 1;
1177        boolean c = sub[0];
1178    mainloop:
1179        for (int i = startIndex; i < total; i++) {
1180            if (array[i] != c) {
1181                continue;
1182            }
1183            int j = 1;
1184            int k = i + 1;
1185            while (j < sublen) {
1186                if (sub[j] != array[k]) {
1187                    continue mainloop;
1188                }
1189                j++; k++;
1190            }
1191            return i;
1192        }
1193        return -1;
1194    }
1195}
Popular Tags