1 16 package org.apache.commons.lang; 17 18 import java.lang.reflect.Array ; 19 import java.util.HashMap ; 20 import java.util.Map ; 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 49 public class ArrayUtils { 50 51 54 public static final Object [] EMPTY_OBJECT_ARRAY = new Object [0]; 55 58 public static final Class [] EMPTY_CLASS_ARRAY = new Class [0]; 59 62 public static final String [] EMPTY_STRING_ARRAY = new String [0]; 63 66 public static final long[] EMPTY_LONG_ARRAY = new long[0]; 67 70 public static final Long [] EMPTY_LONG_OBJECT_ARRAY = new Long [0]; 71 74 public static final int[] EMPTY_INT_ARRAY = new int[0]; 75 78 public static final Integer [] EMPTY_INTEGER_OBJECT_ARRAY = new Integer [0]; 79 82 public static final short[] EMPTY_SHORT_ARRAY = new short[0]; 83 86 public static final Short [] EMPTY_SHORT_OBJECT_ARRAY = new Short [0]; 87 90 public static final byte[] EMPTY_BYTE_ARRAY = new byte[0]; 91 94 public static final Byte [] EMPTY_BYTE_OBJECT_ARRAY = new Byte [0]; 95 98 public static final double[] EMPTY_DOUBLE_ARRAY = new double[0]; 99 102 public static final Double [] EMPTY_DOUBLE_OBJECT_ARRAY = new Double [0]; 103 106 public static final float[] EMPTY_FLOAT_ARRAY = new float[0]; 107 110 public static final Float [] EMPTY_FLOAT_OBJECT_ARRAY = new Float [0]; 111 114 public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0]; 115 118 public static final Boolean [] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean [0]; 119 122 public static final char[] EMPTY_CHAR_ARRAY = new char[0]; 123 126 public static final Character [] EMPTY_CHARACTER_OBJECT_ARRAY = new Character [0]; 127 128 135 public ArrayUtils() { 136 } 137 138 151 public static String toString(Object array) { 152 return toString(array, "{}"); 153 } 154 155 167 public static String toString(Object array, String stringIfNull) { 168 if (array == null) { 169 return stringIfNull; 170 } 171 return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString(); 172 } 173 174 182 public static int hashCode(Object array) { 183 return new HashCodeBuilder().append(array).toHashCode(); 184 } 185 186 196 public static boolean isEquals(Object array1, Object array2) { 197 return new EqualsBuilder().append(array1, array2).isEquals(); 198 } 199 200 227 public static Map toMap(Object [] array) { 228 if (array == null) { 229 return null; 230 } 231 final Map map = new HashMap ((int) (array.length * 1.5)); 232 for (int i = 0; i < array.length; i++) { 233 Object object = array[i]; 234 if (object instanceof Map.Entry ) { 235 Map.Entry entry = (Map.Entry ) object; 236 map.put(entry.getKey(), entry.getValue()); 237 } else if (object instanceof Object []) { 238 Object [] entry = (Object []) object; 239 if (entry.length < 2) { 240 throw new IllegalArgumentException ("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 ("Array element " + i + ", '" 247 + object 248 + "', is neither of type Map.Entry nor an Array"); 249 } 250 } 251 return map; 252 } 253 254 268 public static Object [] clone(Object [] array) { 269 if (array == null) { 270 return null; 271 } 272 return (Object []) array.clone(); 273 } 274 275 284 public static long[] clone(long[] array) { 285 if (array == null) { 286 return null; 287 } 288 return (long[]) array.clone(); 289 } 290 291 300 public static int[] clone(int[] array) { 301 if (array == null) { 302 return null; 303 } 304 return (int[]) array.clone(); 305 } 306 307 316 public static short[] clone(short[] array) { 317 if (array == null) { 318 return null; 319 } 320 return (short[]) array.clone(); 321 } 322 323 332 public static char[] clone(char[] array) { 333 if (array == null) { 334 return null; 335 } 336 return (char[]) array.clone(); 337 } 338 339 348 public static byte[] clone(byte[] array) { 349 if (array == null) { 350 return null; 351 } 352 return (byte[]) array.clone(); 353 } 354 355 364 public static double[] clone(double[] array) { 365 if (array == null) { 366 return null; 367 } 368 return (double[]) array.clone(); 369 } 370 371 380 public static float[] clone(float[] array) { 381 if (array == null) { 382 return null; 383 } 384 return (float[]) array.clone(); 385 } 386 387 396 public static boolean[] clone(boolean[] array) { 397 if (array == null) { 398 return null; 399 } 400 return (boolean[]) array.clone(); 401 } 402 403 432 public static Object [] subarray(Object [] 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 type = array.getClass().getComponentType(); 444 if (newSize <= 0) { 445 return (Object []) Array.newInstance(type, 0); 446 } 447 Object [] subarray = (Object []) Array.newInstance(type, newSize); 448 System.arraycopy(array, startIndexInclusive, subarray, 0, newSize); 449 return subarray; 450 } 451 452 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 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 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 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 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 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 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 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 777 public static boolean isSameLength(Object [] array1, Object [] 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 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 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 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 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 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 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 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 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 951 public static int getLength(Object array) { 952 if (array == null) { 953 return 0; 954 } else { 955 return Array.getLength(array); 956 } 957 } 958 959 968 public static boolean isSameType(Object array1, Object array2) { 969 if (array1 == null || array2 == null) { 970 throw new IllegalArgumentException ("The Array must not be null"); 971 } 972 return array1.getClass().getName().equals(array2.getClass().getName()); 973 } 974 975 986 public static void reverse(Object [] array) { 987 if (array == null) { 988 return; 989 } 990 int i = 0; 991 int j = array.length - 1; 992 Object 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 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 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 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 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 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 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 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 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 1189 1201 public static int indexOf(Object [] array, Object objectToFind) { 1202 return indexOf(array, objectToFind, 0); 1203 } 1204 1205 1219 public static int indexOf(Object [] array, Object 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 1252 public static int lastIndexOf(Object [] array, Object objectToFind) { 1253 return lastIndexOf(array, objectToFind, Integer.MAX_VALUE); 1254 } 1255 1256 1270 public static int lastIndexOf(Object [] array, Object 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 1304 public static boolean contains(Object [] array, Object objectToFind) { 1305 return indexOf(array, objectToFind) != -1; 1306 } 1307 1308 1320 public static int indexOf(long[] array, long valueToFind) { 1321 return indexOf(array, valueToFind, 0); 1322 } 1323 1324 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 1363 public static int lastIndexOf(long[] array, long valueToFind) { 1364 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 1365 } 1366 1367 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 1407 public static boolean contains(long[] array, long valueToFind) { 1408 return indexOf(array, valueToFind) != -1; 1409 } 1410 1411 1423 public static int indexOf(int[] array, int valueToFind) { 1424 return indexOf(array, valueToFind, 0); 1425 } 1426 1427 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 1466 public static int lastIndexOf(int[] array, int valueToFind) { 1467 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 1468 } 1469 1470 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 1510 public static boolean contains(int[] array, int valueToFind) { 1511 return indexOf(array, valueToFind) != -1; 1512 } 1513 1514 1526 public static int indexOf(short[] array, short valueToFind) { 1527 return indexOf(array, valueToFind, 0); 1528 } 1529 1530 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 1569 public static int lastIndexOf(short[] array, short valueToFind) { 1570 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 1571 } 1572 1573 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 1613 public static boolean contains(short[] array, short valueToFind) { 1614 return indexOf(array, valueToFind) != -1; 1615 } 1616 1617 1630 public static int indexOf(char[] array, char valueToFind) { 1631 return indexOf(array, valueToFind, 0); 1632 } 1633 1634 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 1675 public static int lastIndexOf(char[] array, char valueToFind) { 1676 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 1677 } 1678 1679 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 1721 public static boolean contains(char[] array, char valueToFind) { 1722 return indexOf(array, valueToFind) != -1; 1723 } 1724 1725 1737 public static int indexOf(byte[] array, byte valueToFind) { 1738 return indexOf(array, valueToFind, 0); 1739 } 1740 1741 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 1780 public static int lastIndexOf(byte[] array, byte valueToFind) { 1781 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 1782 } 1783 1784 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 1824 public static boolean contains(byte[] array, byte valueToFind) { 1825 return indexOf(array, valueToFind) != -1; 1826 } 1827 1828 1840 public static int indexOf(double[] array, double valueToFind) { 1841 return indexOf(array, valueToFind, 0); 1842 } 1843 1844 1857 public static int indexOf(double[] array, double valueToFind, double tolerance) { 1858 return indexOf(array, valueToFind, 0, tolerance); 1859 } 1860 1861 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 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 1934 public static int lastIndexOf(double[] array, double valueToFind) { 1935 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 1936 } 1937 1938 1951 public static int lastIndexOf(double[] array, double valueToFind, double tolerance) { 1952 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance); 1953 } 1954 1955 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 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 2031 public static boolean contains(double[] array, double valueToFind) { 2032 return indexOf(array, valueToFind) != -1; 2033 } 2034 2035 2048 public static boolean contains(double[] array, double valueToFind, double tolerance) { 2049 return indexOf(array, valueToFind, 0, tolerance) != -1; 2050 } 2051 2052 2064 public static int indexOf(float[] array, float valueToFind) { 2065 return indexOf(array, valueToFind, 0); 2066 } 2067 2068 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 2107 public static int lastIndexOf(float[] array, float valueToFind) { 2108 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 2109 } 2110 2111 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 2151 public static boolean contains(float[] array, float valueToFind) { 2152 return indexOf(array, valueToFind) != -1; 2153 } 2154 2155 2167 public static int indexOf(boolean[] array, boolean valueToFind) { 2168 return indexOf(array, valueToFind, 0); 2169 } 2170 2171 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 2210 public static int lastIndexOf(boolean[] array, boolean valueToFind) { 2211 return lastIndexOf(array, valueToFind, Integer.MAX_VALUE); 2212 } 2213 2214 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 2254 public static boolean contains(boolean[] array, boolean valueToFind) { 2255 return indexOf(array, valueToFind) != -1; 2256 } 2257 2258 2261 2272 public static long[] toPrimitive(Long [] 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 2294 public static long[] toPrimitive(Long [] 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 b = array[i]; 2303 result[i] = (b == null ? valueForNull : b.longValue()); 2304 } 2305 return result; 2306 } 2307 2308 2316 public static Long [] 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 [] result = new Long [array.length]; 2323 for (int i = 0; i < array.length; i++) { 2324 result[i] = new Long (array[i]); 2325 } 2326 return result; 2327 } 2328 2329 2340 public static int[] toPrimitive(Integer [] 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 2362 public static int[] toPrimitive(Integer [] 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 b = array[i]; 2371 result[i] = (b == null ? valueForNull : b.intValue()); 2372 } 2373 return result; 2374 } 2375 2376 2384 public static Integer [] 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 [] result = new Integer [array.length]; 2391 for (int i = 0; i < array.length; i++) { 2392 result[i] = new Integer (array[i]); 2393 } 2394 return result; 2395 } 2396 2397 2408 public static short[] toPrimitive(Short [] 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 2430 public static short[] toPrimitive(Short [] 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 b = array[i]; 2439 result[i] = (b == null ? valueForNull : b.shortValue()); 2440 } 2441 return result; 2442 } 2443 2444 2452 public static Short [] 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 [] result = new Short [array.length]; 2459 for (int i = 0; i < array.length; i++) { 2460 result[i] = new Short (array[i]); 2461 } 2462 return result; 2463 } 2464 2465 2476 public static byte[] toPrimitive(Byte [] 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 2498 public static byte[] toPrimitive(Byte [] 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 b = array[i]; 2507 result[i] = (b == null ? valueForNull : b.byteValue()); 2508 } 2509 return result; 2510 } 2511 2512 2520 public static Byte [] 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 [] result = new Byte [array.length]; 2527 for (int i = 0; i < array.length; i++) { 2528 result[i] = new Byte (array[i]); 2529 } 2530 return result; 2531 } 2532 2533 2544 public static double[] toPrimitive(Double [] 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 2566 public static double[] toPrimitive(Double [] 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 b = array[i]; 2575 result[i] = (b == null ? valueForNull : b.doubleValue()); 2576 } 2577 return result; 2578 } 2579 2580 2588 public static Double [] 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 [] result = new Double [array.length]; 2595 for (int i = 0; i < array.length; i++) { 2596 result[i] = new Double (array[i]); 2597 } 2598 return result; 2599 } 2600 2601 2612 public static float[] toPrimitive(Float [] 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 2634 public static float[] toPrimitive(Float [] 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 b = array[i]; 2643 result[i] = (b == null ? valueForNull : b.floatValue()); 2644 } 2645 return result; 2646 } 2647 2648 2656 public static Float [] 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 [] result = new Float [array.length]; 2663 for (int i = 0; i < array.length; i++) { 2664 result[i] = new Float (array[i]); 2665 } 2666 return result; 2667 } 2668 2669 2680 public static boolean[] toPrimitive(Boolean [] 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 2702 public static boolean[] toPrimitive(Boolean [] 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 b = array[i]; 2711 result[i] = (b == null ? valueForNull : b.booleanValue()); 2712 } 2713 return result; 2714 } 2715 2716 2724 public static Boolean [] 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 [] result = new Boolean [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 2745 public static boolean isEmpty(Object [] array) { 2746 if (array == null || array.length == 0) { 2747 return true; 2748 } 2749 return false; 2750 } 2751 2752 2759 public static boolean isEmpty(long[] array) { 2760 if (array == null || array.length == 0) { 2761 return true; 2762 } 2763 return false; 2764 } 2765 2766 2773 public static boolean isEmpty(int[] array) { 2774 if (array == null || array.length == 0) { 2775 return true; 2776 } 2777 return false; 2778 } 2779 2780 2787 public static boolean isEmpty(short[] array) { 2788 if (array == null || array.length == 0) { 2789 return true; 2790 } 2791 return false; 2792 } 2793 2794 2801 public static boolean isEmpty(char[] array) { 2802 if (array == null || array.length == 0) { 2803 return true; 2804 } 2805 return false; 2806 } 2807 2808 2815 public static boolean isEmpty(byte[] array) { 2816 if (array == null || array.length == 0) { 2817 return true; 2818 } 2819 return false; 2820 } 2821 2822 2829 public static boolean isEmpty(double[] array) { 2830 if (array == null || array.length == 0) { 2831 return true; 2832 } 2833 return false; 2834 } 2835 2836 2843 public static boolean isEmpty(float[] array) { 2844 if (array == null || array.length == 0) { 2845 return true; 2846 } 2847 return false; 2848 } 2849 2850 2857 public static boolean isEmpty(boolean[] array) { 2858 if (array == null || array.length == 0) { 2859 return true; 2860 } 2861 return false; 2862 } 2863 2864 2885 public static Object [] addAll(Object [] array1, Object [] array2) { 2886 if (array1 == null) { 2887 return clone(array2); 2888 } else if (array2 == null) { 2889 return clone(array1); 2890 } 2891 Object [] joinedArray = (Object []) 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 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 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 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 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 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 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 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 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 3153 public static Object [] add(Object [] array, Object element) { 3154 Class type = (array != null ? array.getClass() : (element != null ? element.getClass() : Object .class)); 3155 Object [] newArray = (Object []) copyArrayGrow1(array, type); 3156 newArray[newArray.length - 1] = element; 3157 return newArray; 3158 } 3159 3160 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 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 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 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 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 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 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 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 3385 private static Object copyArrayGrow1(Object array, Class newArrayComponentType) { 3386 if (array != null) { 3387 int arrayLength = Array.getLength(array); 3388 Object 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 3424 public static Object [] add(Object [] array, int index, Object element) { 3425 Class 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 [] { null }; 3433 } 3434 return (Object []) add( array, index, element, clss ); 3435 } 3436 3437 3464 public static boolean[] add(boolean[] array, int index, boolean element) { 3465 return (boolean[]) add( array, index, new Boolean (element), Boolean.TYPE ); 3466 } 3467 3468 3496 public static char[] add(char[] array, int index, char element) { 3497 return (char[]) add( array, index, new Character (element), Character.TYPE ); 3498 } 3499 3500 3527 public static byte[] add(byte[] array, int index, byte element) { 3528 return (byte[]) add( array, index, new Byte (element), Byte.TYPE ); 3529 } 3530 3531 3558 public static short[] add(short[] array, int index, short element) { 3559 return (short[]) add( array, index, new Short (element), Short.TYPE ); 3560 } 3561 3562 3589 public static int[] add(int[] array, int index, int element) { 3590 return (int[]) add( array, index, new Integer (element), Integer.TYPE ); 3591 } 3592 3593 3620 public static long[] add(long[] array, int index, long element) { 3621 return (long[]) add( array, index, new Long (element), Long.TYPE ); 3622 } 3623 3624 3651 public static float[] add(float[] array, int index, float element) { 3652 return (float[]) add( array, index, new Float (element), Float.TYPE ); 3653 } 3654 3655 3682 public static double[] add(double[] array, int index, double element) { 3683 return (double[]) add( array, index, new Double (element), Double.TYPE ); 3684 } 3685 3686 3697 private static Object add(Object array, int index, Object element, Class clss) { 3698 if (array == null) { 3699 if (index != 0) { 3700 throw new IndexOutOfBoundsException ("Index: " + index + ", Length: 0"); 3701 } 3702 Object 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 ("Index: " + index + ", Length: " + length); 3709 } 3710 Object 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 3747 public static Object [] remove(Object [] array, int index) { 3748 return (Object []) remove((Object ) array, index); 3749 } 3750 3751 3776 public static Object [] removeElement(Object [] array, Object element) { 3777 int index = indexOf(array, element); 3778 if (index == -1) { 3779 return clone(array); 3780 } 3781 return remove(array, index); 3782 } 3783 3784 3812 public static boolean[] remove(boolean[] array, int index) { 3813 return (boolean[]) remove((Object ) array, index); 3814 } 3815 3816 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 3877 public static byte[] remove(byte[] array, int index) { 3878 return (byte[]) remove((Object ) array, index); 3879 } 3880 3881 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 3942 public static char[] remove(char[] array, int index) { 3943 return (char[]) remove((Object ) array, index); 3944 } 3945 3946 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 4007 public static double[] remove(double[] array, int index) { 4008 return (double[]) remove((Object ) array, index); 4009 } 4010 4011 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 4072 public static float[] remove(float[] array, int index) { 4073 return (float[]) remove((Object ) array, index); 4074 } 4075 4076 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 4137 public static int[] remove(int[] array, int index) { 4138 return (int[]) remove((Object ) array, index); 4139 } 4140 4141 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 4202 public static long[] remove(long[] array, int index) { 4203 return (long[]) remove((Object ) array, index); 4204 } 4205 4206 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 4267 public static short[] remove(short[] array, int index) { 4268 return (short[]) remove((Object ) array, index); 4269 } 4270 4271 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 4325 private static Object remove(Object array, int index) { 4326 int length = getLength(array); 4327 if (index < 0 || index >= length) { 4328 throw new IndexOutOfBoundsException ("Index: " + index + ", Length: " + length); 4329 } 4330 4331 Object 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 |