PageRenderTime 32ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 1ms

/org.apache.commons.lang/source-bundle/org/apache/commons/lang/ArrayUtils.java

https://github.com/tomsontom/emf-databinding-example
Java | 4416 lines | 1583 code | 193 blank | 2640 comment | 655 complexity | 15e37024df5cb4109f45a0ea93273c82 MD5 | raw file
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. */
  17. package org.apache.commons.lang;
  18. import java.lang.reflect.Array;
  19. import java.util.HashMap;
  20. import java.util.Map;
  21. import org.apache.commons.lang.builder.EqualsBuilder;
  22. import org.apache.commons.lang.builder.HashCodeBuilder;
  23. import org.apache.commons.lang.builder.ToStringBuilder;
  24. import org.apache.commons.lang.builder.ToStringStyle;
  25. /**
  26. * <p>Operations on arrays, primitive arrays (like <code>int[]</code>) and
  27. * primitive wrapper arrays (like <code>Integer[]</code>).</p>
  28. *
  29. * <p>This class tries to handle <code>null</code> input gracefully.
  30. * An exception will not be thrown for a <code>null</code>
  31. * array input. However, an Object array that contains a <code>null</code>
  32. * element may throw an exception. Each method documents its behaviour.</p>
  33. *
  34. * @author Stephen Colebourne
  35. * @author Moritz Petersen
  36. * @author <a href="mailto:fredrik@westermarck.com">Fredrik Westermarck</a>
  37. * @author Nikolay Metchev
  38. * @author Matthew Hawthorne
  39. * @author Tim O'Brien
  40. * @author Pete Gieser
  41. * @author Gary Gregory
  42. * @author <a href="mailto:equinus100@hotmail.com">Ashwin S</a>
  43. * @author Maarten Coene
  44. * @since 2.0
  45. * @version $Id: ArrayUtils.java 632503 2008-03-01 00:21:52Z ggregory $
  46. */
  47. public class ArrayUtils {
  48. /**
  49. * An empty immutable <code>Object</code> array.
  50. */
  51. public static final Object[] EMPTY_OBJECT_ARRAY = new Object[0];
  52. /**
  53. * An empty immutable <code>Class</code> array.
  54. */
  55. public static final Class[] EMPTY_CLASS_ARRAY = new Class[0];
  56. /**
  57. * An empty immutable <code>String</code> array.
  58. */
  59. public static final String[] EMPTY_STRING_ARRAY = new String[0];
  60. /**
  61. * An empty immutable <code>long</code> array.
  62. */
  63. public static final long[] EMPTY_LONG_ARRAY = new long[0];
  64. /**
  65. * An empty immutable <code>Long</code> array.
  66. */
  67. public static final Long[] EMPTY_LONG_OBJECT_ARRAY = new Long[0];
  68. /**
  69. * An empty immutable <code>int</code> array.
  70. */
  71. public static final int[] EMPTY_INT_ARRAY = new int[0];
  72. /**
  73. * An empty immutable <code>Integer</code> array.
  74. */
  75. public static final Integer[] EMPTY_INTEGER_OBJECT_ARRAY = new Integer[0];
  76. /**
  77. * An empty immutable <code>short</code> array.
  78. */
  79. public static final short[] EMPTY_SHORT_ARRAY = new short[0];
  80. /**
  81. * An empty immutable <code>Short</code> array.
  82. */
  83. public static final Short[] EMPTY_SHORT_OBJECT_ARRAY = new Short[0];
  84. /**
  85. * An empty immutable <code>byte</code> array.
  86. */
  87. public static final byte[] EMPTY_BYTE_ARRAY = new byte[0];
  88. /**
  89. * An empty immutable <code>Byte</code> array.
  90. */
  91. public static final Byte[] EMPTY_BYTE_OBJECT_ARRAY = new Byte[0];
  92. /**
  93. * An empty immutable <code>double</code> array.
  94. */
  95. public static final double[] EMPTY_DOUBLE_ARRAY = new double[0];
  96. /**
  97. * An empty immutable <code>Double</code> array.
  98. */
  99. public static final Double[] EMPTY_DOUBLE_OBJECT_ARRAY = new Double[0];
  100. /**
  101. * An empty immutable <code>float</code> array.
  102. */
  103. public static final float[] EMPTY_FLOAT_ARRAY = new float[0];
  104. /**
  105. * An empty immutable <code>Float</code> array.
  106. */
  107. public static final Float[] EMPTY_FLOAT_OBJECT_ARRAY = new Float[0];
  108. /**
  109. * An empty immutable <code>boolean</code> array.
  110. */
  111. public static final boolean[] EMPTY_BOOLEAN_ARRAY = new boolean[0];
  112. /**
  113. * An empty immutable <code>Boolean</code> array.
  114. */
  115. public static final Boolean[] EMPTY_BOOLEAN_OBJECT_ARRAY = new Boolean[0];
  116. /**
  117. * An empty immutable <code>char</code> array.
  118. */
  119. public static final char[] EMPTY_CHAR_ARRAY = new char[0];
  120. /**
  121. * An empty immutable <code>Character</code> array.
  122. */
  123. public static final Character[] EMPTY_CHARACTER_OBJECT_ARRAY = new Character[0];
  124. /**
  125. * The index value when an element is not found in a list or array: <code>-1</code>.
  126. * This value is returned by methods in this class and can also be used in comparisons with values returned by
  127. * various method from {@link java.util.List}.
  128. */
  129. public static final int INDEX_NOT_FOUND = -1;
  130. /**
  131. * <p>ArrayUtils instances should NOT be constructed in standard programming.
  132. * Instead, the class should be used as <code>ArrayUtils.clone(new int[] {2})</code>.</p>
  133. *
  134. * <p>This constructor is public to permit tools that require a JavaBean instance
  135. * to operate.</p>
  136. */
  137. public ArrayUtils() {
  138. super();
  139. }
  140. // Basic methods handling multi-dimensional arrays
  141. //-----------------------------------------------------------------------
  142. /**
  143. * <p>Outputs an array as a String, treating <code>null</code> as an empty array.</p>
  144. *
  145. * <p>Multi-dimensional arrays are handled correctly, including
  146. * multi-dimensional primitive arrays.</p>
  147. *
  148. * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
  149. *
  150. * @param array the array to get a toString for, may be <code>null</code>
  151. * @return a String representation of the array, '{}' if null array input
  152. */
  153. public static String toString(Object array) {
  154. return toString(array, "{}");
  155. }
  156. /**
  157. * <p>Outputs an array as a String handling <code>null</code>s.</p>
  158. *
  159. * <p>Multi-dimensional arrays are handled correctly, including
  160. * multi-dimensional primitive arrays.</p>
  161. *
  162. * <p>The format is that of Java source code, for example <code>{a,b}</code>.</p>
  163. *
  164. * @param array the array to get a toString for, may be <code>null</code>
  165. * @param stringIfNull the String to return if the array is <code>null</code>
  166. * @return a String representation of the array
  167. */
  168. public static String toString(Object array, String stringIfNull) {
  169. if (array == null) {
  170. return stringIfNull;
  171. }
  172. return new ToStringBuilder(array, ToStringStyle.SIMPLE_STYLE).append(array).toString();
  173. }
  174. /**
  175. * <p>Get a hashCode for an array handling multi-dimensional arrays correctly.</p>
  176. *
  177. * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
  178. *
  179. * @param array the array to get a hashCode for, may be <code>null</code>
  180. * @return a hashCode for the array, zero if null array input
  181. */
  182. public static int hashCode(Object array) {
  183. return new HashCodeBuilder().append(array).toHashCode();
  184. }
  185. /**
  186. * <p>Compares two arrays, using equals(), handling multi-dimensional arrays
  187. * correctly.</p>
  188. *
  189. * <p>Multi-dimensional primitive arrays are also handled correctly by this method.</p>
  190. *
  191. * @param array1 the left hand array to compare, may be <code>null</code>
  192. * @param array2 the right hand array to compare, may be <code>null</code>
  193. * @return <code>true</code> if the arrays are equal
  194. */
  195. public static boolean isEquals(Object array1, Object array2) {
  196. return new EqualsBuilder().append(array1, array2).isEquals();
  197. }
  198. // To map
  199. //-----------------------------------------------------------------------
  200. /**
  201. * <p>Converts the given array into a {@link java.util.Map}. Each element of the array
  202. * must be either a {@link java.util.Map.Entry} or an Array, containing at least two
  203. * elements, where the first element is used as key and the second as
  204. * value.</p>
  205. *
  206. * <p>This method can be used to initialize:</p>
  207. * <pre>
  208. * // Create a Map mapping colors.
  209. * Map colorMap = MapUtils.toMap(new String[][] {{
  210. * {"RED", "#FF0000"},
  211. * {"GREEN", "#00FF00"},
  212. * {"BLUE", "#0000FF"}});
  213. * </pre>
  214. *
  215. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  216. *
  217. * @param array an array whose elements are either a {@link java.util.Map.Entry} or
  218. * an Array containing at least two elements, may be <code>null</code>
  219. * @return a <code>Map</code> that was created from the array
  220. * @throws IllegalArgumentException if one element of this Array is
  221. * itself an Array containing less then two elements
  222. * @throws IllegalArgumentException if the array contains elements other
  223. * than {@link java.util.Map.Entry} and an Array
  224. */
  225. public static Map toMap(Object[] array) {
  226. if (array == null) {
  227. return null;
  228. }
  229. final Map map = new HashMap((int) (array.length * 1.5));
  230. for (int i = 0; i < array.length; i++) {
  231. Object object = array[i];
  232. if (object instanceof Map.Entry) {
  233. Map.Entry entry = (Map.Entry) object;
  234. map.put(entry.getKey(), entry.getValue());
  235. } else if (object instanceof Object[]) {
  236. Object[] entry = (Object[]) object;
  237. if (entry.length < 2) {
  238. throw new IllegalArgumentException("Array element " + i + ", '"
  239. + object
  240. + "', has a length less than 2");
  241. }
  242. map.put(entry[0], entry[1]);
  243. } else {
  244. throw new IllegalArgumentException("Array element " + i + ", '"
  245. + object
  246. + "', is neither of type Map.Entry nor an Array");
  247. }
  248. }
  249. return map;
  250. }
  251. // Clone
  252. //-----------------------------------------------------------------------
  253. /**
  254. * <p>Shallow clones an array returning a typecast result and handling
  255. * <code>null</code>.</p>
  256. *
  257. * <p>The objects in the array are not cloned, thus there is no special
  258. * handling for multi-dimensional arrays.</p>
  259. *
  260. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  261. *
  262. * @param array the array to shallow clone, may be <code>null</code>
  263. * @return the cloned array, <code>null</code> if <code>null</code> input
  264. */
  265. public static Object[] clone(Object[] array) {
  266. if (array == null) {
  267. return null;
  268. }
  269. return (Object[]) array.clone();
  270. }
  271. /**
  272. * <p>Clones an array returning a typecast result and handling
  273. * <code>null</code>.</p>
  274. *
  275. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  276. *
  277. * @param array the array to clone, may be <code>null</code>
  278. * @return the cloned array, <code>null</code> if <code>null</code> input
  279. */
  280. public static long[] clone(long[] array) {
  281. if (array == null) {
  282. return null;
  283. }
  284. return (long[]) array.clone();
  285. }
  286. /**
  287. * <p>Clones an array returning a typecast result and handling
  288. * <code>null</code>.</p>
  289. *
  290. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  291. *
  292. * @param array the array to clone, may be <code>null</code>
  293. * @return the cloned array, <code>null</code> if <code>null</code> input
  294. */
  295. public static int[] clone(int[] array) {
  296. if (array == null) {
  297. return null;
  298. }
  299. return (int[]) array.clone();
  300. }
  301. /**
  302. * <p>Clones an array returning a typecast result and handling
  303. * <code>null</code>.</p>
  304. *
  305. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  306. *
  307. * @param array the array to clone, may be <code>null</code>
  308. * @return the cloned array, <code>null</code> if <code>null</code> input
  309. */
  310. public static short[] clone(short[] array) {
  311. if (array == null) {
  312. return null;
  313. }
  314. return (short[]) array.clone();
  315. }
  316. /**
  317. * <p>Clones an array returning a typecast result and handling
  318. * <code>null</code>.</p>
  319. *
  320. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  321. *
  322. * @param array the array to clone, may be <code>null</code>
  323. * @return the cloned array, <code>null</code> if <code>null</code> input
  324. */
  325. public static char[] clone(char[] array) {
  326. if (array == null) {
  327. return null;
  328. }
  329. return (char[]) array.clone();
  330. }
  331. /**
  332. * <p>Clones an array returning a typecast result and handling
  333. * <code>null</code>.</p>
  334. *
  335. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  336. *
  337. * @param array the array to clone, may be <code>null</code>
  338. * @return the cloned array, <code>null</code> if <code>null</code> input
  339. */
  340. public static byte[] clone(byte[] array) {
  341. if (array == null) {
  342. return null;
  343. }
  344. return (byte[]) array.clone();
  345. }
  346. /**
  347. * <p>Clones an array returning a typecast result and handling
  348. * <code>null</code>.</p>
  349. *
  350. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  351. *
  352. * @param array the array to clone, may be <code>null</code>
  353. * @return the cloned array, <code>null</code> if <code>null</code> input
  354. */
  355. public static double[] clone(double[] array) {
  356. if (array == null) {
  357. return null;
  358. }
  359. return (double[]) array.clone();
  360. }
  361. /**
  362. * <p>Clones an array returning a typecast result and handling
  363. * <code>null</code>.</p>
  364. *
  365. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  366. *
  367. * @param array the array to clone, may be <code>null</code>
  368. * @return the cloned array, <code>null</code> if <code>null</code> input
  369. */
  370. public static float[] clone(float[] array) {
  371. if (array == null) {
  372. return null;
  373. }
  374. return (float[]) array.clone();
  375. }
  376. /**
  377. * <p>Clones an array returning a typecast result and handling
  378. * <code>null</code>.</p>
  379. *
  380. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  381. *
  382. * @param array the array to clone, may be <code>null</code>
  383. * @return the cloned array, <code>null</code> if <code>null</code> input
  384. */
  385. public static boolean[] clone(boolean[] array) {
  386. if (array == null) {
  387. return null;
  388. }
  389. return (boolean[]) array.clone();
  390. }
  391. // Subarrays
  392. //-----------------------------------------------------------------------
  393. /**
  394. * <p>Produces a new array containing the elements between
  395. * the start and end indices.</p>
  396. *
  397. * <p>The start index is inclusive, the end index exclusive.
  398. * Null array input produces null output.</p>
  399. *
  400. * <p>The component type of the subarray is always the same as
  401. * that of the input array. Thus, if the input is an array of type
  402. * <code>Date</code>, the following usage is envisaged:</p>
  403. *
  404. * <pre>
  405. * Date[] someDates = (Date[])ArrayUtils.subarray(allDates, 2, 5);
  406. * </pre>
  407. *
  408. * @param array the array
  409. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  410. * is promoted to 0, overvalue (&gt;array.length) results
  411. * in an empty array.
  412. * @param endIndexExclusive elements up to endIndex-1 are present in the
  413. * returned subarray. Undervalue (&lt; startIndex) produces
  414. * empty array, overvalue (&gt;array.length) is demoted to
  415. * array length.
  416. * @return a new array containing the elements between
  417. * the start and end indices.
  418. * @since 2.1
  419. */
  420. public static Object[] subarray(Object[] array, int startIndexInclusive, int endIndexExclusive) {
  421. if (array == null) {
  422. return null;
  423. }
  424. if (startIndexInclusive < 0) {
  425. startIndexInclusive = 0;
  426. }
  427. if (endIndexExclusive > array.length) {
  428. endIndexExclusive = array.length;
  429. }
  430. int newSize = endIndexExclusive - startIndexInclusive;
  431. Class type = array.getClass().getComponentType();
  432. if (newSize <= 0) {
  433. return (Object[]) Array.newInstance(type, 0);
  434. }
  435. Object[] subarray = (Object[]) Array.newInstance(type, newSize);
  436. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  437. return subarray;
  438. }
  439. /**
  440. * <p>Produces a new <code>long</code> array containing the elements
  441. * between the start and end indices.</p>
  442. *
  443. * <p>The start index is inclusive, the end index exclusive.
  444. * Null array input produces null output.</p>
  445. *
  446. * @param array the array
  447. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  448. * is promoted to 0, overvalue (&gt;array.length) results
  449. * in an empty array.
  450. * @param endIndexExclusive elements up to endIndex-1 are present in the
  451. * returned subarray. Undervalue (&lt; startIndex) produces
  452. * empty array, overvalue (&gt;array.length) is demoted to
  453. * array length.
  454. * @return a new array containing the elements between
  455. * the start and end indices.
  456. * @since 2.1
  457. */
  458. public static long[] subarray(long[] array, int startIndexInclusive, int endIndexExclusive) {
  459. if (array == null) {
  460. return null;
  461. }
  462. if (startIndexInclusive < 0) {
  463. startIndexInclusive = 0;
  464. }
  465. if (endIndexExclusive > array.length) {
  466. endIndexExclusive = array.length;
  467. }
  468. int newSize = endIndexExclusive - startIndexInclusive;
  469. if (newSize <= 0) {
  470. return EMPTY_LONG_ARRAY;
  471. }
  472. long[] subarray = new long[newSize];
  473. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  474. return subarray;
  475. }
  476. /**
  477. * <p>Produces a new <code>int</code> array containing the elements
  478. * between the start and end indices.</p>
  479. *
  480. * <p>The start index is inclusive, the end index exclusive.
  481. * Null array input produces null output.</p>
  482. *
  483. * @param array the array
  484. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  485. * is promoted to 0, overvalue (&gt;array.length) results
  486. * in an empty array.
  487. * @param endIndexExclusive elements up to endIndex-1 are present in the
  488. * returned subarray. Undervalue (&lt; startIndex) produces
  489. * empty array, overvalue (&gt;array.length) is demoted to
  490. * array length.
  491. * @return a new array containing the elements between
  492. * the start and end indices.
  493. * @since 2.1
  494. */
  495. public static int[] subarray(int[] array, int startIndexInclusive, int endIndexExclusive) {
  496. if (array == null) {
  497. return null;
  498. }
  499. if (startIndexInclusive < 0) {
  500. startIndexInclusive = 0;
  501. }
  502. if (endIndexExclusive > array.length) {
  503. endIndexExclusive = array.length;
  504. }
  505. int newSize = endIndexExclusive - startIndexInclusive;
  506. if (newSize <= 0) {
  507. return EMPTY_INT_ARRAY;
  508. }
  509. int[] subarray = new int[newSize];
  510. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  511. return subarray;
  512. }
  513. /**
  514. * <p>Produces a new <code>short</code> array containing the elements
  515. * between the start and end indices.</p>
  516. *
  517. * <p>The start index is inclusive, the end index exclusive.
  518. * Null array input produces null output.</p>
  519. *
  520. * @param array the array
  521. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  522. * is promoted to 0, overvalue (&gt;array.length) results
  523. * in an empty array.
  524. * @param endIndexExclusive elements up to endIndex-1 are present in the
  525. * returned subarray. Undervalue (&lt; startIndex) produces
  526. * empty array, overvalue (&gt;array.length) is demoted to
  527. * array length.
  528. * @return a new array containing the elements between
  529. * the start and end indices.
  530. * @since 2.1
  531. */
  532. public static short[] subarray(short[] array, int startIndexInclusive, int endIndexExclusive) {
  533. if (array == null) {
  534. return null;
  535. }
  536. if (startIndexInclusive < 0) {
  537. startIndexInclusive = 0;
  538. }
  539. if (endIndexExclusive > array.length) {
  540. endIndexExclusive = array.length;
  541. }
  542. int newSize = endIndexExclusive - startIndexInclusive;
  543. if (newSize <= 0) {
  544. return EMPTY_SHORT_ARRAY;
  545. }
  546. short[] subarray = new short[newSize];
  547. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  548. return subarray;
  549. }
  550. /**
  551. * <p>Produces a new <code>char</code> array containing the elements
  552. * between the start and end indices.</p>
  553. *
  554. * <p>The start index is inclusive, the end index exclusive.
  555. * Null array input produces null output.</p>
  556. *
  557. * @param array the array
  558. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  559. * is promoted to 0, overvalue (&gt;array.length) results
  560. * in an empty array.
  561. * @param endIndexExclusive elements up to endIndex-1 are present in the
  562. * returned subarray. Undervalue (&lt; startIndex) produces
  563. * empty array, overvalue (&gt;array.length) is demoted to
  564. * array length.
  565. * @return a new array containing the elements between
  566. * the start and end indices.
  567. * @since 2.1
  568. */
  569. public static char[] subarray(char[] array, int startIndexInclusive, int endIndexExclusive) {
  570. if (array == null) {
  571. return null;
  572. }
  573. if (startIndexInclusive < 0) {
  574. startIndexInclusive = 0;
  575. }
  576. if (endIndexExclusive > array.length) {
  577. endIndexExclusive = array.length;
  578. }
  579. int newSize = endIndexExclusive - startIndexInclusive;
  580. if (newSize <= 0) {
  581. return EMPTY_CHAR_ARRAY;
  582. }
  583. char[] subarray = new char[newSize];
  584. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  585. return subarray;
  586. }
  587. /**
  588. * <p>Produces a new <code>byte</code> array containing the elements
  589. * between the start and end indices.</p>
  590. *
  591. * <p>The start index is inclusive, the end index exclusive.
  592. * Null array input produces null output.</p>
  593. *
  594. * @param array the array
  595. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  596. * is promoted to 0, overvalue (&gt;array.length) results
  597. * in an empty array.
  598. * @param endIndexExclusive elements up to endIndex-1 are present in the
  599. * returned subarray. Undervalue (&lt; startIndex) produces
  600. * empty array, overvalue (&gt;array.length) is demoted to
  601. * array length.
  602. * @return a new array containing the elements between
  603. * the start and end indices.
  604. * @since 2.1
  605. */
  606. public static byte[] subarray(byte[] array, int startIndexInclusive, int endIndexExclusive) {
  607. if (array == null) {
  608. return null;
  609. }
  610. if (startIndexInclusive < 0) {
  611. startIndexInclusive = 0;
  612. }
  613. if (endIndexExclusive > array.length) {
  614. endIndexExclusive = array.length;
  615. }
  616. int newSize = endIndexExclusive - startIndexInclusive;
  617. if (newSize <= 0) {
  618. return EMPTY_BYTE_ARRAY;
  619. }
  620. byte[] subarray = new byte[newSize];
  621. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  622. return subarray;
  623. }
  624. /**
  625. * <p>Produces a new <code>double</code> array containing the elements
  626. * between the start and end indices.</p>
  627. *
  628. * <p>The start index is inclusive, the end index exclusive.
  629. * Null array input produces null output.</p>
  630. *
  631. * @param array the array
  632. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  633. * is promoted to 0, overvalue (&gt;array.length) results
  634. * in an empty array.
  635. * @param endIndexExclusive elements up to endIndex-1 are present in the
  636. * returned subarray. Undervalue (&lt; startIndex) produces
  637. * empty array, overvalue (&gt;array.length) is demoted to
  638. * array length.
  639. * @return a new array containing the elements between
  640. * the start and end indices.
  641. * @since 2.1
  642. */
  643. public static double[] subarray(double[] array, int startIndexInclusive, int endIndexExclusive) {
  644. if (array == null) {
  645. return null;
  646. }
  647. if (startIndexInclusive < 0) {
  648. startIndexInclusive = 0;
  649. }
  650. if (endIndexExclusive > array.length) {
  651. endIndexExclusive = array.length;
  652. }
  653. int newSize = endIndexExclusive - startIndexInclusive;
  654. if (newSize <= 0) {
  655. return EMPTY_DOUBLE_ARRAY;
  656. }
  657. double[] subarray = new double[newSize];
  658. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  659. return subarray;
  660. }
  661. /**
  662. * <p>Produces a new <code>float</code> array containing the elements
  663. * between the start and end indices.</p>
  664. *
  665. * <p>The start index is inclusive, the end index exclusive.
  666. * Null array input produces null output.</p>
  667. *
  668. * @param array the array
  669. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  670. * is promoted to 0, overvalue (&gt;array.length) results
  671. * in an empty array.
  672. * @param endIndexExclusive elements up to endIndex-1 are present in the
  673. * returned subarray. Undervalue (&lt; startIndex) produces
  674. * empty array, overvalue (&gt;array.length) is demoted to
  675. * array length.
  676. * @return a new array containing the elements between
  677. * the start and end indices.
  678. * @since 2.1
  679. */
  680. public static float[] subarray(float[] array, int startIndexInclusive, int endIndexExclusive) {
  681. if (array == null) {
  682. return null;
  683. }
  684. if (startIndexInclusive < 0) {
  685. startIndexInclusive = 0;
  686. }
  687. if (endIndexExclusive > array.length) {
  688. endIndexExclusive = array.length;
  689. }
  690. int newSize = endIndexExclusive - startIndexInclusive;
  691. if (newSize <= 0) {
  692. return EMPTY_FLOAT_ARRAY;
  693. }
  694. float[] subarray = new float[newSize];
  695. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  696. return subarray;
  697. }
  698. /**
  699. * <p>Produces a new <code>boolean</code> array containing the elements
  700. * between the start and end indices.</p>
  701. *
  702. * <p>The start index is inclusive, the end index exclusive.
  703. * Null array input produces null output.</p>
  704. *
  705. * @param array the array
  706. * @param startIndexInclusive the starting index. Undervalue (&lt;0)
  707. * is promoted to 0, overvalue (&gt;array.length) results
  708. * in an empty array.
  709. * @param endIndexExclusive elements up to endIndex-1 are present in the
  710. * returned subarray. Undervalue (&lt; startIndex) produces
  711. * empty array, overvalue (&gt;array.length) is demoted to
  712. * array length.
  713. * @return a new array containing the elements between
  714. * the start and end indices.
  715. * @since 2.1
  716. */
  717. public static boolean[] subarray(boolean[] array, int startIndexInclusive, int endIndexExclusive) {
  718. if (array == null) {
  719. return null;
  720. }
  721. if (startIndexInclusive < 0) {
  722. startIndexInclusive = 0;
  723. }
  724. if (endIndexExclusive > array.length) {
  725. endIndexExclusive = array.length;
  726. }
  727. int newSize = endIndexExclusive - startIndexInclusive;
  728. if (newSize <= 0) {
  729. return EMPTY_BOOLEAN_ARRAY;
  730. }
  731. boolean[] subarray = new boolean[newSize];
  732. System.arraycopy(array, startIndexInclusive, subarray, 0, newSize);
  733. return subarray;
  734. }
  735. // Is same length
  736. //-----------------------------------------------------------------------
  737. /**
  738. * <p>Checks whether two arrays are the same length, treating
  739. * <code>null</code> arrays as length <code>0</code>.
  740. *
  741. * <p>Any multi-dimensional aspects of the arrays are ignored.</p>
  742. *
  743. * @param array1 the first array, may be <code>null</code>
  744. * @param array2 the second array, may be <code>null</code>
  745. * @return <code>true</code> if length of arrays matches, treating
  746. * <code>null</code> as an empty array
  747. */
  748. public static boolean isSameLength(Object[] array1, Object[] array2) {
  749. if ((array1 == null && array2 != null && array2.length > 0) ||
  750. (array2 == null && array1 != null && array1.length > 0) ||
  751. (array1 != null && array2 != null && array1.length != array2.length)) {
  752. return false;
  753. }
  754. return true;
  755. }
  756. /**
  757. * <p>Checks whether two arrays are the same length, treating
  758. * <code>null</code> arrays as length <code>0</code>.</p>
  759. *
  760. * @param array1 the first array, may be <code>null</code>
  761. * @param array2 the second array, may be <code>null</code>
  762. * @return <code>true</code> if length of arrays matches, treating
  763. * <code>null</code> as an empty array
  764. */
  765. public static boolean isSameLength(long[] array1, long[] array2) {
  766. if ((array1 == null && array2 != null && array2.length > 0) ||
  767. (array2 == null && array1 != null && array1.length > 0) ||
  768. (array1 != null && array2 != null && array1.length != array2.length)) {
  769. return false;
  770. }
  771. return true;
  772. }
  773. /**
  774. * <p>Checks whether two arrays are the same length, treating
  775. * <code>null</code> arrays as length <code>0</code>.</p>
  776. *
  777. * @param array1 the first array, may be <code>null</code>
  778. * @param array2 the second array, may be <code>null</code>
  779. * @return <code>true</code> if length of arrays matches, treating
  780. * <code>null</code> as an empty array
  781. */
  782. public static boolean isSameLength(int[] array1, int[] array2) {
  783. if ((array1 == null && array2 != null && array2.length > 0) ||
  784. (array2 == null && array1 != null && array1.length > 0) ||
  785. (array1 != null && array2 != null && array1.length != array2.length)) {
  786. return false;
  787. }
  788. return true;
  789. }
  790. /**
  791. * <p>Checks whether two arrays are the same length, treating
  792. * <code>null</code> arrays as length <code>0</code>.</p>
  793. *
  794. * @param array1 the first array, may be <code>null</code>
  795. * @param array2 the second array, may be <code>null</code>
  796. * @return <code>true</code> if length of arrays matches, treating
  797. * <code>null</code> as an empty array
  798. */
  799. public static boolean isSameLength(short[] array1, short[] array2) {
  800. if ((array1 == null && array2 != null && array2.length > 0) ||
  801. (array2 == null && array1 != null && array1.length > 0) ||
  802. (array1 != null && array2 != null && array1.length != array2.length)) {
  803. return false;
  804. }
  805. return true;
  806. }
  807. /**
  808. * <p>Checks whether two arrays are the same length, treating
  809. * <code>null</code> arrays as length <code>0</code>.</p>
  810. *
  811. * @param array1 the first array, may be <code>null</code>
  812. * @param array2 the second array, may be <code>null</code>
  813. * @return <code>true</code> if length of arrays matches, treating
  814. * <code>null</code> as an empty array
  815. */
  816. public static boolean isSameLength(char[] array1, char[] array2) {
  817. if ((array1 == null && array2 != null && array2.length > 0) ||
  818. (array2 == null && array1 != null && array1.length > 0) ||
  819. (array1 != null && array2 != null && array1.length != array2.length)) {
  820. return false;
  821. }
  822. return true;
  823. }
  824. /**
  825. * <p>Checks whether two arrays are the same length, treating
  826. * <code>null</code> arrays as length <code>0</code>.</p>
  827. *
  828. * @param array1 the first array, may be <code>null</code>
  829. * @param array2 the second array, may be <code>null</code>
  830. * @return <code>true</code> if length of arrays matches, treating
  831. * <code>null</code> as an empty array
  832. */
  833. public static boolean isSameLength(byte[] array1, byte[] array2) {
  834. if ((array1 == null && array2 != null && array2.length > 0) ||
  835. (array2 == null && array1 != null && array1.length > 0) ||
  836. (array1 != null && array2 != null && array1.length != array2.length)) {
  837. return false;
  838. }
  839. return true;
  840. }
  841. /**
  842. * <p>Checks whether two arrays are the same length, treating
  843. * <code>null</code> arrays as length <code>0</code>.</p>
  844. *
  845. * @param array1 the first array, may be <code>null</code>
  846. * @param array2 the second array, may be <code>null</code>
  847. * @return <code>true</code> if length of arrays matches, treating
  848. * <code>null</code> as an empty array
  849. */
  850. public static boolean isSameLength(double[] array1, double[] array2) {
  851. if ((array1 == null && array2 != null && array2.length > 0) ||
  852. (array2 == null && array1 != null && array1.length > 0) ||
  853. (array1 != null && array2 != null && array1.length != array2.length)) {
  854. return false;
  855. }
  856. return true;
  857. }
  858. /**
  859. * <p>Checks whether two arrays are the same length, treating
  860. * <code>null</code> arrays as length <code>0</code>.</p>
  861. *
  862. * @param array1 the first array, may be <code>null</code>
  863. * @param array2 the second array, may be <code>null</code>
  864. * @return <code>true</code> if length of arrays matches, treating
  865. * <code>null</code> as an empty array
  866. */
  867. public static boolean isSameLength(float[] array1, float[] 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. * <p>Checks whether two arrays are the same length, treating
  877. * <code>null</code> arrays as length <code>0</code>.</p>
  878. *
  879. * @param array1 the first array, may be <code>null</code>
  880. * @param array2 the second array, may be <code>null</code>
  881. * @return <code>true</code> if length of arrays matches, treating
  882. * <code>null</code> as an empty array
  883. */
  884. public static boolean isSameLength(boolean[] array1, boolean[] array2) {
  885. if ((array1 == null && array2 != null && array2.length > 0) ||
  886. (array2 == null && array1 != null && array1.length > 0) ||
  887. (array1 != null && array2 != null && array1.length != array2.length)) {
  888. return false;
  889. }
  890. return true;
  891. }
  892. //-----------------------------------------------------------------------
  893. /**
  894. * <p>Returns the length of the specified array.
  895. * This method can deal with <code>Object</code> arrays and with primitive arrays.</p>
  896. *
  897. * <p>If the input array is <code>null</code>, <code>0</code> is returned.</p>
  898. *
  899. * <pre>
  900. * ArrayUtils.getLength(null) = 0
  901. * ArrayUtils.getLength([]) = 0
  902. * ArrayUtils.getLength([null]) = 1
  903. * ArrayUtils.getLength([true, false]) = 2
  904. * ArrayUtils.getLength([1, 2, 3]) = 3
  905. * ArrayUtils.getLength(["a", "b", "c"]) = 3
  906. * </pre>
  907. *
  908. * @param array the array to retrieve the length from, may be null
  909. * @return The length of the array, or <code>0</code> if the array is <code>null</code>
  910. * @throws IllegalArgumentException if the object arguement is not an array.
  911. * @since 2.1
  912. */
  913. public static int getLength(Object array) {
  914. if (array == null) {
  915. return 0;
  916. }
  917. return Array.getLength(array);
  918. }
  919. /**
  920. * <p>Checks whether two arrays are the same type taking into account
  921. * multi-dimensional arrays.</p>
  922. *
  923. * @param array1 the first array, must not be <code>null</code>
  924. * @param array2 the second array, must not be <code>null</code>
  925. * @return <code>true</code> if type of arrays matches
  926. * @throws IllegalArgumentException if either array is <code>null</code>
  927. */
  928. public static boolean isSameType(Object array1, Object array2) {
  929. if (array1 == null || array2 == null) {
  930. throw new IllegalArgumentException("The Array must not be null");
  931. }
  932. return array1.getClass().getName().equals(array2.getClass().getName());
  933. }
  934. // Reverse
  935. //-----------------------------------------------------------------------
  936. /**
  937. * <p>Reverses the order of the given array.</p>
  938. *
  939. * <p>There is no special handling for multi-dimensional arrays.</p>
  940. *
  941. * <p>This method does nothing for a <code>null</code> input array.</p>
  942. *
  943. * @param array the array to reverse, may be <code>null</code>
  944. */
  945. public static void reverse(Object[] array) {
  946. if (array == null) {
  947. return;
  948. }
  949. int i = 0;
  950. int j = array.length - 1;
  951. Object tmp;
  952. while (j > i) {
  953. tmp = array[j];
  954. array[j] = array[i];
  955. array[i] = tmp;
  956. j--;
  957. i++;
  958. }
  959. }
  960. /**
  961. * <p>Reverses the order of the given array.</p>
  962. *
  963. * <p>This method does nothing for a <code>null</code> input array.</p>
  964. *
  965. * @param array the array to reverse, may be <code>null</code>
  966. */
  967. public static void reverse(long[] array) {
  968. if (array == null) {
  969. return;
  970. }
  971. int i = 0;
  972. int j = array.length - 1;
  973. long tmp;
  974. while (j > i) {
  975. tmp = array[j];
  976. array[j] = array[i];
  977. array[i] = tmp;
  978. j--;
  979. i++;
  980. }
  981. }
  982. /**
  983. * <p>Reverses the order of the given array.</p>
  984. *
  985. * <p>This method does nothing for a <code>null</code> input array.</p>
  986. *
  987. * @param array the array to reverse, may be <code>null</code>
  988. */
  989. public static void reverse(int[] array) {
  990. if (array == null) {
  991. return;
  992. }
  993. int i = 0;
  994. int j = array.length - 1;
  995. int tmp;
  996. while (j > i) {
  997. tmp = array[j];
  998. array[j] = array[i];
  999. array[i] = tmp;
  1000. j--;
  1001. i++;
  1002. }
  1003. }
  1004. /**
  1005. * <p>Reverses the order of the given array.</p>
  1006. *
  1007. * <p>This method does nothing for a <code>null</code> input array.</p>
  1008. *
  1009. * @param array the array to reverse, may be <code>null</code>
  1010. */
  1011. public static void reverse(short[] array) {
  1012. if (array == null) {
  1013. return;
  1014. }
  1015. int i = 0;
  1016. int j = array.length - 1;
  1017. short tmp;
  1018. while (j > i) {
  1019. tmp = array[j];
  1020. array[j] = array[i];
  1021. array[i] = tmp;
  1022. j--;
  1023. i++;
  1024. }
  1025. }
  1026. /**
  1027. * <p>Reverses the order of the given array.</p>
  1028. *
  1029. * <p>This method does nothing for a <code>null</code> input array.</p>
  1030. *
  1031. * @param array the array to reverse, may be <code>null</code>
  1032. */
  1033. public static void reverse(char[] array) {
  1034. if (array == null) {
  1035. return;
  1036. }
  1037. int i = 0;
  1038. int j = array.length - 1;
  1039. char tmp;
  1040. while (j > i) {
  1041. tmp = array[j];
  1042. array[j] = array[i];
  1043. array[i] = tmp;
  1044. j--;
  1045. i++;
  1046. }
  1047. }
  1048. /**
  1049. * <p>Reverses the order of the given array.</p>
  1050. *
  1051. * <p>This method does nothing for a <code>null</code> input array.</p>
  1052. *
  1053. * @param array the array to reverse, may be <code>null</code>
  1054. */
  1055. public static void reverse(byte[] array) {
  1056. if (array == null) {
  1057. return;
  1058. }
  1059. int i = 0;
  1060. int j = array.length - 1;
  1061. byte 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. * <p>Reverses the order of the given array.</p>
  1072. *
  1073. * <p>This method does nothing for a <code>null</code> input array.</p>
  1074. *
  1075. * @param array the array to reverse, may be <code>null</code>
  1076. */
  1077. public static void reverse(double[] array) {
  1078. if (array == null) {
  1079. return;
  1080. }
  1081. int i = 0;
  1082. int j = array.length - 1;
  1083. double tmp;
  1084. while (j > i) {
  1085. tmp = array[j];
  1086. array[j] = array[i];
  1087. array[i] = tmp;
  1088. j--;
  1089. i++;
  1090. }
  1091. }
  1092. /**
  1093. * <p>Reverses the order of the given array.</p>
  1094. *
  1095. * <p>This method does nothing for a <code>null</code> input array.</p>
  1096. *
  1097. * @param array the array to reverse, may be <code>null</code>
  1098. */
  1099. public static void reverse(float[] array) {
  1100. if (array == null) {
  1101. return;
  1102. }
  1103. int i = 0;
  1104. int j = array.length - 1;
  1105. float tmp;
  1106. while (j > i) {
  1107. tmp = array[j];
  1108. array[j] = array[i];
  1109. array[i] = tmp;
  1110. j--;
  1111. i++;
  1112. }
  1113. }
  1114. /**
  1115. * <p>Reverses the order of the given array.</p>
  1116. *
  1117. * <p>This method does nothing for a <code>null</code> input array.</p>
  1118. *
  1119. * @param array the array to reverse, may be <code>null</code>
  1120. */
  1121. public static void reverse(boolean[] array) {
  1122. if (array == null) {
  1123. return;
  1124. }
  1125. int i = 0;
  1126. int j = array.length - 1;
  1127. boolean tmp;
  1128. while (j > i) {
  1129. tmp = array[j];
  1130. array[j] = array[i];
  1131. array[i] = tmp;
  1132. j--;
  1133. i++;
  1134. }
  1135. }
  1136. // IndexOf search
  1137. // ----------------------------------------------------------------------
  1138. // Object IndexOf
  1139. //-----------------------------------------------------------------------
  1140. /**
  1141. * <p>Finds the index of the given object in the array.</p>
  1142. *
  1143. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1144. *
  1145. * @param array the array to search through for the object, may be <code>null</code>
  1146. * @param objectToFind the object to find, may be <code>null</code>
  1147. * @return the index of the object within the array,
  1148. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1149. */
  1150. public static int indexOf(Object[] array, Object objectToFind) {
  1151. return indexOf(array, objectToFind, 0);
  1152. }
  1153. /**
  1154. * <p>Finds the index of the given object in the array starting at the given index.</p>
  1155. *
  1156. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1157. *
  1158. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1159. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1160. *
  1161. * @param array the array to search through for the object, may be <code>null</code>
  1162. * @param objectToFind the object to find, may be <code>null</code>
  1163. * @param startIndex the index to start searching at
  1164. * @return the index of the object within the array starting at the index,
  1165. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1166. */
  1167. public static int indexOf(Object[] array, Object objectToFind, int startIndex) {
  1168. if (array == null) {
  1169. return INDEX_NOT_FOUND;
  1170. }
  1171. if (startIndex < 0) {
  1172. startIndex = 0;
  1173. }
  1174. if (objectToFind == null) {
  1175. for (int i = startIndex; i < array.length; i++) {
  1176. if (array[i] == null) {
  1177. return i;
  1178. }
  1179. }
  1180. } else {
  1181. for (int i = startIndex; i < array.length; i++) {
  1182. if (objectToFind.equals(array[i])) {
  1183. return i;
  1184. }
  1185. }
  1186. }
  1187. return INDEX_NOT_FOUND;
  1188. }
  1189. /**
  1190. * <p>Finds the last index of the given object within the array.</p>
  1191. *
  1192. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1193. *
  1194. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1195. * @param objectToFind the object to find, may be <code>null</code>
  1196. * @return the last index of the object within the array,
  1197. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1198. */
  1199. public static int lastIndexOf(Object[] array, Object objectToFind) {
  1200. return lastIndexOf(array, objectToFind, Integer.MAX_VALUE);
  1201. }
  1202. /**
  1203. * <p>Finds the last index of the given object in the array starting at the given index.</p>
  1204. *
  1205. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1206. *
  1207. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than
  1208. * the array length will search from the end of the array.</p>
  1209. *
  1210. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1211. * @param objectToFind the object to find, may be <code>null</code>
  1212. * @param startIndex the start index to travers backwards from
  1213. * @return the last index of the object within the array,
  1214. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1215. */
  1216. public static int lastIndexOf(Object[] array, Object objectToFind, int startIndex) {
  1217. if (array == null) {
  1218. return INDEX_NOT_FOUND;
  1219. }
  1220. if (startIndex < 0) {
  1221. return INDEX_NOT_FOUND;
  1222. } else if (startIndex >= array.length) {
  1223. startIndex = array.length - 1;
  1224. }
  1225. if (objectToFind == null) {
  1226. for (int i = startIndex; i >= 0; i--) {
  1227. if (array[i] == null) {
  1228. return i;
  1229. }
  1230. }
  1231. } else {
  1232. for (int i = startIndex; i >= 0; i--) {
  1233. if (objectToFind.equals(array[i])) {
  1234. return i;
  1235. }
  1236. }
  1237. }
  1238. return INDEX_NOT_FOUND;
  1239. }
  1240. /**
  1241. * <p>Checks if the object is in the given array.</p>
  1242. *
  1243. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1244. *
  1245. * @param array the array to search through
  1246. * @param objectToFind the object to find
  1247. * @return <code>true</code> if the array contains the object
  1248. */
  1249. public static boolean contains(Object[] array, Object objectToFind) {
  1250. return indexOf(array, objectToFind) != INDEX_NOT_FOUND;
  1251. }
  1252. // long IndexOf
  1253. //-----------------------------------------------------------------------
  1254. /**
  1255. * <p>Finds the index of the given value in the array.</p>
  1256. *
  1257. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1258. *
  1259. * @param array the array to search through for the object, may be <code>null</code>
  1260. * @param valueToFind the value to find
  1261. * @return the index of the value within the array,
  1262. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1263. */
  1264. public static int indexOf(long[] array, long valueToFind) {
  1265. return indexOf(array, valueToFind, 0);
  1266. }
  1267. /**
  1268. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1269. *
  1270. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1271. *
  1272. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1273. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1274. *
  1275. * @param array the array to search through for the object, may be <code>null</code>
  1276. * @param valueToFind the value to find
  1277. * @param startIndex the index to start searching at
  1278. * @return the index of the value within the array,
  1279. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1280. */
  1281. public static int indexOf(long[] array, long valueToFind, int startIndex) {
  1282. if (array == null) {
  1283. return INDEX_NOT_FOUND;
  1284. }
  1285. if (startIndex < 0) {
  1286. startIndex = 0;
  1287. }
  1288. for (int i = startIndex; i < array.length; i++) {
  1289. if (valueToFind == array[i]) {
  1290. return i;
  1291. }
  1292. }
  1293. return INDEX_NOT_FOUND;
  1294. }
  1295. /**
  1296. * <p>Finds the last index of the given value within the array.</p>
  1297. *
  1298. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1299. *
  1300. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1301. * @param valueToFind the object to find
  1302. * @return the last index of the value within the array,
  1303. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1304. */
  1305. public static int lastIndexOf(long[] array, long valueToFind) {
  1306. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1307. }
  1308. /**
  1309. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  1310. *
  1311. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1312. *
  1313. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  1314. * array length will search from the end of the array.</p>
  1315. *
  1316. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1317. * @param valueToFind the value to find
  1318. * @param startIndex the start index to travers backwards from
  1319. * @return the last index of the value within the array,
  1320. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1321. */
  1322. public static int lastIndexOf(long[] array, long valueToFind, int startIndex) {
  1323. if (array == null) {
  1324. return INDEX_NOT_FOUND;
  1325. }
  1326. if (startIndex < 0) {
  1327. return INDEX_NOT_FOUND;
  1328. } else if (startIndex >= array.length) {
  1329. startIndex = array.length - 1;
  1330. }
  1331. for (int i = startIndex; i >= 0; i--) {
  1332. if (valueToFind == array[i]) {
  1333. return i;
  1334. }
  1335. }
  1336. return INDEX_NOT_FOUND;
  1337. }
  1338. /**
  1339. * <p>Checks if the value is in the given array.</p>
  1340. *
  1341. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1342. *
  1343. * @param array the array to search through
  1344. * @param valueToFind the value to find
  1345. * @return <code>true</code> if the array contains the object
  1346. */
  1347. public static boolean contains(long[] array, long valueToFind) {
  1348. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  1349. }
  1350. // int IndexOf
  1351. //-----------------------------------------------------------------------
  1352. /**
  1353. * <p>Finds the index of the given value in the array.</p>
  1354. *
  1355. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1356. *
  1357. * @param array the array to search through for the object, may be <code>null</code>
  1358. * @param valueToFind the value to find
  1359. * @return the index of the value within the array,
  1360. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1361. */
  1362. public static int indexOf(int[] array, int valueToFind) {
  1363. return indexOf(array, valueToFind, 0);
  1364. }
  1365. /**
  1366. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1367. *
  1368. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1369. *
  1370. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1371. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1372. *
  1373. * @param array the array to search through for the object, may be <code>null</code>
  1374. * @param valueToFind the value to find
  1375. * @param startIndex the index to start searching at
  1376. * @return the index of the value within the array,
  1377. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1378. */
  1379. public static int indexOf(int[] array, int valueToFind, int startIndex) {
  1380. if (array == null) {
  1381. return INDEX_NOT_FOUND;
  1382. }
  1383. if (startIndex < 0) {
  1384. startIndex = 0;
  1385. }
  1386. for (int i = startIndex; i < array.length; i++) {
  1387. if (valueToFind == array[i]) {
  1388. return i;
  1389. }
  1390. }
  1391. return INDEX_NOT_FOUND;
  1392. }
  1393. /**
  1394. * <p>Finds the last index of the given value within the array.</p>
  1395. *
  1396. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1397. *
  1398. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1399. * @param valueToFind the object to find
  1400. * @return the last index of the value within the array,
  1401. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1402. */
  1403. public static int lastIndexOf(int[] array, int valueToFind) {
  1404. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1405. }
  1406. /**
  1407. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  1408. *
  1409. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1410. *
  1411. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  1412. * array length will search from the end of the array.</p>
  1413. *
  1414. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1415. * @param valueToFind the value to find
  1416. * @param startIndex the start index to travers backwards from
  1417. * @return the last index of the value within the array,
  1418. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1419. */
  1420. public static int lastIndexOf(int[] array, int valueToFind, int startIndex) {
  1421. if (array == null) {
  1422. return INDEX_NOT_FOUND;
  1423. }
  1424. if (startIndex < 0) {
  1425. return INDEX_NOT_FOUND;
  1426. } else if (startIndex >= array.length) {
  1427. startIndex = array.length - 1;
  1428. }
  1429. for (int i = startIndex; i >= 0; i--) {
  1430. if (valueToFind == array[i]) {
  1431. return i;
  1432. }
  1433. }
  1434. return INDEX_NOT_FOUND;
  1435. }
  1436. /**
  1437. * <p>Checks if the value is in the given array.</p>
  1438. *
  1439. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1440. *
  1441. * @param array the array to search through
  1442. * @param valueToFind the value to find
  1443. * @return <code>true</code> if the array contains the object
  1444. */
  1445. public static boolean contains(int[] array, int valueToFind) {
  1446. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  1447. }
  1448. // short IndexOf
  1449. //-----------------------------------------------------------------------
  1450. /**
  1451. * <p>Finds the index of the given value in the array.</p>
  1452. *
  1453. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1454. *
  1455. * @param array the array to search through for the object, may be <code>null</code>
  1456. * @param valueToFind the value to find
  1457. * @return the index of the value within the array,
  1458. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1459. */
  1460. public static int indexOf(short[] array, short valueToFind) {
  1461. return indexOf(array, valueToFind, 0);
  1462. }
  1463. /**
  1464. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1465. *
  1466. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1467. *
  1468. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1469. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1470. *
  1471. * @param array the array to search through for the object, may be <code>null</code>
  1472. * @param valueToFind the value to find
  1473. * @param startIndex the index to start searching at
  1474. * @return the index of the value within the array,
  1475. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1476. */
  1477. public static int indexOf(short[] array, short valueToFind, int startIndex) {
  1478. if (array == null) {
  1479. return INDEX_NOT_FOUND;
  1480. }
  1481. if (startIndex < 0) {
  1482. startIndex = 0;
  1483. }
  1484. for (int i = startIndex; i < array.length; i++) {
  1485. if (valueToFind == array[i]) {
  1486. return i;
  1487. }
  1488. }
  1489. return INDEX_NOT_FOUND;
  1490. }
  1491. /**
  1492. * <p>Finds the last index of the given value within the array.</p>
  1493. *
  1494. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1495. *
  1496. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1497. * @param valueToFind the object to find
  1498. * @return the last index of the value within the array,
  1499. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1500. */
  1501. public static int lastIndexOf(short[] array, short valueToFind) {
  1502. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1503. }
  1504. /**
  1505. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  1506. *
  1507. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1508. *
  1509. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  1510. * array length will search from the end of the array.</p>
  1511. *
  1512. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1513. * @param valueToFind the value to find
  1514. * @param startIndex the start index to travers backwards from
  1515. * @return the last index of the value within the array,
  1516. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1517. */
  1518. public static int lastIndexOf(short[] array, short valueToFind, int startIndex) {
  1519. if (array == null) {
  1520. return INDEX_NOT_FOUND;
  1521. }
  1522. if (startIndex < 0) {
  1523. return INDEX_NOT_FOUND;
  1524. } else if (startIndex >= array.length) {
  1525. startIndex = array.length - 1;
  1526. }
  1527. for (int i = startIndex; i >= 0; i--) {
  1528. if (valueToFind == array[i]) {
  1529. return i;
  1530. }
  1531. }
  1532. return INDEX_NOT_FOUND;
  1533. }
  1534. /**
  1535. * <p>Checks if the value is in the given array.</p>
  1536. *
  1537. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1538. *
  1539. * @param array the array to search through
  1540. * @param valueToFind the value to find
  1541. * @return <code>true</code> if the array contains the object
  1542. */
  1543. public static boolean contains(short[] array, short valueToFind) {
  1544. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  1545. }
  1546. // char IndexOf
  1547. //-----------------------------------------------------------------------
  1548. /**
  1549. * <p>Finds the index of the given value in the array.</p>
  1550. *
  1551. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1552. *
  1553. * @param array the array to search through for the object, may be <code>null</code>
  1554. * @param valueToFind the value to find
  1555. * @return the index of the value within the array,
  1556. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1557. * @since 2.1
  1558. */
  1559. public static int indexOf(char[] array, char valueToFind) {
  1560. return indexOf(array, valueToFind, 0);
  1561. }
  1562. /**
  1563. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1564. *
  1565. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1566. *
  1567. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1568. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1569. *
  1570. * @param array the array to search through for the object, may be <code>null</code>
  1571. * @param valueToFind the value to find
  1572. * @param startIndex the index to start searching at
  1573. * @return the index of the value within the array,
  1574. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1575. * @since 2.1
  1576. */
  1577. public static int indexOf(char[] array, char valueToFind, int startIndex) {
  1578. if (array == null) {
  1579. return INDEX_NOT_FOUND;
  1580. }
  1581. if (startIndex < 0) {
  1582. startIndex = 0;
  1583. }
  1584. for (int i = startIndex; i < array.length; i++) {
  1585. if (valueToFind == array[i]) {
  1586. return i;
  1587. }
  1588. }
  1589. return INDEX_NOT_FOUND;
  1590. }
  1591. /**
  1592. * <p>Finds the last index of the given value within the array.</p>
  1593. *
  1594. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1595. *
  1596. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1597. * @param valueToFind the object to find
  1598. * @return the last index of the value within the array,
  1599. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1600. * @since 2.1
  1601. */
  1602. public static int lastIndexOf(char[] array, char valueToFind) {
  1603. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1604. }
  1605. /**
  1606. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  1607. *
  1608. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1609. *
  1610. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  1611. * array length will search from the end of the array.</p>
  1612. *
  1613. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1614. * @param valueToFind the value to find
  1615. * @param startIndex the start index to travers backwards from
  1616. * @return the last index of the value within the array,
  1617. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1618. * @since 2.1
  1619. */
  1620. public static int lastIndexOf(char[] array, char valueToFind, int startIndex) {
  1621. if (array == null) {
  1622. return INDEX_NOT_FOUND;
  1623. }
  1624. if (startIndex < 0) {
  1625. return INDEX_NOT_FOUND;
  1626. } else if (startIndex >= array.length) {
  1627. startIndex = array.length - 1;
  1628. }
  1629. for (int i = startIndex; i >= 0; i--) {
  1630. if (valueToFind == array[i]) {
  1631. return i;
  1632. }
  1633. }
  1634. return INDEX_NOT_FOUND;
  1635. }
  1636. /**
  1637. * <p>Checks if the value is in the given array.</p>
  1638. *
  1639. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1640. *
  1641. * @param array the array to search through
  1642. * @param valueToFind the value to find
  1643. * @return <code>true</code> if the array contains the object
  1644. * @since 2.1
  1645. */
  1646. public static boolean contains(char[] array, char valueToFind) {
  1647. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  1648. }
  1649. // byte IndexOf
  1650. //-----------------------------------------------------------------------
  1651. /**
  1652. * <p>Finds the index of the given value in the array.</p>
  1653. *
  1654. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1655. *
  1656. * @param array the array to search through for the object, may be <code>null</code>
  1657. * @param valueToFind the value to find
  1658. * @return the index of the value within the array,
  1659. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1660. */
  1661. public static int indexOf(byte[] array, byte valueToFind) {
  1662. return indexOf(array, valueToFind, 0);
  1663. }
  1664. /**
  1665. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1666. *
  1667. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1668. *
  1669. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1670. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1671. *
  1672. * @param array the array to search through for the object, may be <code>null</code>
  1673. * @param valueToFind the value to find
  1674. * @param startIndex the index to start searching at
  1675. * @return the index of the value within the array,
  1676. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1677. */
  1678. public static int indexOf(byte[] array, byte valueToFind, int startIndex) {
  1679. if (array == null) {
  1680. return INDEX_NOT_FOUND;
  1681. }
  1682. if (startIndex < 0) {
  1683. startIndex = 0;
  1684. }
  1685. for (int i = startIndex; i < array.length; i++) {
  1686. if (valueToFind == array[i]) {
  1687. return i;
  1688. }
  1689. }
  1690. return INDEX_NOT_FOUND;
  1691. }
  1692. /**
  1693. * <p>Finds the last index of the given value within the array.</p>
  1694. *
  1695. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1696. *
  1697. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1698. * @param valueToFind the object to find
  1699. * @return the last index of the value within the array,
  1700. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1701. */
  1702. public static int lastIndexOf(byte[] array, byte valueToFind) {
  1703. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1704. }
  1705. /**
  1706. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  1707. *
  1708. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1709. *
  1710. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  1711. * array length will search from the end of the array.</p>
  1712. *
  1713. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1714. * @param valueToFind the value to find
  1715. * @param startIndex the start index to travers backwards from
  1716. * @return the last index of the value within the array,
  1717. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1718. */
  1719. public static int lastIndexOf(byte[] array, byte valueToFind, int startIndex) {
  1720. if (array == null) {
  1721. return INDEX_NOT_FOUND;
  1722. }
  1723. if (startIndex < 0) {
  1724. return INDEX_NOT_FOUND;
  1725. } else if (startIndex >= array.length) {
  1726. startIndex = array.length - 1;
  1727. }
  1728. for (int i = startIndex; i >= 0; i--) {
  1729. if (valueToFind == array[i]) {
  1730. return i;
  1731. }
  1732. }
  1733. return INDEX_NOT_FOUND;
  1734. }
  1735. /**
  1736. * <p>Checks if the value is in the given array.</p>
  1737. *
  1738. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1739. *
  1740. * @param array the array to search through
  1741. * @param valueToFind the value to find
  1742. * @return <code>true</code> if the array contains the object
  1743. */
  1744. public static boolean contains(byte[] array, byte valueToFind) {
  1745. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  1746. }
  1747. // double IndexOf
  1748. //-----------------------------------------------------------------------
  1749. /**
  1750. * <p>Finds the index of the given value in the array.</p>
  1751. *
  1752. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1753. *
  1754. * @param array the array to search through for the object, may be <code>null</code>
  1755. * @param valueToFind the value to find
  1756. * @return the index of the value within the array,
  1757. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1758. */
  1759. public static int indexOf(double[] array, double valueToFind) {
  1760. return indexOf(array, valueToFind, 0);
  1761. }
  1762. /**
  1763. * <p>Finds the index of the given value within a given tolerance in the array.
  1764. * This method will return the index of the first value which falls between the region
  1765. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1766. *
  1767. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1768. *
  1769. * @param array the array to search through for the object, may be <code>null</code>
  1770. * @param valueToFind the value to find
  1771. * @param tolerance tolerance of the search
  1772. * @return the index of the value within the array,
  1773. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1774. */
  1775. public static int indexOf(double[] array, double valueToFind, double tolerance) {
  1776. return indexOf(array, valueToFind, 0, tolerance);
  1777. }
  1778. /**
  1779. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1780. *
  1781. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1782. *
  1783. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1784. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1785. *
  1786. * @param array the array to search through for the object, may be <code>null</code>
  1787. * @param valueToFind the value to find
  1788. * @param startIndex the index to start searching at
  1789. * @return the index of the value within the array,
  1790. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1791. */
  1792. public static int indexOf(double[] array, double valueToFind, int startIndex) {
  1793. if (ArrayUtils.isEmpty(array)) {
  1794. return INDEX_NOT_FOUND;
  1795. }
  1796. if (startIndex < 0) {
  1797. startIndex = 0;
  1798. }
  1799. for (int i = startIndex; i < array.length; i++) {
  1800. if (valueToFind == array[i]) {
  1801. return i;
  1802. }
  1803. }
  1804. return INDEX_NOT_FOUND;
  1805. }
  1806. /**
  1807. * <p>Finds the index of the given value in the array starting at the given index.
  1808. * This method will return the index of the first value which falls between the region
  1809. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1810. *
  1811. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1812. *
  1813. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1814. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1815. *
  1816. * @param array the array to search through for the object, may be <code>null</code>
  1817. * @param valueToFind the value to find
  1818. * @param startIndex the index to start searching at
  1819. * @param tolerance tolerance of the search
  1820. * @return the index of the value within the array,
  1821. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1822. */
  1823. public static int indexOf(double[] array, double valueToFind, int startIndex, double tolerance) {
  1824. if (ArrayUtils.isEmpty(array)) {
  1825. return INDEX_NOT_FOUND;
  1826. }
  1827. if (startIndex < 0) {
  1828. startIndex = 0;
  1829. }
  1830. double min = valueToFind - tolerance;
  1831. double max = valueToFind + tolerance;
  1832. for (int i = startIndex; i < array.length; i++) {
  1833. if (array[i] >= min && array[i] <= max) {
  1834. return i;
  1835. }
  1836. }
  1837. return INDEX_NOT_FOUND;
  1838. }
  1839. /**
  1840. * <p>Finds the last index of the given value within the array.</p>
  1841. *
  1842. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1843. *
  1844. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  1845. * @param valueToFind the object to find
  1846. * @return the last index of the value within the array,
  1847. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1848. */
  1849. public static int lastIndexOf(double[] array, double valueToFind) {
  1850. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  1851. }
  1852. /**
  1853. * <p>Finds the last index of the given value within a given tolerance in the array.
  1854. * This method will return the index of the last value which falls between the region
  1855. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1856. *
  1857. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1858. *
  1859. * @param array the array to search through for the object, may be <code>null</code>
  1860. * @param valueToFind the value to find
  1861. * @param tolerance tolerance of the search
  1862. * @return the index of the value within the array,
  1863. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1864. */
  1865. public static int lastIndexOf(double[] array, double valueToFind, double tolerance) {
  1866. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE, tolerance);
  1867. }
  1868. /**
  1869. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  1870. *
  1871. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1872. *
  1873. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  1874. * array length will search from the end of the array.</p>
  1875. *
  1876. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1877. * @param valueToFind the value to find
  1878. * @param startIndex the start index to travers backwards from
  1879. * @return the last index of the value within the array,
  1880. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1881. */
  1882. public static int lastIndexOf(double[] array, double valueToFind, int startIndex) {
  1883. if (ArrayUtils.isEmpty(array)) {
  1884. return INDEX_NOT_FOUND;
  1885. }
  1886. if (startIndex < 0) {
  1887. return INDEX_NOT_FOUND;
  1888. } else if (startIndex >= array.length) {
  1889. startIndex = array.length - 1;
  1890. }
  1891. for (int i = startIndex; i >= 0; i--) {
  1892. if (valueToFind == array[i]) {
  1893. return i;
  1894. }
  1895. }
  1896. return INDEX_NOT_FOUND;
  1897. }
  1898. /**
  1899. * <p>Finds the last index of the given value in the array starting at the given index.
  1900. * This method will return the index of the last value which falls between the region
  1901. * defined by valueToFind - tolerance and valueToFind + tolerance.</p>
  1902. *
  1903. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1904. *
  1905. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  1906. * array length will search from the end of the array.</p>
  1907. *
  1908. * @param array the array to traverse for looking for the object, may be <code>null</code>
  1909. * @param valueToFind the value to find
  1910. * @param startIndex the start index to travers backwards from
  1911. * @param tolerance search for value within plus/minus this amount
  1912. * @return the last index of the value within the array,
  1913. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1914. */
  1915. public static int lastIndexOf(double[] array, double valueToFind, int startIndex, double tolerance) {
  1916. if (ArrayUtils.isEmpty(array)) {
  1917. return INDEX_NOT_FOUND;
  1918. }
  1919. if (startIndex < 0) {
  1920. return INDEX_NOT_FOUND;
  1921. } else if (startIndex >= array.length) {
  1922. startIndex = array.length - 1;
  1923. }
  1924. double min = valueToFind - tolerance;
  1925. double max = valueToFind + tolerance;
  1926. for (int i = startIndex; i >= 0; i--) {
  1927. if (array[i] >= min && array[i] <= max) {
  1928. return i;
  1929. }
  1930. }
  1931. return INDEX_NOT_FOUND;
  1932. }
  1933. /**
  1934. * <p>Checks if the value is in the given array.</p>
  1935. *
  1936. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  1937. *
  1938. * @param array the array to search through
  1939. * @param valueToFind the value to find
  1940. * @return <code>true</code> if the array contains the object
  1941. */
  1942. public static boolean contains(double[] array, double valueToFind) {
  1943. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  1944. }
  1945. /**
  1946. * <p>Checks if a value falling within the given tolerance is in the
  1947. * given array. If the array contains a value within the inclusive range
  1948. * defined by (value - tolerance) to (value + tolerance).</p>
  1949. *
  1950. * <p>The method returns <code>false</code> if a <code>null</code> array
  1951. * is passed in.</p>
  1952. *
  1953. * @param array the array to search
  1954. * @param valueToFind the value to find
  1955. * @param tolerance the array contains the tolerance of the search
  1956. * @return true if value falling within tolerance is in array
  1957. */
  1958. public static boolean contains(double[] array, double valueToFind, double tolerance) {
  1959. return indexOf(array, valueToFind, 0, tolerance) != INDEX_NOT_FOUND;
  1960. }
  1961. // float IndexOf
  1962. //-----------------------------------------------------------------------
  1963. /**
  1964. * <p>Finds the index of the given value in the array.</p>
  1965. *
  1966. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1967. *
  1968. * @param array the array to search through for the object, may be <code>null</code>
  1969. * @param valueToFind the value to find
  1970. * @return the index of the value within the array,
  1971. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1972. */
  1973. public static int indexOf(float[] array, float valueToFind) {
  1974. return indexOf(array, valueToFind, 0);
  1975. }
  1976. /**
  1977. * <p>Finds the index of the given value in the array starting at the given index.</p>
  1978. *
  1979. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  1980. *
  1981. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  1982. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  1983. *
  1984. * @param array the array to search through for the object, may be <code>null</code>
  1985. * @param valueToFind the value to find
  1986. * @param startIndex the index to start searching at
  1987. * @return the index of the value within the array,
  1988. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  1989. */
  1990. public static int indexOf(float[] array, float valueToFind, int startIndex) {
  1991. if (ArrayUtils.isEmpty(array)) {
  1992. return INDEX_NOT_FOUND;
  1993. }
  1994. if (startIndex < 0) {
  1995. startIndex = 0;
  1996. }
  1997. for (int i = startIndex; i < array.length; i++) {
  1998. if (valueToFind == array[i]) {
  1999. return i;
  2000. }
  2001. }
  2002. return INDEX_NOT_FOUND;
  2003. }
  2004. /**
  2005. * <p>Finds the last index of the given value within the array.</p>
  2006. *
  2007. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  2008. *
  2009. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  2010. * @param valueToFind the object to find
  2011. * @return the last index of the value within the array,
  2012. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  2013. */
  2014. public static int lastIndexOf(float[] array, float valueToFind) {
  2015. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  2016. }
  2017. /**
  2018. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  2019. *
  2020. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  2021. *
  2022. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than the
  2023. * array length will search from the end of the array.</p>
  2024. *
  2025. * @param array the array to traverse for looking for the object, may be <code>null</code>
  2026. * @param valueToFind the value to find
  2027. * @param startIndex the start index to travers backwards from
  2028. * @return the last index of the value within the array,
  2029. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  2030. */
  2031. public static int lastIndexOf(float[] array, float valueToFind, int startIndex) {
  2032. if (ArrayUtils.isEmpty(array)) {
  2033. return INDEX_NOT_FOUND;
  2034. }
  2035. if (startIndex < 0) {
  2036. return INDEX_NOT_FOUND;
  2037. } else if (startIndex >= array.length) {
  2038. startIndex = array.length - 1;
  2039. }
  2040. for (int i = startIndex; i >= 0; i--) {
  2041. if (valueToFind == array[i]) {
  2042. return i;
  2043. }
  2044. }
  2045. return INDEX_NOT_FOUND;
  2046. }
  2047. /**
  2048. * <p>Checks if the value is in the given array.</p>
  2049. *
  2050. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  2051. *
  2052. * @param array the array to search through
  2053. * @param valueToFind the value to find
  2054. * @return <code>true</code> if the array contains the object
  2055. */
  2056. public static boolean contains(float[] array, float valueToFind) {
  2057. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  2058. }
  2059. // boolean IndexOf
  2060. //-----------------------------------------------------------------------
  2061. /**
  2062. * <p>Finds the index of the given value in the array.</p>
  2063. *
  2064. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  2065. *
  2066. * @param array the array to search through for the object, may be <code>null</code>
  2067. * @param valueToFind the value to find
  2068. * @return the index of the value within the array,
  2069. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  2070. */
  2071. public static int indexOf(boolean[] array, boolean valueToFind) {
  2072. return indexOf(array, valueToFind, 0);
  2073. }
  2074. /**
  2075. * <p>Finds the index of the given value in the array starting at the given index.</p>
  2076. *
  2077. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  2078. *
  2079. * <p>A negative startIndex is treated as zero. A startIndex larger than the array
  2080. * length will return {@link #INDEX_NOT_FOUND} (<code>-1</code>).</p>
  2081. *
  2082. * @param array the array to search through for the object, may be <code>null</code>
  2083. * @param valueToFind the value to find
  2084. * @param startIndex the index to start searching at
  2085. * @return the index of the value within the array,
  2086. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code>
  2087. * array input
  2088. */
  2089. public static int indexOf(boolean[] array, boolean valueToFind, int startIndex) {
  2090. if (ArrayUtils.isEmpty(array)) {
  2091. return INDEX_NOT_FOUND;
  2092. }
  2093. if (startIndex < 0) {
  2094. startIndex = 0;
  2095. }
  2096. for (int i = startIndex; i < array.length; i++) {
  2097. if (valueToFind == array[i]) {
  2098. return i;
  2099. }
  2100. }
  2101. return INDEX_NOT_FOUND;
  2102. }
  2103. /**
  2104. * <p>Finds the last index of the given value within the array.</p>
  2105. *
  2106. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) if
  2107. * <code>null</code> array input.</p>
  2108. *
  2109. * @param array the array to travers backwords looking for the object, may be <code>null</code>
  2110. * @param valueToFind the object to find
  2111. * @return the last index of the value within the array,
  2112. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  2113. */
  2114. public static int lastIndexOf(boolean[] array, boolean valueToFind) {
  2115. return lastIndexOf(array, valueToFind, Integer.MAX_VALUE);
  2116. }
  2117. /**
  2118. * <p>Finds the last index of the given value in the array starting at the given index.</p>
  2119. *
  2120. * <p>This method returns {@link #INDEX_NOT_FOUND} (<code>-1</code>) for a <code>null</code> input array.</p>
  2121. *
  2122. * <p>A negative startIndex will return {@link #INDEX_NOT_FOUND} (<code>-1</code>). A startIndex larger than
  2123. * the array length will search from the end of the array.</p>
  2124. *
  2125. * @param array the array to traverse for looking for the object, may be <code>null</code>
  2126. * @param valueToFind the value to find
  2127. * @param startIndex the start index to travers backwards from
  2128. * @return the last index of the value within the array,
  2129. * {@link #INDEX_NOT_FOUND} (<code>-1</code>) if not found or <code>null</code> array input
  2130. */
  2131. public static int lastIndexOf(boolean[] array, boolean valueToFind, int startIndex) {
  2132. if (ArrayUtils.isEmpty(array)) {
  2133. return INDEX_NOT_FOUND;
  2134. }
  2135. if (startIndex < 0) {
  2136. return INDEX_NOT_FOUND;
  2137. } else if (startIndex >= array.length) {
  2138. startIndex = array.length - 1;
  2139. }
  2140. for (int i = startIndex; i >= 0; i--) {
  2141. if (valueToFind == array[i]) {
  2142. return i;
  2143. }
  2144. }
  2145. return INDEX_NOT_FOUND;
  2146. }
  2147. /**
  2148. * <p>Checks if the value is in the given array.</p>
  2149. *
  2150. * <p>The method returns <code>false</code> if a <code>null</code> array is passed in.</p>
  2151. *
  2152. * @param array the array to search through
  2153. * @param valueToFind the value to find
  2154. * @return <code>true</code> if the array contains the object
  2155. */
  2156. public static boolean contains(boolean[] array, boolean valueToFind) {
  2157. return indexOf(array, valueToFind) != INDEX_NOT_FOUND;
  2158. }
  2159. // Primitive/Object array converters
  2160. // ----------------------------------------------------------------------
  2161. // Character array converters
  2162. // ----------------------------------------------------------------------
  2163. /**
  2164. * <p>Converts an array of object Characters to primitives.</p>
  2165. *
  2166. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2167. *
  2168. * @param array a <code>Character</code> array, may be <code>null</code>
  2169. * @return a <code>char</code> array, <code>null</code> if null array input
  2170. * @throws NullPointerException if array content is <code>null</code>
  2171. */
  2172. public static char[] toPrimitive(Character[] array) {
  2173. if (array == null) {
  2174. return null;
  2175. } else if (array.length == 0) {
  2176. return EMPTY_CHAR_ARRAY;
  2177. }
  2178. final char[] result = new char[array.length];
  2179. for (int i = 0; i < array.length; i++) {
  2180. result[i] = array[i].charValue();
  2181. }
  2182. return result;
  2183. }
  2184. /**
  2185. * <p>Converts an array of object Character to primitives handling <code>null</code>.</p>
  2186. *
  2187. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2188. *
  2189. * @param array a <code>Character</code> array, may be <code>null</code>
  2190. * @param valueForNull the value to insert if <code>null</code> found
  2191. * @return a <code>char</code> array, <code>null</code> if null array input
  2192. */
  2193. public static char[] toPrimitive(Character[] array, char valueForNull) {
  2194. if (array == null) {
  2195. return null;
  2196. } else if (array.length == 0) {
  2197. return EMPTY_CHAR_ARRAY;
  2198. }
  2199. final char[] result = new char[array.length];
  2200. for (int i = 0; i < array.length; i++) {
  2201. Character b = array[i];
  2202. result[i] = (b == null ? valueForNull : b.charValue());
  2203. }
  2204. return result;
  2205. }
  2206. /**
  2207. * <p>Converts an array of primitive chars to objects.</p>
  2208. *
  2209. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2210. *
  2211. * @param array a <code>char</code> array
  2212. * @return a <code>Character</code> array, <code>null</code> if null array input
  2213. */
  2214. public static Character[] toObject(char[] array) {
  2215. if (array == null) {
  2216. return null;
  2217. } else if (array.length == 0) {
  2218. return EMPTY_CHARACTER_OBJECT_ARRAY;
  2219. }
  2220. final Character[] result = new Character[array.length];
  2221. for (int i = 0; i < array.length; i++) {
  2222. result[i] = new Character(array[i]);
  2223. }
  2224. return result;
  2225. }
  2226. // Long array converters
  2227. // ----------------------------------------------------------------------
  2228. /**
  2229. * <p>Converts an array of object Longs to primitives.</p>
  2230. *
  2231. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2232. *
  2233. * @param array a <code>Long</code> array, may be <code>null</code>
  2234. * @return a <code>long</code> array, <code>null</code> if null array input
  2235. * @throws NullPointerException if array content is <code>null</code>
  2236. */
  2237. public static long[] toPrimitive(Long[] array) {
  2238. if (array == null) {
  2239. return null;
  2240. } else if (array.length == 0) {
  2241. return EMPTY_LONG_ARRAY;
  2242. }
  2243. final long[] result = new long[array.length];
  2244. for (int i = 0; i < array.length; i++) {
  2245. result[i] = array[i].longValue();
  2246. }
  2247. return result;
  2248. }
  2249. /**
  2250. * <p>Converts an array of object Long to primitives handling <code>null</code>.</p>
  2251. *
  2252. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2253. *
  2254. * @param array a <code>Long</code> array, may be <code>null</code>
  2255. * @param valueForNull the value to insert if <code>null</code> found
  2256. * @return a <code>long</code> array, <code>null</code> if null array input
  2257. */
  2258. public static long[] toPrimitive(Long[] array, long valueForNull) {
  2259. if (array == null) {
  2260. return null;
  2261. } else if (array.length == 0) {
  2262. return EMPTY_LONG_ARRAY;
  2263. }
  2264. final long[] result = new long[array.length];
  2265. for (int i = 0; i < array.length; i++) {
  2266. Long b = array[i];
  2267. result[i] = (b == null ? valueForNull : b.longValue());
  2268. }
  2269. return result;
  2270. }
  2271. /**
  2272. * <p>Converts an array of primitive longs to objects.</p>
  2273. *
  2274. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2275. *
  2276. * @param array a <code>long</code> array
  2277. * @return a <code>Long</code> array, <code>null</code> if null array input
  2278. */
  2279. public static Long[] toObject(long[] array) {
  2280. if (array == null) {
  2281. return null;
  2282. } else if (array.length == 0) {
  2283. return EMPTY_LONG_OBJECT_ARRAY;
  2284. }
  2285. final Long[] result = new Long[array.length];
  2286. for (int i = 0; i < array.length; i++) {
  2287. result[i] = new Long(array[i]);
  2288. }
  2289. return result;
  2290. }
  2291. // Int array converters
  2292. // ----------------------------------------------------------------------
  2293. /**
  2294. * <p>Converts an array of object Integers to primitives.</p>
  2295. *
  2296. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2297. *
  2298. * @param array a <code>Integer</code> array, may be <code>null</code>
  2299. * @return an <code>int</code> array, <code>null</code> if null array input
  2300. * @throws NullPointerException if array content is <code>null</code>
  2301. */
  2302. public static int[] toPrimitive(Integer[] array) {
  2303. if (array == null) {
  2304. return null;
  2305. } else if (array.length == 0) {
  2306. return EMPTY_INT_ARRAY;
  2307. }
  2308. final int[] result = new int[array.length];
  2309. for (int i = 0; i < array.length; i++) {
  2310. result[i] = array[i].intValue();
  2311. }
  2312. return result;
  2313. }
  2314. /**
  2315. * <p>Converts an array of object Integer to primitives handling <code>null</code>.</p>
  2316. *
  2317. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2318. *
  2319. * @param array a <code>Integer</code> array, may be <code>null</code>
  2320. * @param valueForNull the value to insert if <code>null</code> found
  2321. * @return an <code>int</code> array, <code>null</code> if null array input
  2322. */
  2323. public static int[] toPrimitive(Integer[] array, int valueForNull) {
  2324. if (array == null) {
  2325. return null;
  2326. } else if (array.length == 0) {
  2327. return EMPTY_INT_ARRAY;
  2328. }
  2329. final int[] result = new int[array.length];
  2330. for (int i = 0; i < array.length; i++) {
  2331. Integer b = array[i];
  2332. result[i] = (b == null ? valueForNull : b.intValue());
  2333. }
  2334. return result;
  2335. }
  2336. /**
  2337. * <p>Converts an array of primitive ints to objects.</p>
  2338. *
  2339. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2340. *
  2341. * @param array an <code>int</code> array
  2342. * @return an <code>Integer</code> array, <code>null</code> if null array input
  2343. */
  2344. public static Integer[] toObject(int[] array) {
  2345. if (array == null) {
  2346. return null;
  2347. } else if (array.length == 0) {
  2348. return EMPTY_INTEGER_OBJECT_ARRAY;
  2349. }
  2350. final Integer[] result = new Integer[array.length];
  2351. for (int i = 0; i < array.length; i++) {
  2352. result[i] = new Integer(array[i]);
  2353. }
  2354. return result;
  2355. }
  2356. // Short array converters
  2357. // ----------------------------------------------------------------------
  2358. /**
  2359. * <p>Converts an array of object Shorts to primitives.</p>
  2360. *
  2361. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2362. *
  2363. * @param array a <code>Short</code> array, may be <code>null</code>
  2364. * @return a <code>byte</code> array, <code>null</code> if null array input
  2365. * @throws NullPointerException if array content is <code>null</code>
  2366. */
  2367. public static short[] toPrimitive(Short[] array) {
  2368. if (array == null) {
  2369. return null;
  2370. } else if (array.length == 0) {
  2371. return EMPTY_SHORT_ARRAY;
  2372. }
  2373. final short[] result = new short[array.length];
  2374. for (int i = 0; i < array.length; i++) {
  2375. result[i] = array[i].shortValue();
  2376. }
  2377. return result;
  2378. }
  2379. /**
  2380. * <p>Converts an array of object Short to primitives handling <code>null</code>.</p>
  2381. *
  2382. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2383. *
  2384. * @param array a <code>Short</code> array, may be <code>null</code>
  2385. * @param valueForNull the value to insert if <code>null</code> found
  2386. * @return a <code>byte</code> array, <code>null</code> if null array input
  2387. */
  2388. public static short[] toPrimitive(Short[] array, short valueForNull) {
  2389. if (array == null) {
  2390. return null;
  2391. } else if (array.length == 0) {
  2392. return EMPTY_SHORT_ARRAY;
  2393. }
  2394. final short[] result = new short[array.length];
  2395. for (int i = 0; i < array.length; i++) {
  2396. Short b = array[i];
  2397. result[i] = (b == null ? valueForNull : b.shortValue());
  2398. }
  2399. return result;
  2400. }
  2401. /**
  2402. * <p>Converts an array of primitive shorts to objects.</p>
  2403. *
  2404. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2405. *
  2406. * @param array a <code>short</code> array
  2407. * @return a <code>Short</code> array, <code>null</code> if null array input
  2408. */
  2409. public static Short[] toObject(short[] array) {
  2410. if (array == null) {
  2411. return null;
  2412. } else if (array.length == 0) {
  2413. return EMPTY_SHORT_OBJECT_ARRAY;
  2414. }
  2415. final Short[] result = new Short[array.length];
  2416. for (int i = 0; i < array.length; i++) {
  2417. result[i] = new Short(array[i]);
  2418. }
  2419. return result;
  2420. }
  2421. // Byte array converters
  2422. // ----------------------------------------------------------------------
  2423. /**
  2424. * <p>Converts an array of object Bytes to primitives.</p>
  2425. *
  2426. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2427. *
  2428. * @param array a <code>Byte</code> array, may be <code>null</code>
  2429. * @return a <code>byte</code> array, <code>null</code> if null array input
  2430. * @throws NullPointerException if array content is <code>null</code>
  2431. */
  2432. public static byte[] toPrimitive(Byte[] array) {
  2433. if (array == null) {
  2434. return null;
  2435. } else if (array.length == 0) {
  2436. return EMPTY_BYTE_ARRAY;
  2437. }
  2438. final byte[] result = new byte[array.length];
  2439. for (int i = 0; i < array.length; i++) {
  2440. result[i] = array[i].byteValue();
  2441. }
  2442. return result;
  2443. }
  2444. /**
  2445. * <p>Converts an array of object Bytes to primitives handling <code>null</code>.</p>
  2446. *
  2447. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2448. *
  2449. * @param array a <code>Byte</code> array, may be <code>null</code>
  2450. * @param valueForNull the value to insert if <code>null</code> found
  2451. * @return a <code>byte</code> array, <code>null</code> if null array input
  2452. */
  2453. public static byte[] toPrimitive(Byte[] array, byte valueForNull) {
  2454. if (array == null) {
  2455. return null;
  2456. } else if (array.length == 0) {
  2457. return EMPTY_BYTE_ARRAY;
  2458. }
  2459. final byte[] result = new byte[array.length];
  2460. for (int i = 0; i < array.length; i++) {
  2461. Byte b = array[i];
  2462. result[i] = (b == null ? valueForNull : b.byteValue());
  2463. }
  2464. return result;
  2465. }
  2466. /**
  2467. * <p>Converts an array of primitive bytes to objects.</p>
  2468. *
  2469. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2470. *
  2471. * @param array a <code>byte</code> array
  2472. * @return a <code>Byte</code> array, <code>null</code> if null array input
  2473. */
  2474. public static Byte[] toObject(byte[] array) {
  2475. if (array == null) {
  2476. return null;
  2477. } else if (array.length == 0) {
  2478. return EMPTY_BYTE_OBJECT_ARRAY;
  2479. }
  2480. final Byte[] result = new Byte[array.length];
  2481. for (int i = 0; i < array.length; i++) {
  2482. result[i] = new Byte(array[i]);
  2483. }
  2484. return result;
  2485. }
  2486. // Double array converters
  2487. // ----------------------------------------------------------------------
  2488. /**
  2489. * <p>Converts an array of object Doubles to primitives.</p>
  2490. *
  2491. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2492. *
  2493. * @param array a <code>Double</code> array, may be <code>null</code>
  2494. * @return a <code>double</code> array, <code>null</code> if null array input
  2495. * @throws NullPointerException if array content is <code>null</code>
  2496. */
  2497. public static double[] toPrimitive(Double[] array) {
  2498. if (array == null) {
  2499. return null;
  2500. } else if (array.length == 0) {
  2501. return EMPTY_DOUBLE_ARRAY;
  2502. }
  2503. final double[] result = new double[array.length];
  2504. for (int i = 0; i < array.length; i++) {
  2505. result[i] = array[i].doubleValue();
  2506. }
  2507. return result;
  2508. }
  2509. /**
  2510. * <p>Converts an array of object Doubles to primitives handling <code>null</code>.</p>
  2511. *
  2512. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2513. *
  2514. * @param array a <code>Double</code> array, may be <code>null</code>
  2515. * @param valueForNull the value to insert if <code>null</code> found
  2516. * @return a <code>double</code> array, <code>null</code> if null array input
  2517. */
  2518. public static double[] toPrimitive(Double[] array, double valueForNull) {
  2519. if (array == null) {
  2520. return null;
  2521. } else if (array.length == 0) {
  2522. return EMPTY_DOUBLE_ARRAY;
  2523. }
  2524. final double[] result = new double[array.length];
  2525. for (int i = 0; i < array.length; i++) {
  2526. Double b = array[i];
  2527. result[i] = (b == null ? valueForNull : b.doubleValue());
  2528. }
  2529. return result;
  2530. }
  2531. /**
  2532. * <p>Converts an array of primitive doubles to objects.</p>
  2533. *
  2534. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2535. *
  2536. * @param array a <code>double</code> array
  2537. * @return a <code>Double</code> array, <code>null</code> if null array input
  2538. */
  2539. public static Double[] toObject(double[] array) {
  2540. if (array == null) {
  2541. return null;
  2542. } else if (array.length == 0) {
  2543. return EMPTY_DOUBLE_OBJECT_ARRAY;
  2544. }
  2545. final Double[] result = new Double[array.length];
  2546. for (int i = 0; i < array.length; i++) {
  2547. result[i] = new Double(array[i]);
  2548. }
  2549. return result;
  2550. }
  2551. // Float array converters
  2552. // ----------------------------------------------------------------------
  2553. /**
  2554. * <p>Converts an array of object Floats to primitives.</p>
  2555. *
  2556. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2557. *
  2558. * @param array a <code>Float</code> array, may be <code>null</code>
  2559. * @return a <code>float</code> array, <code>null</code> if null array input
  2560. * @throws NullPointerException if array content is <code>null</code>
  2561. */
  2562. public static float[] toPrimitive(Float[] array) {
  2563. if (array == null) {
  2564. return null;
  2565. } else if (array.length == 0) {
  2566. return EMPTY_FLOAT_ARRAY;
  2567. }
  2568. final float[] result = new float[array.length];
  2569. for (int i = 0; i < array.length; i++) {
  2570. result[i] = array[i].floatValue();
  2571. }
  2572. return result;
  2573. }
  2574. /**
  2575. * <p>Converts an array of object Floats to primitives handling <code>null</code>.</p>
  2576. *
  2577. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2578. *
  2579. * @param array a <code>Float</code> array, may be <code>null</code>
  2580. * @param valueForNull the value to insert if <code>null</code> found
  2581. * @return a <code>float</code> array, <code>null</code> if null array input
  2582. */
  2583. public static float[] toPrimitive(Float[] array, float valueForNull) {
  2584. if (array == null) {
  2585. return null;
  2586. } else if (array.length == 0) {
  2587. return EMPTY_FLOAT_ARRAY;
  2588. }
  2589. final float[] result = new float[array.length];
  2590. for (int i = 0; i < array.length; i++) {
  2591. Float b = array[i];
  2592. result[i] = (b == null ? valueForNull : b.floatValue());
  2593. }
  2594. return result;
  2595. }
  2596. /**
  2597. * <p>Converts an array of primitive floats to objects.</p>
  2598. *
  2599. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2600. *
  2601. * @param array a <code>float</code> array
  2602. * @return a <code>Float</code> array, <code>null</code> if null array input
  2603. */
  2604. public static Float[] toObject(float[] array) {
  2605. if (array == null) {
  2606. return null;
  2607. } else if (array.length == 0) {
  2608. return EMPTY_FLOAT_OBJECT_ARRAY;
  2609. }
  2610. final Float[] result = new Float[array.length];
  2611. for (int i = 0; i < array.length; i++) {
  2612. result[i] = new Float(array[i]);
  2613. }
  2614. return result;
  2615. }
  2616. // Boolean array converters
  2617. // ----------------------------------------------------------------------
  2618. /**
  2619. * <p>Converts an array of object Booleans to primitives.</p>
  2620. *
  2621. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2622. *
  2623. * @param array a <code>Boolean</code> array, may be <code>null</code>
  2624. * @return a <code>boolean</code> array, <code>null</code> if null array input
  2625. * @throws NullPointerException if array content is <code>null</code>
  2626. */
  2627. public static boolean[] toPrimitive(Boolean[] array) {
  2628. if (array == null) {
  2629. return null;
  2630. } else if (array.length == 0) {
  2631. return EMPTY_BOOLEAN_ARRAY;
  2632. }
  2633. final boolean[] result = new boolean[array.length];
  2634. for (int i = 0; i < array.length; i++) {
  2635. result[i] = array[i].booleanValue();
  2636. }
  2637. return result;
  2638. }
  2639. /**
  2640. * <p>Converts an array of object Booleans to primitives handling <code>null</code>.</p>
  2641. *
  2642. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2643. *
  2644. * @param array a <code>Boolean</code> array, may be <code>null</code>
  2645. * @param valueForNull the value to insert if <code>null</code> found
  2646. * @return a <code>boolean</code> array, <code>null</code> if null array input
  2647. */
  2648. public static boolean[] toPrimitive(Boolean[] array, boolean valueForNull) {
  2649. if (array == null) {
  2650. return null;
  2651. } else if (array.length == 0) {
  2652. return EMPTY_BOOLEAN_ARRAY;
  2653. }
  2654. final boolean[] result = new boolean[array.length];
  2655. for (int i = 0; i < array.length; i++) {
  2656. Boolean b = array[i];
  2657. result[i] = (b == null ? valueForNull : b.booleanValue());
  2658. }
  2659. return result;
  2660. }
  2661. /**
  2662. * <p>Converts an array of primitive booleans to objects.</p>
  2663. *
  2664. * <p>This method returns <code>null</code> for a <code>null</code> input array.</p>
  2665. *
  2666. * @param array a <code>boolean</code> array
  2667. * @return a <code>Boolean</code> array, <code>null</code> if null array input
  2668. */
  2669. public static Boolean[] toObject(boolean[] array) {
  2670. if (array == null) {
  2671. return null;
  2672. } else if (array.length == 0) {
  2673. return EMPTY_BOOLEAN_OBJECT_ARRAY;
  2674. }
  2675. final Boolean[] result = new Boolean[array.length];
  2676. for (int i = 0; i < array.length; i++) {
  2677. result[i] = (array[i] ? Boolean.TRUE : Boolean.FALSE);
  2678. }
  2679. return result;
  2680. }
  2681. // ----------------------------------------------------------------------
  2682. /**
  2683. * <p>Checks if an array of Objects is empty or <code>null</code>.</p>
  2684. *
  2685. * @param array the array to test
  2686. * @return <code>true</code> if the array is empty or <code>null</code>
  2687. * @since 2.1
  2688. */
  2689. public static boolean isEmpty(Object[] array) {
  2690. if (array == null || array.length == 0) {
  2691. return true;
  2692. }
  2693. return false;
  2694. }
  2695. /**
  2696. * <p>Checks if an array of primitive longs is empty or <code>null</code>.</p>
  2697. *
  2698. * @param array the array to test
  2699. * @return <code>true</code> if the array is empty or <code>null</code>
  2700. * @since 2.1
  2701. */
  2702. public static boolean isEmpty(long[] array) {
  2703. if (array == null || array.length == 0) {
  2704. return true;
  2705. }
  2706. return false;
  2707. }
  2708. /**
  2709. * <p>Checks if an array of primitive ints is empty or <code>null</code>.</p>
  2710. *
  2711. * @param array the array to test
  2712. * @return <code>true</code> if the array is empty or <code>null</code>
  2713. * @since 2.1
  2714. */
  2715. public static boolean isEmpty(int[] array) {
  2716. if (array == null || array.length == 0) {
  2717. return true;
  2718. }
  2719. return false;
  2720. }
  2721. /**
  2722. * <p>Checks if an array of primitive shorts is empty or <code>null</code>.</p>
  2723. *
  2724. * @param array the array to test
  2725. * @return <code>true</code> if the array is empty or <code>null</code>
  2726. * @since 2.1
  2727. */
  2728. public static boolean isEmpty(short[] array) {
  2729. if (array == null || array.length == 0) {
  2730. return true;
  2731. }
  2732. return false;
  2733. }
  2734. /**
  2735. * <p>Checks if an array of primitive chars is empty or <code>null</code>.</p>
  2736. *
  2737. * @param array the array to test
  2738. * @return <code>true</code> if the array is empty or <code>null</code>
  2739. * @since 2.1
  2740. */
  2741. public static boolean isEmpty(char[] array) {
  2742. if (array == null || array.length == 0) {
  2743. return true;
  2744. }
  2745. return false;
  2746. }
  2747. /**
  2748. * <p>Checks if an array of primitive bytes is empty or <code>null</code>.</p>
  2749. *
  2750. * @param array the array to test
  2751. * @return <code>true</code> if the array is empty or <code>null</code>
  2752. * @since 2.1
  2753. */
  2754. public static boolean isEmpty(byte[] array) {
  2755. if (array == null || array.length == 0) {
  2756. return true;
  2757. }
  2758. return false;
  2759. }
  2760. /**
  2761. * <p>Checks if an array of primitive doubles is empty or <code>null</code>.</p>
  2762. *
  2763. * @param array the array to test
  2764. * @return <code>true</code> if the array is empty or <code>null</code>
  2765. * @since 2.1
  2766. */
  2767. public static boolean isEmpty(double[] array) {
  2768. if (array == null || array.length == 0) {
  2769. return true;
  2770. }
  2771. return false;
  2772. }
  2773. /**
  2774. * <p>Checks if an array of primitive floats is empty or <code>null</code>.</p>
  2775. *
  2776. * @param array the array to test
  2777. * @return <code>true</code> if the array is empty or <code>null</code>
  2778. * @since 2.1
  2779. */
  2780. public static boolean isEmpty(float[] array) {
  2781. if (array == null || array.length == 0) {
  2782. return true;
  2783. }
  2784. return false;
  2785. }
  2786. /**
  2787. * <p>Checks if an array of primitive booleans is empty or <code>null</code>.</p>
  2788. *
  2789. * @param array the array to test
  2790. * @return <code>true</code> if the array is empty or <code>null</code>
  2791. * @since 2.1
  2792. */
  2793. public static boolean isEmpty(boolean[] array) {
  2794. if (array == null || array.length == 0) {
  2795. return true;
  2796. }
  2797. return false;
  2798. }
  2799. /**
  2800. * <p>Adds all the elements of the given arrays into a new array.</p>
  2801. * <p>The new array contains all of the element of <code>array1</code> followed
  2802. * by all of the elements <code>array2</code>. When an array is returned, it is always
  2803. * a new array.</p>
  2804. *
  2805. * <pre>
  2806. * ArrayUtils.addAll(null, null) = null
  2807. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  2808. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  2809. * ArrayUtils.addAll([], []) = []
  2810. * ArrayUtils.addAll([null], [null]) = [null, null]
  2811. * ArrayUtils.addAll(["a", "b", "c"], ["1", "2", "3"]) = ["a", "b", "c", "1", "2", "3"]
  2812. * </pre>
  2813. *
  2814. * @param array1 the first array whose elements are added to the new array, may be <code>null</code>
  2815. * @param array2 the second array whose elements are added to the new array, may be <code>null</code>
  2816. * @return The new array, <code>null</code> if <code>null</code> array inputs.
  2817. * The type of the new array is the type of the first array.
  2818. * @since 2.1
  2819. */
  2820. public static Object[] addAll(Object[] array1, Object[] array2) {
  2821. if (array1 == null) {
  2822. return clone(array2);
  2823. } else if (array2 == null) {
  2824. return clone(array1);
  2825. }
  2826. Object[] joinedArray = (Object[]) Array.newInstance(array1.getClass().getComponentType(),
  2827. array1.length + array2.length);
  2828. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  2829. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  2830. return joinedArray;
  2831. }
  2832. /**
  2833. * <p>Adds all the elements of the given arrays into a new array.</p>
  2834. * <p>The new array contains all of the element of <code>array1</code> followed
  2835. * by all of the elements <code>array2</code>. When an array is returned, it is always
  2836. * a new array.</p>
  2837. *
  2838. * <pre>
  2839. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  2840. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  2841. * ArrayUtils.addAll([], []) = []
  2842. * </pre>
  2843. *
  2844. * @param array1 the first array whose elements are added to the new array.
  2845. * @param array2 the second array whose elements are added to the new array.
  2846. * @return The new boolean[] array.
  2847. * @since 2.1
  2848. */
  2849. public static boolean[] addAll(boolean[] array1, boolean[] array2) {
  2850. if (array1 == null) {
  2851. return clone(array2);
  2852. } else if (array2 == null) {
  2853. return clone(array1);
  2854. }
  2855. boolean[] joinedArray = new boolean[array1.length + array2.length];
  2856. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  2857. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  2858. return joinedArray;
  2859. }
  2860. /**
  2861. * <p>Adds all the elements of the given arrays into a new array.</p>
  2862. * <p>The new array contains all of the element of <code>array1</code> followed
  2863. * by all of the elements <code>array2</code>. When an array is returned, it is always
  2864. * a new array.</p>
  2865. *
  2866. * <pre>
  2867. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  2868. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  2869. * ArrayUtils.addAll([], []) = []
  2870. * </pre>
  2871. *
  2872. * @param array1 the first array whose elements are added to the new array.
  2873. * @param array2 the second array whose elements are added to the new array.
  2874. * @return The new char[] array.
  2875. * @since 2.1
  2876. */
  2877. public static char[] addAll(char[] array1, char[] array2) {
  2878. if (array1 == null) {
  2879. return clone(array2);
  2880. } else if (array2 == null) {
  2881. return clone(array1);
  2882. }
  2883. char[] joinedArray = new char[array1.length + array2.length];
  2884. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  2885. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  2886. return joinedArray;
  2887. }
  2888. /**
  2889. * <p>Adds all the elements of the given arrays into a new array.</p>
  2890. * <p>The new array contains all of the element of <code>array1</code> followed
  2891. * by all of the elements <code>array2</code>. When an array is returned, it is always
  2892. * a new array.</p>
  2893. *
  2894. * <pre>
  2895. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  2896. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  2897. * ArrayUtils.addAll([], []) = []
  2898. * </pre>
  2899. *
  2900. * @param array1 the first array whose elements are added to the new array.
  2901. * @param array2 the second array whose elements are added to the new array.
  2902. * @return The new byte[] array.
  2903. * @since 2.1
  2904. */
  2905. public static byte[] addAll(byte[] array1, byte[] array2) {
  2906. if (array1 == null) {
  2907. return clone(array2);
  2908. } else if (array2 == null) {
  2909. return clone(array1);
  2910. }
  2911. byte[] joinedArray = new byte[array1.length + array2.length];
  2912. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  2913. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  2914. return joinedArray;
  2915. }
  2916. /**
  2917. * <p>Adds all the elements of the given arrays into a new array.</p>
  2918. * <p>The new array contains all of the element of <code>array1</code> followed
  2919. * by all of the elements <code>array2</code>. When an array is returned, it is always
  2920. * a new array.</p>
  2921. *
  2922. * <pre>
  2923. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  2924. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  2925. * ArrayUtils.addAll([], []) = []
  2926. * </pre>
  2927. *
  2928. * @param array1 the first array whose elements are added to the new array.
  2929. * @param array2 the second array whose elements are added to the new array.
  2930. * @return The new short[] array.
  2931. * @since 2.1
  2932. */
  2933. public static short[] addAll(short[] array1, short[] array2) {
  2934. if (array1 == null) {
  2935. return clone(array2);
  2936. } else if (array2 == null) {
  2937. return clone(array1);
  2938. }
  2939. short[] joinedArray = new short[array1.length + array2.length];
  2940. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  2941. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  2942. return joinedArray;
  2943. }
  2944. /**
  2945. * <p>Adds all the elements of the given arrays into a new array.</p>
  2946. * <p>The new array contains all of the element of <code>array1</code> followed
  2947. * by all of the elements <code>array2</code>. When an array is returned, it is always
  2948. * a new array.</p>
  2949. *
  2950. * <pre>
  2951. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  2952. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  2953. * ArrayUtils.addAll([], []) = []
  2954. * </pre>
  2955. *
  2956. * @param array1 the first array whose elements are added to the new array.
  2957. * @param array2 the second array whose elements are added to the new array.
  2958. * @return The new int[] array.
  2959. * @since 2.1
  2960. */
  2961. public static int[] addAll(int[] array1, int[] array2) {
  2962. if (array1 == null) {
  2963. return clone(array2);
  2964. } else if (array2 == null) {
  2965. return clone(array1);
  2966. }
  2967. int[] joinedArray = new int[array1.length + array2.length];
  2968. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  2969. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  2970. return joinedArray;
  2971. }
  2972. /**
  2973. * <p>Adds all the elements of the given arrays into a new array.</p>
  2974. * <p>The new array contains all of the element of <code>array1</code> followed
  2975. * by all of the elements <code>array2</code>. When an array is returned, it is always
  2976. * a new array.</p>
  2977. *
  2978. * <pre>
  2979. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  2980. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  2981. * ArrayUtils.addAll([], []) = []
  2982. * </pre>
  2983. *
  2984. * @param array1 the first array whose elements are added to the new array.
  2985. * @param array2 the second array whose elements are added to the new array.
  2986. * @return The new long[] array.
  2987. * @since 2.1
  2988. */
  2989. public static long[] addAll(long[] array1, long[] array2) {
  2990. if (array1 == null) {
  2991. return clone(array2);
  2992. } else if (array2 == null) {
  2993. return clone(array1);
  2994. }
  2995. long[] joinedArray = new long[array1.length + array2.length];
  2996. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  2997. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  2998. return joinedArray;
  2999. }
  3000. /**
  3001. * <p>Adds all the elements of the given arrays into a new array.</p>
  3002. * <p>The new array contains all of the element of <code>array1</code> followed
  3003. * by all of the elements <code>array2</code>. When an array is returned, it is always
  3004. * a new array.</p>
  3005. *
  3006. * <pre>
  3007. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  3008. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  3009. * ArrayUtils.addAll([], []) = []
  3010. * </pre>
  3011. *
  3012. * @param array1 the first array whose elements are added to the new array.
  3013. * @param array2 the second array whose elements are added to the new array.
  3014. * @return The new float[] array.
  3015. * @since 2.1
  3016. */
  3017. public static float[] addAll(float[] array1, float[] array2) {
  3018. if (array1 == null) {
  3019. return clone(array2);
  3020. } else if (array2 == null) {
  3021. return clone(array1);
  3022. }
  3023. float[] joinedArray = new float[array1.length + array2.length];
  3024. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  3025. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  3026. return joinedArray;
  3027. }
  3028. /**
  3029. * <p>Adds all the elements of the given arrays into a new array.</p>
  3030. * <p>The new array contains all of the element of <code>array1</code> followed
  3031. * by all of the elements <code>array2</code>. When an array is returned, it is always
  3032. * a new array.</p>
  3033. *
  3034. * <pre>
  3035. * ArrayUtils.addAll(array1, null) = cloned copy of array1
  3036. * ArrayUtils.addAll(null, array2) = cloned copy of array2
  3037. * ArrayUtils.addAll([], []) = []
  3038. * </pre>
  3039. *
  3040. * @param array1 the first array whose elements are added to the new array.
  3041. * @param array2 the second array whose elements are added to the new array.
  3042. * @return The new double[] array.
  3043. * @since 2.1
  3044. */
  3045. public static double[] addAll(double[] array1, double[] array2) {
  3046. if (array1 == null) {
  3047. return clone(array2);
  3048. } else if (array2 == null) {
  3049. return clone(array1);
  3050. }
  3051. double[] joinedArray = new double[array1.length + array2.length];
  3052. System.arraycopy(array1, 0, joinedArray, 0, array1.length);
  3053. System.arraycopy(array2, 0, joinedArray, array1.length, array2.length);
  3054. return joinedArray;
  3055. }
  3056. /**
  3057. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3058. *
  3059. * <p>The new array contains the same elements of the input
  3060. * array plus the given element in the last position. The component type of
  3061. * the new array is the same as that of the input array.</p>
  3062. *
  3063. * <p>If the input array is <code>null</code>, a new one element array is returned
  3064. * whose component type is the same as the element.</p>
  3065. *
  3066. * <pre>
  3067. * ArrayUtils.add(null, null) = [null]
  3068. * ArrayUtils.add(null, "a") = ["a"]
  3069. * ArrayUtils.add(["a"], null) = ["a", null]
  3070. * ArrayUtils.add(["a"], "b") = ["a", "b"]
  3071. * ArrayUtils.add(["a", "b"], "c") = ["a", "b", "c"]
  3072. * </pre>
  3073. *
  3074. * @param array the array to "add" the element to, may be <code>null</code>
  3075. * @param element the object to add
  3076. * @return A new array containing the existing elements plus the new element
  3077. * @since 2.1
  3078. */
  3079. public static Object[] add(Object[] array, Object element) {
  3080. Class type = array != null ? array.getClass() : (element != null ? element.getClass() : Object.class);
  3081. Object[] newArray = (Object[]) copyArrayGrow1(array, type);
  3082. newArray[newArray.length - 1] = element;
  3083. return newArray;
  3084. }
  3085. /**
  3086. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3087. *
  3088. * <p>The new array contains the same elements of the input
  3089. * array plus the given element in the last position. The component type of
  3090. * the new array is the same as that of the input array.</p>
  3091. *
  3092. * <p>If the input array is <code>null</code>, a new one element array is returned
  3093. * whose component type is the same as the element.</p>
  3094. *
  3095. * <pre>
  3096. * ArrayUtils.add(null, true) = [true]
  3097. * ArrayUtils.add([true], false) = [true, false]
  3098. * ArrayUtils.add([true, false], true) = [true, false, true]
  3099. * </pre>
  3100. *
  3101. * @param array the array to copy and add the element to, may be <code>null</code>
  3102. * @param element the object to add at the last index of the new array
  3103. * @return A new array containing the existing elements plus the new element
  3104. * @since 2.1
  3105. */
  3106. public static boolean[] add(boolean[] array, boolean element) {
  3107. boolean[] newArray = (boolean[])copyArrayGrow1(array, Boolean.TYPE);
  3108. newArray[newArray.length - 1] = element;
  3109. return newArray;
  3110. }
  3111. /**
  3112. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3113. *
  3114. * <p>The new array contains the same elements of the input
  3115. * array plus the given element in the last position. The component type of
  3116. * the new array is the same as that of the input array.</p>
  3117. *
  3118. * <p>If the input array is <code>null</code>, a new one element array is returned
  3119. * whose component type is the same as the element.</p>
  3120. *
  3121. * <pre>
  3122. * ArrayUtils.add(null, 0) = [0]
  3123. * ArrayUtils.add([1], 0) = [1, 0]
  3124. * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  3125. * </pre>
  3126. *
  3127. * @param array the array to copy and add the element to, may be <code>null</code>
  3128. * @param element the object to add at the last index of the new array
  3129. * @return A new array containing the existing elements plus the new element
  3130. * @since 2.1
  3131. */
  3132. public static byte[] add(byte[] array, byte element) {
  3133. byte[] newArray = (byte[])copyArrayGrow1(array, Byte.TYPE);
  3134. newArray[newArray.length - 1] = element;
  3135. return newArray;
  3136. }
  3137. /**
  3138. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3139. *
  3140. * <p>The new array contains the same elements of the input
  3141. * array plus the given element in the last position. The component type of
  3142. * the new array is the same as that of the input array.</p>
  3143. *
  3144. * <p>If the input array is <code>null</code>, a new one element array is returned
  3145. * whose component type is the same as the element.</p>
  3146. *
  3147. * <pre>
  3148. * ArrayUtils.add(null, '0') = ['0']
  3149. * ArrayUtils.add(['1'], '0') = ['1', '0']
  3150. * ArrayUtils.add(['1', '0'], '1') = ['1', '0', '1']
  3151. * </pre>
  3152. *
  3153. * @param array the array to copy and add the element to, may be <code>null</code>
  3154. * @param element the object to add at the last index of the new array
  3155. * @return A new array containing the existing elements plus the new element
  3156. * @since 2.1
  3157. */
  3158. public static char[] add(char[] array, char element) {
  3159. char[] newArray = (char[])copyArrayGrow1(array, Character.TYPE);
  3160. newArray[newArray.length - 1] = element;
  3161. return newArray;
  3162. }
  3163. /**
  3164. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3165. *
  3166. * <p>The new array contains the same elements of the input
  3167. * array plus the given element in the last position. The component type of
  3168. * the new array is the same as that of the input array.</p>
  3169. *
  3170. * <p>If the input array is <code>null</code>, a new one element array is returned
  3171. * whose component type is the same as the element.</p>
  3172. *
  3173. * <pre>
  3174. * ArrayUtils.add(null, 0) = [0]
  3175. * ArrayUtils.add([1], 0) = [1, 0]
  3176. * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  3177. * </pre>
  3178. *
  3179. * @param array the array to copy and add the element to, may be <code>null</code>
  3180. * @param element the object to add at the last index of the new array
  3181. * @return A new array containing the existing elements plus the new element
  3182. * @since 2.1
  3183. */
  3184. public static double[] add(double[] array, double element) {
  3185. double[] newArray = (double[])copyArrayGrow1(array, Double.TYPE);
  3186. newArray[newArray.length - 1] = element;
  3187. return newArray;
  3188. }
  3189. /**
  3190. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3191. *
  3192. * <p>The new array contains the same elements of the input
  3193. * array plus the given element in the last position. The component type of
  3194. * the new array is the same as that of the input array.</p>
  3195. *
  3196. * <p>If the input array is <code>null</code>, a new one element array is returned
  3197. * whose component type is the same as the element.</p>
  3198. *
  3199. * <pre>
  3200. * ArrayUtils.add(null, 0) = [0]
  3201. * ArrayUtils.add([1], 0) = [1, 0]
  3202. * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  3203. * </pre>
  3204. *
  3205. * @param array the array to copy and add the element to, may be <code>null</code>
  3206. * @param element the object to add at the last index of the new array
  3207. * @return A new array containing the existing elements plus the new element
  3208. * @since 2.1
  3209. */
  3210. public static float[] add(float[] array, float element) {
  3211. float[] newArray = (float[])copyArrayGrow1(array, Float.TYPE);
  3212. newArray[newArray.length - 1] = element;
  3213. return newArray;
  3214. }
  3215. /**
  3216. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3217. *
  3218. * <p>The new array contains the same elements of the input
  3219. * array plus the given element in the last position. The component type of
  3220. * the new array is the same as that of the input array.</p>
  3221. *
  3222. * <p>If the input array is <code>null</code>, a new one element array is returned
  3223. * whose component type is the same as the element.</p>
  3224. *
  3225. * <pre>
  3226. * ArrayUtils.add(null, 0) = [0]
  3227. * ArrayUtils.add([1], 0) = [1, 0]
  3228. * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  3229. * </pre>
  3230. *
  3231. * @param array the array to copy and add the element to, may be <code>null</code>
  3232. * @param element the object to add at the last index of the new array
  3233. * @return A new array containing the existing elements plus the new element
  3234. * @since 2.1
  3235. */
  3236. public static int[] add(int[] array, int element) {
  3237. int[] newArray = (int[])copyArrayGrow1(array, Integer.TYPE);
  3238. newArray[newArray.length - 1] = element;
  3239. return newArray;
  3240. }
  3241. /**
  3242. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3243. *
  3244. * <p>The new array contains the same elements of the input
  3245. * array plus the given element in the last position. The component type of
  3246. * the new array is the same as that of the input array.</p>
  3247. *
  3248. * <p>If the input array is <code>null</code>, a new one element array is returned
  3249. * whose component type is the same as the element.</p>
  3250. *
  3251. * <pre>
  3252. * ArrayUtils.add(null, 0) = [0]
  3253. * ArrayUtils.add([1], 0) = [1, 0]
  3254. * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  3255. * </pre>
  3256. *
  3257. * @param array the array to copy and add the element to, may be <code>null</code>
  3258. * @param element the object to add at the last index of the new array
  3259. * @return A new array containing the existing elements plus the new element
  3260. * @since 2.1
  3261. */
  3262. public static long[] add(long[] array, long element) {
  3263. long[] newArray = (long[])copyArrayGrow1(array, Long.TYPE);
  3264. newArray[newArray.length - 1] = element;
  3265. return newArray;
  3266. }
  3267. /**
  3268. * <p>Copies the given array and adds the given element at the end of the new array.</p>
  3269. *
  3270. * <p>The new array contains the same elements of the input
  3271. * array plus the given element in the last position. The component type of
  3272. * the new array is the same as that of the input array.</p>
  3273. *
  3274. * <p>If the input array is <code>null</code>, a new one element array is returned
  3275. * whose component type is the same as the element.</p>
  3276. *
  3277. * <pre>
  3278. * ArrayUtils.add(null, 0) = [0]
  3279. * ArrayUtils.add([1], 0) = [1, 0]
  3280. * ArrayUtils.add([1, 0], 1) = [1, 0, 1]
  3281. * </pre>
  3282. *
  3283. * @param array the array to copy and add the element to, may be <code>null</code>
  3284. * @param element the object to add at the last index of the new array
  3285. * @return A new array containing the existing elements plus the new element
  3286. * @since 2.1
  3287. */
  3288. public static short[] add(short[] array, short element) {
  3289. short[] newArray = (short[])copyArrayGrow1(array, Short.TYPE);
  3290. newArray[newArray.length - 1] = element;
  3291. return newArray;
  3292. }
  3293. /**
  3294. * Returns a copy of the given array of size 1 greater than the argument.
  3295. * The last value of the array is left to the default value.
  3296. *
  3297. * @param array The array to copy, must not be <code>null</code>.
  3298. * @param newArrayComponentType If <code>array</code> is <code>null</code>, create a
  3299. * size 1 array of this type.
  3300. * @return A new copy of the array of size 1 greater than the input.
  3301. */
  3302. private static Object copyArrayGrow1(Object array, Class newArrayComponentType) {
  3303. if (array != null) {
  3304. int arrayLength = Array.getLength(array);
  3305. Object newArray = Array.newInstance(array.getClass().getComponentType(), arrayLength + 1);
  3306. System.arraycopy(array, 0, newArray, 0, arrayLength);
  3307. return newArray;
  3308. }
  3309. return Array.newInstance(newArrayComponentType, 1);
  3310. }
  3311. /**
  3312. * <p>Inserts the specified element at the specified position in the array.
  3313. * Shifts the element currently at that position (if any) and any subsequent
  3314. * elements to the right (adds one to their indices).</p>
  3315. *
  3316. * <p>This method returns a new array with the same elements of the input
  3317. * array plus the given element on the specified position. The component
  3318. * type of the returned array is always the same as that of the input
  3319. * array.</p>
  3320. *
  3321. * <p>If the input array is <code>null</code>, a new one element array is returned
  3322. * whose component type is the same as the element.</p>
  3323. *
  3324. * <pre>
  3325. * ArrayUtils.add(null, 0, null) = [null]
  3326. * ArrayUtils.add(null, 0, "a") = ["a"]
  3327. * ArrayUtils.add(["a"], 1, null) = ["a", null]
  3328. * ArrayUtils.add(["a"], 1, "b") = ["a", "b"]
  3329. * ArrayUtils.add(["a", "b"], 3, "c") = ["a", "b", "c"]
  3330. * </pre>
  3331. *
  3332. * @param array the array to add the element to, may be <code>null</code>
  3333. * @param index the position of the new object
  3334. * @param element the object to add
  3335. * @return A new array containing the existing elements and the new element
  3336. * @throws IndexOutOfBoundsException if the index is out of range
  3337. * (index < 0 || index > array.length).
  3338. */
  3339. public static Object[] add(Object[] array, int index, Object element) {
  3340. Class clss = null;
  3341. if (array != null) {
  3342. clss = array.getClass().getComponentType();
  3343. } else if (element != null) {
  3344. clss = element.getClass();
  3345. } else {
  3346. return new Object[]{null};
  3347. }
  3348. return (Object[]) add(array, index, element, clss);
  3349. }
  3350. /**
  3351. * <p>Inserts the specified element at the specified position in the array.
  3352. * Shifts the element currently at that position (if any) and any subsequent
  3353. * elements to the right (adds one to their indices).</p>
  3354. *
  3355. * <p>This method returns a new array with the same elements of the input
  3356. * array plus the given element on the specified position. The component
  3357. * type of the returned array is always the same as that of the input
  3358. * array.</p>
  3359. *
  3360. * <p>If the input array is <code>null</code>, a new one element array is returned
  3361. * whose component type is the same as the element.</p>
  3362. *
  3363. * <pre>
  3364. * ArrayUtils.add(null, 0, true) = [true]
  3365. * ArrayUtils.add([true], 0, false) = [false, true]
  3366. * ArrayUtils.add([false], 1, true) = [false, true]
  3367. * ArrayUtils.add([true, false], 1, true) = [true, true, false]
  3368. * </pre>
  3369. *
  3370. * @param array the array to add the element to, may be <code>null</code>
  3371. * @param index the position of the new object
  3372. * @param element the object to add
  3373. * @return A new array containing the existing elements and the new element
  3374. * @throws IndexOutOfBoundsException if the index is out of range
  3375. * (index < 0 || index > array.length).
  3376. */
  3377. public static boolean[] add(boolean[] array, int index, boolean element) {
  3378. return (boolean[]) add(array, index, BooleanUtils.toBooleanObject(element), Boolean.TYPE);
  3379. }
  3380. /**
  3381. * <p>Inserts the specified element at the specified position in the array.
  3382. * Shifts the element currently at that position (if any) and any subsequent
  3383. * elements to the right (adds one to their indices).</p>
  3384. *
  3385. * <p>This method returns a new array with the same elements of the input
  3386. * array plus the given element on the specified position. The component
  3387. * type of the returned array is always the same as that of the input
  3388. * array.</p>
  3389. *
  3390. * <p>If the input array is <code>null</code>, a new one element array is returned
  3391. * whose component type is the same as the element.</p>
  3392. *
  3393. * <pre>
  3394. * ArrayUtils.add(null, 0, 'a') = ['a']
  3395. * ArrayUtils.add(['a'], 0, 'b') = ['b', 'a']
  3396. * ArrayUtils.add(['a', 'b'], 0, 'c') = ['c', 'a', 'b']
  3397. * ArrayUtils.add(['a', 'b'], 1, 'k') = ['a', 'k', 'b']
  3398. * ArrayUtils.add(['a', 'b', 'c'], 1, 't') = ['a', 't', 'b', 'c']
  3399. * </pre>
  3400. *
  3401. * @param array the array to add the element to, may be <code>null</code>
  3402. * @param index the position of the new object
  3403. * @param element the object to add
  3404. * @return A new array containing the existing elements and the new element
  3405. * @throws IndexOutOfBoundsException if the index is out of range
  3406. * (index < 0 || index > array.length).
  3407. */
  3408. public static char[] add(char[] array, int index, char element) {
  3409. return (char[]) add(array, index, new Character(element), Character.TYPE);
  3410. }
  3411. /**
  3412. * <p>Inserts the specified element at the specified position in the array.
  3413. * Shifts the element currently at that position (if any) and any subsequent
  3414. * elements to the right (adds one to their indices).</p>
  3415. *
  3416. * <p>This method returns a new array with the same elements of the input
  3417. * array plus the given element on the specified position. The component
  3418. * type of the returned array is always the same as that of the input
  3419. * array.</p>
  3420. *
  3421. * <p>If the input array is <code>null</code>, a new one element array is returned
  3422. * whose component type is the same as the element.</p>
  3423. *
  3424. * <pre>
  3425. * ArrayUtils.add([1], 0, 2) = [2, 1]
  3426. * ArrayUtils.add([2, 6], 2, 3) = [2, 6, 3]
  3427. * ArrayUtils.add([2, 6], 0, 1) = [1, 2, 6]
  3428. * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
  3429. * </pre>
  3430. *
  3431. * @param array the array to add the element to, may be <code>null</code>
  3432. * @param index the position of the new object
  3433. * @param element the object to add
  3434. * @return A new array containing the existing elements and the new element
  3435. * @throws IndexOutOfBoundsException if the index is out of range
  3436. * (index < 0 || index > array.length).
  3437. */
  3438. public static byte[] add(byte[] array, int index, byte element) {
  3439. return (byte[]) add(array, index, new Byte(element), Byte.TYPE);
  3440. }
  3441. /**
  3442. * <p>Inserts the specified element at the specified position in the array.
  3443. * Shifts the element currently at that position (if any) and any subsequent
  3444. * elements to the right (adds one to their indices).</p>
  3445. *
  3446. * <p>This method returns a new array with the same elements of the input
  3447. * array plus the given element on the specified position. The component
  3448. * type of the returned array is always the same as that of the input
  3449. * array.</p>
  3450. *
  3451. * <p>If the input array is <code>null</code>, a new one element array is returned
  3452. * whose component type is the same as the element.</p>
  3453. *
  3454. * <pre>
  3455. * ArrayUtils.add([1], 0, 2) = [2, 1]
  3456. * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
  3457. * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
  3458. * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
  3459. * </pre>
  3460. *
  3461. * @param array the array to add the element to, may be <code>null</code>
  3462. * @param index the position of the new object
  3463. * @param element the object to add
  3464. * @return A new array containing the existing elements and the new element
  3465. * @throws IndexOutOfBoundsException if the index is out of range
  3466. * (index < 0 || index > array.length).
  3467. */
  3468. public static short[] add(short[] array, int index, short element) {
  3469. return (short[]) add(array, index, new Short(element), Short.TYPE);
  3470. }
  3471. /**
  3472. * <p>Inserts the specified element at the specified position in the array.
  3473. * Shifts the element currently at that position (if any) and any subsequent
  3474. * elements to the right (adds one to their indices).</p>
  3475. *
  3476. * <p>This method returns a new array with the same elements of the input
  3477. * array plus the given element on the specified position. The component
  3478. * type of the returned array is always the same as that of the input
  3479. * array.</p>
  3480. *
  3481. * <p>If the input array is <code>null</code>, a new one element array is returned
  3482. * whose component type is the same as the element.</p>
  3483. *
  3484. * <pre>
  3485. * ArrayUtils.add([1], 0, 2) = [2, 1]
  3486. * ArrayUtils.add([2, 6], 2, 10) = [2, 6, 10]
  3487. * ArrayUtils.add([2, 6], 0, -4) = [-4, 2, 6]
  3488. * ArrayUtils.add([2, 6, 3], 2, 1) = [2, 6, 1, 3]
  3489. * </pre>
  3490. *
  3491. * @param array the array to add the element to, may be <code>null</code>
  3492. * @param index the position of the new object
  3493. * @param element the object to add
  3494. * @return A new array containing the existing elements and the new element
  3495. * @throws IndexOutOfBoundsException if the index is out of range
  3496. * (index < 0 || index > array.length).
  3497. */
  3498. public static int[] add(int[] array, int index, int element) {
  3499. return (int[]) add(array, index, new Integer(element), Integer.TYPE);
  3500. }
  3501. /**
  3502. * <p>Inserts the specified element at the specified position in the array.
  3503. * Shifts the element currently at that position (if any) and any subsequent
  3504. * elements to the right (adds one to their indices).</p>
  3505. *
  3506. * <p>This method returns a new array with the same elements of the input
  3507. * array plus the given element on the specified position. The component
  3508. * type of the returned array is always the same as that of the input
  3509. * array.</p>
  3510. *
  3511. * <p>If the input array is <code>null</code>, a new one element array is returned
  3512. * whose component type is the same as the element.</p>
  3513. *
  3514. * <pre>
  3515. * ArrayUtils.add([1L], 0, 2L) = [2L, 1L]
  3516. * ArrayUtils.add([2L, 6L], 2, 10L) = [2L, 6L, 10L]
  3517. * ArrayUtils.add([2L, 6L], 0, -4L) = [-4L, 2L, 6L]
  3518. * ArrayUtils.add([2L, 6L, 3L], 2, 1L) = [2L, 6L, 1L, 3L]
  3519. * </pre>
  3520. *
  3521. * @param array the array to add the element to, may be <code>null</code>
  3522. * @param index the position of the new object
  3523. * @param element the object to add
  3524. * @return A new array containing the existing elements and the new element
  3525. * @throws IndexOutOfBoundsException if the index is out of range
  3526. * (index < 0 || index > array.length).
  3527. */
  3528. public static long[] add(long[] array, int index, long element) {
  3529. return (long[]) add(array, index, new Long(element), Long.TYPE);
  3530. }
  3531. /**
  3532. * <p>Inserts the specified element at the specified position in the array.
  3533. * Shifts the element currently at that position (if any) and any subsequent
  3534. * elements to the right (adds one to their indices).</p>
  3535. *
  3536. * <p>This method returns a new array with the same elements of the input
  3537. * array plus the given element on the specified position. The component
  3538. * type of the returned array is always the same as that of the input
  3539. * array.</p>
  3540. *
  3541. * <p>If the input array is <code>null</code>, a new one element array is returned
  3542. * whose component type is the same as the element.</p>
  3543. *
  3544. * <pre>
  3545. * ArrayUtils.add([1.1f], 0, 2.2f) = [2.2f, 1.1f]
  3546. * ArrayUtils.add([2.3f, 6.4f], 2, 10.5f) = [2.3f, 6.4f, 10.5f]
  3547. * ArrayUtils.add([2.6f, 6.7f], 0, -4.8f) = [-4.8f, 2.6f, 6.7f]
  3548. * ArrayUtils.add([2.9f, 6.0f, 0.3f], 2, 1.0f) = [2.9f, 6.0f, 1.0f, 0.3f]
  3549. * </pre>
  3550. *
  3551. * @param array the array to add the element to, may be <code>null</code>
  3552. * @param index the position of the new object
  3553. * @param element the object to add
  3554. * @return A new array containing the existing elements and the new element
  3555. * @throws IndexOutOfBoundsException if the index is out of range
  3556. * (index < 0 || index > array.length).
  3557. */
  3558. public static float[] add(float[] array, int index, float element) {
  3559. return (float[]) add(array, index, new Float(element), Float.TYPE);
  3560. }
  3561. /**
  3562. * <p>Inserts the specified element at the specified position in the array.
  3563. * Shifts the element currently at that position (if any) and any subsequent
  3564. * elements to the right (adds one to their indices).</p>
  3565. *
  3566. * <p>This method returns a new array with the same elements of the input
  3567. * array plus the given element on the specified position. The component
  3568. * type of the returned array is always the same as that of the input
  3569. * array.</p>
  3570. *
  3571. * <p>If the input array is <code>null</code>, a new one element array is returned
  3572. * whose component type is the same as the element.</p>
  3573. *
  3574. * <pre>
  3575. * ArrayUtils.add([1.1], 0, 2.2) = [2.2, 1.1]
  3576. * ArrayUtils.add([2.3, 6.4], 2, 10.5) = [2.3, 6.4, 10.5]
  3577. * ArrayUtils.add([2.6, 6.7], 0, -4.8) = [-4.8, 2.6, 6.7]
  3578. * ArrayUtils.add([2.9, 6.0, 0.3], 2, 1.0) = [2.9, 6.0, 1.0, 0.3]
  3579. * </pre>
  3580. *
  3581. * @param array the array to add the element to, may be <code>null</code>
  3582. * @param index the position of the new object
  3583. * @param element the object to add
  3584. * @return A new array containing the existing elements and the new element
  3585. * @throws IndexOutOfBoundsException if the index is out of range
  3586. * (index < 0 || index > array.length).
  3587. */
  3588. public static double[] add(double[] array, int index, double element) {
  3589. return (double[]) add(array, index, new Double(element), Double.TYPE);
  3590. }
  3591. /**
  3592. * Underlying implementation of add(array, index, element) methods.
  3593. * The last parameter is the class, which may not equal element.getClass
  3594. * for primitives.
  3595. *
  3596. * @param array the array to add the element to, may be <code>null</code>
  3597. * @param index the position of the new object
  3598. * @param element the object to add
  3599. * @param clss the type of the element being added
  3600. * @return A new array containing the existing elements and the new element
  3601. */
  3602. private static Object add(Object array, int index, Object element, Class clss) {
  3603. if (array == null) {
  3604. if (index != 0) {
  3605. throw new IndexOutOfBoundsException("Index: " + index + ", Length: 0");
  3606. }
  3607. Object joinedArray = Array.newInstance(clss, 1);
  3608. Array.set(joinedArray, 0, element);
  3609. return joinedArray;
  3610. }
  3611. int length = Array.getLength(array);
  3612. if (index > length || index < 0) {
  3613. throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
  3614. }
  3615. Object result = Array.newInstance(clss, length + 1);
  3616. System.arraycopy(array, 0, result, 0, index);
  3617. Array.set(result, index, element);
  3618. if (index < length) {
  3619. System.arraycopy(array, index, result, index + 1, length - index);
  3620. }
  3621. return result;
  3622. }
  3623. /**
  3624. * <p>Removes the element at the specified position from the specified array.
  3625. * All subsequent elements are shifted to the left (substracts one from
  3626. * their indices).</p>
  3627. *
  3628. * <p>This method returns a new array with the same elements of the input
  3629. * array except the element on the specified position. The component
  3630. * type of the returned array is always the same as that of the input
  3631. * array.</p>
  3632. *
  3633. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  3634. * will be thrown, because in that case no valid index can be specified.</p>
  3635. *
  3636. * <pre>
  3637. * ArrayUtils.remove(["a"], 0) = []
  3638. * ArrayUtils.remove(["a", "b"], 0) = ["b"]
  3639. * ArrayUtils.remove(["a", "b"], 1) = ["a"]
  3640. * ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
  3641. * </pre>
  3642. *
  3643. * @param array the array to remove the element from, may not be <code>null</code>
  3644. * @param index the position of the element to be removed
  3645. * @return A new array containing the existing elements except the element
  3646. * at the specified position.
  3647. * @throws IndexOutOfBoundsException if the index is out of range
  3648. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  3649. * @since 2.1
  3650. */
  3651. public static Object[] remove(Object[] array, int index) {
  3652. return (Object[]) remove((Object) array, index);
  3653. }
  3654. /**
  3655. * <p>Removes the first occurrence of the specified element from the
  3656. * specified array. All subsequent elements are shifted to the left
  3657. * (substracts one from their indices). If the array doesn't contains
  3658. * such an element, no elements are removed from the array.</p>
  3659. *
  3660. * <p>This method returns a new array with the same elements of the input
  3661. * array except the first occurrence of the specified element. The component
  3662. * type of the returned array is always the same as that of the input
  3663. * array.</p>
  3664. *
  3665. * <pre>
  3666. * ArrayUtils.removeElement(null, "a") = null
  3667. * ArrayUtils.removeElement([], "a") = []
  3668. * ArrayUtils.removeElement(["a"], "b") = ["a"]
  3669. * ArrayUtils.removeElement(["a", "b"], "a") = ["b"]
  3670. * ArrayUtils.removeElement(["a", "b", "a"], "a") = ["b", "a"]
  3671. * </pre>
  3672. *
  3673. * @param array the array to remove the element from, may be <code>null</code>
  3674. * @param element the element to be removed
  3675. * @return A new array containing the existing elements except the first
  3676. * occurrence of the specified element.
  3677. * @since 2.1
  3678. */
  3679. public static Object[] removeElement(Object[] array, Object element) {
  3680. int index = indexOf(array, element);
  3681. if (index == INDEX_NOT_FOUND) {
  3682. return clone(array);
  3683. }
  3684. return remove(array, index);
  3685. }
  3686. /**
  3687. * <p>Removes the element at the specified position from the specified array.
  3688. * All subsequent elements are shifted to the left (substracts one from
  3689. * their indices).</p>
  3690. *
  3691. * <p>This method returns a new array with the same elements of the input
  3692. * array except the element on the specified position. The component
  3693. * type of the returned array is always the same as that of the input
  3694. * array.</p>
  3695. *
  3696. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  3697. * will be thrown, because in that case no valid index can be specified.</p>
  3698. *
  3699. * <pre>
  3700. * ArrayUtils.remove([true], 0) = []
  3701. * ArrayUtils.remove([true, false], 0) = [false]
  3702. * ArrayUtils.remove([true, false], 1) = [true]
  3703. * ArrayUtils.remove([true, true, false], 1) = [true, false]
  3704. * </pre>
  3705. *
  3706. * @param array the array to remove the element from, may not be <code>null</code>
  3707. * @param index the position of the element to be removed
  3708. * @return A new array containing the existing elements except the element
  3709. * at the specified position.
  3710. * @throws IndexOutOfBoundsException if the index is out of range
  3711. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  3712. * @since 2.1
  3713. */
  3714. public static boolean[] remove(boolean[] array, int index) {
  3715. return (boolean[]) remove((Object) array, index);
  3716. }
  3717. /**
  3718. * <p>Removes the first occurrence of the specified element from the
  3719. * specified array. All subsequent elements are shifted to the left
  3720. * (substracts one from their indices). If the array doesn't contains
  3721. * such an element, no elements are removed from the array.</p>
  3722. *
  3723. * <p>This method returns a new array with the same elements of the input
  3724. * array except the first occurrence of the specified element. The component
  3725. * type of the returned array is always the same as that of the input
  3726. * array.</p>
  3727. *
  3728. * <pre>
  3729. * ArrayUtils.removeElement(null, true) = null
  3730. * ArrayUtils.removeElement([], true) = []
  3731. * ArrayUtils.removeElement([true], false) = [true]
  3732. * ArrayUtils.removeElement([true, false], false) = [true]
  3733. * ArrayUtils.removeElement([true, false, true], true) = [false, true]
  3734. * </pre>
  3735. *
  3736. * @param array the array to remove the element from, may be <code>null</code>
  3737. * @param element the element to be removed
  3738. * @return A new array containing the existing elements except the first
  3739. * occurrence of the specified element.
  3740. * @since 2.1
  3741. */
  3742. public static boolean[] removeElement(boolean[] array, boolean element) {
  3743. int index = indexOf(array, element);
  3744. if (index == INDEX_NOT_FOUND) {
  3745. return clone(array);
  3746. }
  3747. return remove(array, index);
  3748. }
  3749. /**
  3750. * <p>Removes the element at the specified position from the specified array.
  3751. * All subsequent elements are shifted to the left (substracts one from
  3752. * their indices).</p>
  3753. *
  3754. * <p>This method returns a new array with the same elements of the input
  3755. * array except the element on the specified position. The component
  3756. * type of the returned array is always the same as that of the input
  3757. * array.</p>
  3758. *
  3759. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  3760. * will be thrown, because in that case no valid index can be specified.</p>
  3761. *
  3762. * <pre>
  3763. * ArrayUtils.remove([1], 0) = []
  3764. * ArrayUtils.remove([1, 0], 0) = [0]
  3765. * ArrayUtils.remove([1, 0], 1) = [1]
  3766. * ArrayUtils.remove([1, 0, 1], 1) = [1, 1]
  3767. * </pre>
  3768. *
  3769. * @param array the array to remove the element from, may not be <code>null</code>
  3770. * @param index the position of the element to be removed
  3771. * @return A new array containing the existing elements except the element
  3772. * at the specified position.
  3773. * @throws IndexOutOfBoundsException if the index is out of range
  3774. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  3775. * @since 2.1
  3776. */
  3777. public static byte[] remove(byte[] array, int index) {
  3778. return (byte[]) remove((Object) array, index);
  3779. }
  3780. /**
  3781. * <p>Removes the first occurrence of the specified element from the
  3782. * specified array. All subsequent elements are shifted to the left
  3783. * (substracts one from their indices). If the array doesn't contains
  3784. * such an element, no elements are removed from the array.</p>
  3785. *
  3786. * <p>This method returns a new array with the same elements of the input
  3787. * array except the first occurrence of the specified element. The component
  3788. * type of the returned array is always the same as that of the input
  3789. * array.</p>
  3790. *
  3791. * <pre>
  3792. * ArrayUtils.removeElement(null, 1) = null
  3793. * ArrayUtils.removeElement([], 1) = []
  3794. * ArrayUtils.removeElement([1], 0) = [1]
  3795. * ArrayUtils.removeElement([1, 0], 0) = [1]
  3796. * ArrayUtils.removeElement([1, 0, 1], 1) = [0, 1]
  3797. * </pre>
  3798. *
  3799. * @param array the array to remove the element from, may be <code>null</code>
  3800. * @param element the element to be removed
  3801. * @return A new array containing the existing elements except the first
  3802. * occurrence of the specified element.
  3803. * @since 2.1
  3804. */
  3805. public static byte[] removeElement(byte[] array, byte element) {
  3806. int index = indexOf(array, element);
  3807. if (index == INDEX_NOT_FOUND) {
  3808. return clone(array);
  3809. }
  3810. return remove(array, index);
  3811. }
  3812. /**
  3813. * <p>Removes the element at the specified position from the specified array.
  3814. * All subsequent elements are shifted to the left (substracts one from
  3815. * their indices).</p>
  3816. *
  3817. * <p>This method returns a new array with the same elements of the input
  3818. * array except the element on the specified position. The component
  3819. * type of the returned array is always the same as that of the input
  3820. * array.</p>
  3821. *
  3822. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  3823. * will be thrown, because in that case no valid index can be specified.</p>
  3824. *
  3825. * <pre>
  3826. * ArrayUtils.remove(['a'], 0) = []
  3827. * ArrayUtils.remove(['a', 'b'], 0) = ['b']
  3828. * ArrayUtils.remove(['a', 'b'], 1) = ['a']
  3829. * ArrayUtils.remove(['a', 'b', 'c'], 1) = ['a', 'c']
  3830. * </pre>
  3831. *
  3832. * @param array the array to remove the element from, may not be <code>null</code>
  3833. * @param index the position of the element to be removed
  3834. * @return A new array containing the existing elements except the element
  3835. * at the specified position.
  3836. * @throws IndexOutOfBoundsException if the index is out of range
  3837. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  3838. * @since 2.1
  3839. */
  3840. public static char[] remove(char[] array, int index) {
  3841. return (char[]) remove((Object) array, index);
  3842. }
  3843. /**
  3844. * <p>Removes the first occurrence of the specified element from the
  3845. * specified array. All subsequent elements are shifted to the left
  3846. * (substracts one from their indices). If the array doesn't contains
  3847. * such an element, no elements are removed from the array.</p>
  3848. *
  3849. * <p>This method returns a new array with the same elements of the input
  3850. * array except the first occurrence of the specified element. The component
  3851. * type of the returned array is always the same as that of the input
  3852. * array.</p>
  3853. *
  3854. * <pre>
  3855. * ArrayUtils.removeElement(null, 'a') = null
  3856. * ArrayUtils.removeElement([], 'a') = []
  3857. * ArrayUtils.removeElement(['a'], 'b') = ['a']
  3858. * ArrayUtils.removeElement(['a', 'b'], 'a') = ['b']
  3859. * ArrayUtils.removeElement(['a', 'b', 'a'], 'a') = ['b', 'a']
  3860. * </pre>
  3861. *
  3862. * @param array the array to remove the element from, may be <code>null</code>
  3863. * @param element the element to be removed
  3864. * @return A new array containing the existing elements except the first
  3865. * occurrence of the specified element.
  3866. * @since 2.1
  3867. */
  3868. public static char[] removeElement(char[] array, char element) {
  3869. int index = indexOf(array, element);
  3870. if (index == INDEX_NOT_FOUND) {
  3871. return clone(array);
  3872. }
  3873. return remove(array, index);
  3874. }
  3875. /**
  3876. * <p>Removes the element at the specified position from the specified array.
  3877. * All subsequent elements are shifted to the left (substracts one from
  3878. * their indices).</p>
  3879. *
  3880. * <p>This method returns a new array with the same elements of the input
  3881. * array except the element on the specified position. The component
  3882. * type of the returned array is always the same as that of the input
  3883. * array.</p>
  3884. *
  3885. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  3886. * will be thrown, because in that case no valid index can be specified.</p>
  3887. *
  3888. * <pre>
  3889. * ArrayUtils.remove([1.1], 0) = []
  3890. * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
  3891. * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
  3892. * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
  3893. * </pre>
  3894. *
  3895. * @param array the array to remove the element from, may not be <code>null</code>
  3896. * @param index the position of the element to be removed
  3897. * @return A new array containing the existing elements except the element
  3898. * at the specified position.
  3899. * @throws IndexOutOfBoundsException if the index is out of range
  3900. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  3901. * @since 2.1
  3902. */
  3903. public static double[] remove(double[] array, int index) {
  3904. return (double[]) remove((Object) array, index);
  3905. }
  3906. /**
  3907. * <p>Removes the first occurrence of the specified element from the
  3908. * specified array. All subsequent elements are shifted to the left
  3909. * (substracts one from their indices). If the array doesn't contains
  3910. * such an element, no elements are removed from the array.</p>
  3911. *
  3912. * <p>This method returns a new array with the same elements of the input
  3913. * array except the first occurrence of the specified element. The component
  3914. * type of the returned array is always the same as that of the input
  3915. * array.</p>
  3916. *
  3917. * <pre>
  3918. * ArrayUtils.removeElement(null, 1.1) = null
  3919. * ArrayUtils.removeElement([], 1.1) = []
  3920. * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
  3921. * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
  3922. * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
  3923. * </pre>
  3924. *
  3925. * @param array the array to remove the element from, may be <code>null</code>
  3926. * @param element the element to be removed
  3927. * @return A new array containing the existing elements except the first
  3928. * occurrence of the specified element.
  3929. * @since 2.1
  3930. */
  3931. public static double[] removeElement(double[] array, double element) {
  3932. int index = indexOf(array, element);
  3933. if (index == INDEX_NOT_FOUND) {
  3934. return clone(array);
  3935. }
  3936. return remove(array, index);
  3937. }
  3938. /**
  3939. * <p>Removes the element at the specified position from the specified array.
  3940. * All subsequent elements are shifted to the left (substracts one from
  3941. * their indices).</p>
  3942. *
  3943. * <p>This method returns a new array with the same elements of the input
  3944. * array except the element on the specified position. The component
  3945. * type of the returned array is always the same as that of the input
  3946. * array.</p>
  3947. *
  3948. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  3949. * will be thrown, because in that case no valid index can be specified.</p>
  3950. *
  3951. * <pre>
  3952. * ArrayUtils.remove([1.1], 0) = []
  3953. * ArrayUtils.remove([2.5, 6.0], 0) = [6.0]
  3954. * ArrayUtils.remove([2.5, 6.0], 1) = [2.5]
  3955. * ArrayUtils.remove([2.5, 6.0, 3.8], 1) = [2.5, 3.8]
  3956. * </pre>
  3957. *
  3958. * @param array the array to remove the element from, may not be <code>null</code>
  3959. * @param index the position of the element to be removed
  3960. * @return A new array containing the existing elements except the element
  3961. * at the specified position.
  3962. * @throws IndexOutOfBoundsException if the index is out of range
  3963. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  3964. * @since 2.1
  3965. */
  3966. public static float[] remove(float[] array, int index) {
  3967. return (float[]) remove((Object) array, index);
  3968. }
  3969. /**
  3970. * <p>Removes the first occurrence of the specified element from the
  3971. * specified array. All subsequent elements are shifted to the left
  3972. * (substracts one from their indices). If the array doesn't contains
  3973. * such an element, no elements are removed from the array.</p>
  3974. *
  3975. * <p>This method returns a new array with the same elements of the input
  3976. * array except the first occurrence of the specified element. The component
  3977. * type of the returned array is always the same as that of the input
  3978. * array.</p>
  3979. *
  3980. * <pre>
  3981. * ArrayUtils.removeElement(null, 1.1) = null
  3982. * ArrayUtils.removeElement([], 1.1) = []
  3983. * ArrayUtils.removeElement([1.1], 1.2) = [1.1]
  3984. * ArrayUtils.removeElement([1.1, 2.3], 1.1) = [2.3]
  3985. * ArrayUtils.removeElement([1.1, 2.3, 1.1], 1.1) = [2.3, 1.1]
  3986. * </pre>
  3987. *
  3988. * @param array the array to remove the element from, may be <code>null</code>
  3989. * @param element the element to be removed
  3990. * @return A new array containing the existing elements except the first
  3991. * occurrence of the specified element.
  3992. * @since 2.1
  3993. */
  3994. public static float[] removeElement(float[] array, float element) {
  3995. int index = indexOf(array, element);
  3996. if (index == INDEX_NOT_FOUND) {
  3997. return clone(array);
  3998. }
  3999. return remove(array, index);
  4000. }
  4001. /**
  4002. * <p>Removes the element at the specified position from the specified array.
  4003. * All subsequent elements are shifted to the left (substracts one from
  4004. * their indices).</p>
  4005. *
  4006. * <p>This method returns a new array with the same elements of the input
  4007. * array except the element on the specified position. The component
  4008. * type of the returned array is always the same as that of the input
  4009. * array.</p>
  4010. *
  4011. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  4012. * will be thrown, because in that case no valid index can be specified.</p>
  4013. *
  4014. * <pre>
  4015. * ArrayUtils.remove([1], 0) = []
  4016. * ArrayUtils.remove([2, 6], 0) = [6]
  4017. * ArrayUtils.remove([2, 6], 1) = [2]
  4018. * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
  4019. * </pre>
  4020. *
  4021. * @param array the array to remove the element from, may not be <code>null</code>
  4022. * @param index the position of the element to be removed
  4023. * @return A new array containing the existing elements except the element
  4024. * at the specified position.
  4025. * @throws IndexOutOfBoundsException if the index is out of range
  4026. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  4027. * @since 2.1
  4028. */
  4029. public static int[] remove(int[] array, int index) {
  4030. return (int[]) remove((Object) array, index);
  4031. }
  4032. /**
  4033. * <p>Removes the first occurrence of the specified element from the
  4034. * specified array. All subsequent elements are shifted to the left
  4035. * (substracts one from their indices). If the array doesn't contains
  4036. * such an element, no elements are removed from the array.</p>
  4037. *
  4038. * <p>This method returns a new array with the same elements of the input
  4039. * array except the first occurrence of the specified element. The component
  4040. * type of the returned array is always the same as that of the input
  4041. * array.</p>
  4042. *
  4043. * <pre>
  4044. * ArrayUtils.removeElement(null, 1) = null
  4045. * ArrayUtils.removeElement([], 1) = []
  4046. * ArrayUtils.removeElement([1], 2) = [1]
  4047. * ArrayUtils.removeElement([1, 3], 1) = [3]
  4048. * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
  4049. * </pre>
  4050. *
  4051. * @param array the array to remove the element from, may be <code>null</code>
  4052. * @param element the element to be removed
  4053. * @return A new array containing the existing elements except the first
  4054. * occurrence of the specified element.
  4055. * @since 2.1
  4056. */
  4057. public static int[] removeElement(int[] array, int element) {
  4058. int index = indexOf(array, element);
  4059. if (index == INDEX_NOT_FOUND) {
  4060. return clone(array);
  4061. }
  4062. return remove(array, index);
  4063. }
  4064. /**
  4065. * <p>Removes the element at the specified position from the specified array.
  4066. * All subsequent elements are shifted to the left (substracts one from
  4067. * their indices).</p>
  4068. *
  4069. * <p>This method returns a new array with the same elements of the input
  4070. * array except the element on the specified position. The component
  4071. * type of the returned array is always the same as that of the input
  4072. * array.</p>
  4073. *
  4074. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  4075. * will be thrown, because in that case no valid index can be specified.</p>
  4076. *
  4077. * <pre>
  4078. * ArrayUtils.remove([1], 0) = []
  4079. * ArrayUtils.remove([2, 6], 0) = [6]
  4080. * ArrayUtils.remove([2, 6], 1) = [2]
  4081. * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
  4082. * </pre>
  4083. *
  4084. * @param array the array to remove the element from, may not be <code>null</code>
  4085. * @param index the position of the element to be removed
  4086. * @return A new array containing the existing elements except the element
  4087. * at the specified position.
  4088. * @throws IndexOutOfBoundsException if the index is out of range
  4089. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  4090. * @since 2.1
  4091. */
  4092. public static long[] remove(long[] array, int index) {
  4093. return (long[]) remove((Object) array, index);
  4094. }
  4095. /**
  4096. * <p>Removes the first occurrence of the specified element from the
  4097. * specified array. All subsequent elements are shifted to the left
  4098. * (substracts one from their indices). If the array doesn't contains
  4099. * such an element, no elements are removed from the array.</p>
  4100. *
  4101. * <p>This method returns a new array with the same elements of the input
  4102. * array except the first occurrence of the specified element. The component
  4103. * type of the returned array is always the same as that of the input
  4104. * array.</p>
  4105. *
  4106. * <pre>
  4107. * ArrayUtils.removeElement(null, 1) = null
  4108. * ArrayUtils.removeElement([], 1) = []
  4109. * ArrayUtils.removeElement([1], 2) = [1]
  4110. * ArrayUtils.removeElement([1, 3], 1) = [3]
  4111. * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
  4112. * </pre>
  4113. *
  4114. * @param array the array to remove the element from, may be <code>null</code>
  4115. * @param element the element to be removed
  4116. * @return A new array containing the existing elements except the first
  4117. * occurrence of the specified element.
  4118. * @since 2.1
  4119. */
  4120. public static long[] removeElement(long[] array, long element) {
  4121. int index = indexOf(array, element);
  4122. if (index == INDEX_NOT_FOUND) {
  4123. return clone(array);
  4124. }
  4125. return remove(array, index);
  4126. }
  4127. /**
  4128. * <p>Removes the element at the specified position from the specified array.
  4129. * All subsequent elements are shifted to the left (substracts one from
  4130. * their indices).</p>
  4131. *
  4132. * <p>This method returns a new array with the same elements of the input
  4133. * array except the element on the specified position. The component
  4134. * type of the returned array is always the same as that of the input
  4135. * array.</p>
  4136. *
  4137. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  4138. * will be thrown, because in that case no valid index can be specified.</p>
  4139. *
  4140. * <pre>
  4141. * ArrayUtils.remove([1], 0) = []
  4142. * ArrayUtils.remove([2, 6], 0) = [6]
  4143. * ArrayUtils.remove([2, 6], 1) = [2]
  4144. * ArrayUtils.remove([2, 6, 3], 1) = [2, 3]
  4145. * </pre>
  4146. *
  4147. * @param array the array to remove the element from, may not be <code>null</code>
  4148. * @param index the position of the element to be removed
  4149. * @return A new array containing the existing elements except the element
  4150. * at the specified position.
  4151. * @throws IndexOutOfBoundsException if the index is out of range
  4152. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  4153. * @since 2.1
  4154. */
  4155. public static short[] remove(short[] array, int index) {
  4156. return (short[]) remove((Object) array, index);
  4157. }
  4158. /**
  4159. * <p>Removes the first occurrence of the specified element from the
  4160. * specified array. All subsequent elements are shifted to the left
  4161. * (substracts one from their indices). If the array doesn't contains
  4162. * such an element, no elements are removed from the array.</p>
  4163. *
  4164. * <p>This method returns a new array with the same elements of the input
  4165. * array except the first occurrence of the specified element. The component
  4166. * type of the returned array is always the same as that of the input
  4167. * array.</p>
  4168. *
  4169. * <pre>
  4170. * ArrayUtils.removeElement(null, 1) = null
  4171. * ArrayUtils.removeElement([], 1) = []
  4172. * ArrayUtils.removeElement([1], 2) = [1]
  4173. * ArrayUtils.removeElement([1, 3], 1) = [3]
  4174. * ArrayUtils.removeElement([1, 3, 1], 1) = [3, 1]
  4175. * </pre>
  4176. *
  4177. * @param array the array to remove the element from, may be <code>null</code>
  4178. * @param element the element to be removed
  4179. * @return A new array containing the existing elements except the first
  4180. * occurrence of the specified element.
  4181. * @since 2.1
  4182. */
  4183. public static short[] removeElement(short[] array, short element) {
  4184. int index = indexOf(array, element);
  4185. if (index == INDEX_NOT_FOUND) {
  4186. return clone(array);
  4187. }
  4188. return remove(array, index);
  4189. }
  4190. /**
  4191. * <p>Removes the element at the specified position from the specified array.
  4192. * All subsequent elements are shifted to the left (substracts one from
  4193. * their indices).</p>
  4194. *
  4195. * <p>This method returns a new array with the same elements of the input
  4196. * array except the element on the specified position. The component
  4197. * type of the returned array is always the same as that of the input
  4198. * array.</p>
  4199. *
  4200. * <p>If the input array is <code>null</code>, an IndexOutOfBoundsException
  4201. * will be thrown, because in that case no valid index can be specified.</p>
  4202. *
  4203. * @param array the array to remove the element from, may not be <code>null</code>
  4204. * @param index the position of the element to be removed
  4205. * @return A new array containing the existing elements except the element
  4206. * at the specified position.
  4207. * @throws IndexOutOfBoundsException if the index is out of range
  4208. * (index < 0 || index >= array.length), or if the array is <code>null</code>.
  4209. * @since 2.1
  4210. */
  4211. private static Object remove(Object array, int index) {
  4212. int length = getLength(array);
  4213. if (index < 0 || index >= length) {
  4214. throw new IndexOutOfBoundsException("Index: " + index + ", Length: " + length);
  4215. }
  4216. Object result = Array.newInstance(array.getClass().getComponentType(), length - 1);
  4217. System.arraycopy(array, 0, result, 0, index);
  4218. if (index < length - 1) {
  4219. System.arraycopy(array, index + 1, result, index, length - index - 1);
  4220. }
  4221. return result;
  4222. }
  4223. }