1. /**
  2. * @author Albert Beaupre
  3. */
  4. @SuppressWarnings("unchecked")
  5. public final class ArrayUtility {
  6. /**
  7. * A pseudorandom number generator
  8. **/
  9. private static Random r = new Random();
  10. /**
  11. * Creates an array starting from the specified {@code start} argument and
  12. * ending at the {@code end} argument incrementing at the specified
  13. * {@code increment} argument.
  14. *
  15. * @param start the starting value
  16. * @param end the ending value
  17. * @param increment the value to increment by
  18. * @return an array filled with the values ranging from start to end by the
  19. * increment value
  20. */
  21. public static double[] range(double start, double end, double increment) {
  22. double[] a = new double[(int) (Math.abs(start - end) / increment)];
  23. int index = 0;
  24. double j = start < end ? (increment < 0 ? -increment : increment) : (increment < 0 ? increment : -increment);
  25. for (double i = start; start < end ? i <= end : i >= end; i += j) {
  26. a = Arrays.copyOf(a, index + 1);
  27. a[index++] = i;
  28. }
  29. return a;
  30. }
  31. /**
  32. * Creates an array starting from the specified {@code start} argument and
  33. * ending at the {@code end} argument incrementing at the specified
  34. * {@code increment} argument.
  35. *
  36. * @param start the starting value
  37. * @param end the ending value
  38. * @param increment the value to increment by
  39. * @return an array filled with the values ranging from start to end by the
  40. * increment value
  41. */
  42. public static int[] range(int start, int end, int increment) {
  43. int[] a = new int[Math.abs(start - end) / increment];
  44. int index = 0;
  45. int j = start < end ? (increment < 0 ? -increment : increment) : (increment < 0 ? increment : -increment);
  46. for (int i = start; start < end ? i <= end : i >= end; i += j) {
  47. a = Arrays.copyOf(a, index + 1);
  48. a[index++] = i;
  49. }
  50. return a;
  51. }
  52. /**
  53. * Creates an array starting from the specified {@code start} argument and
  54. * ending at the {@code end} argument incrementing at the specified
  55. * {@code increment} argument.
  56. *
  57. * @param start the starting value
  58. * @param end the ending value
  59. * @param increment the value to increment by
  60. * @return an array filled with the values ranging from start to end by the
  61. * increment value
  62. */
  63. public static float[] range(float start, float end, float increment) {
  64. float[] a = new float[(int) (Math.abs(start - end) / increment)];
  65. int index = 0;
  66. float j = start < end ? (increment < 0 ? -increment : increment) : (increment < 0 ? increment : -increment);
  67. for (float i = start; start < end ? i <= end : i >= end; i += j) {
  68. a = Arrays.copyOf(a, index + 1);
  69. a[index++] = i;
  70. }
  71. return a;
  72. }
  73. /**
  74. * Creates an array starting from the specified {@code start} argument and
  75. * ending at the {@code end} argument incrementing at the specified
  76. * {@code increment} argument.
  77. *
  78. * @param start the starting value
  79. * @param end the ending value
  80. * @param increment the value to increment by
  81. * @return an array filled with the values ranging from start to end by the
  82. * increment value
  83. */
  84. public static short[] range(short start, short end, short increment) {
  85. short[] a = new short[Math.abs(start - end) / increment];
  86. int index = 0;
  87. short j = (short) (start < end ? (increment < 0 ? -increment : increment) : (increment < 0 ? increment : -increment));
  88. for (short i = start; start < end ? i <= end : i >= end; i += j) {
  89. a = Arrays.copyOf(a, index + 1);
  90. a[index++] = i;
  91. }
  92. return a;
  93. }
  94. /**
  95. * Creates an array starting from the specified {@code start} argument and
  96. * ending at the {@code end} argument incrementing at the specified
  97. * {@code increment} argument.
  98. *
  99. * @param start the starting value
  100. * @param end the ending value
  101. * @param increment the value to increment by
  102. * @return an array filled with the values ranging from start to end by the
  103. * increment value
  104. */
  105. public static long[] range(long start, long end, long increment) {
  106. long[] a = new long[(int) (Math.abs(start - end) / increment)];
  107. int index = 0;
  108. long j = start < end ? (increment < 0 ? -increment : increment) : (increment < 0 ? increment : -increment);
  109. for (long i = start; start < end ? i <= end : i >= end; i += j) {
  110. a = Arrays.copyOf(a, index + 1);
  111. a[index++] = i;
  112. }
  113. return a;
  114. }
  115. /**
  116. * Creates an array starting from the specified {@code start} argument and
  117. * ending at the {@code end} argument incrementing at the specified
  118. * {@code increment} argument.
  119. *
  120. * @param start the starting value
  121. * @param end the ending value
  122. * @param increment the value to increment by
  123. * @return an array filled with the values ranging from start to end by the
  124. * increment value
  125. */
  126. public static byte[] range(byte start, byte end, byte increment) {
  127. byte[] a = new byte[Math.abs(start - end) / increment];
  128. int index = 0;
  129. byte j = (byte) (start < end ? (increment < 0 ? -increment : increment) : (increment < 0 ? increment : -increment));
  130. for (byte i = start; start < end ? i <= end : i >= end; i += j) {
  131. a = Arrays.copyOf(a, index + 1);
  132. a[index++] = i;
  133. }
  134. return a;
  135. }
  136. /**
  137. * Shuffles the elements within the specified {@code Array}.
  138. *
  139. * @param a the array to shuffle
  140. * @return the shuffled array
  141. */
  142. public static <T> void shuffle(T[] a) {
  143. for (int i = 0; i < a.length; i++)
  144. swap(i, r.nextInt(a.length), a);
  145. }
  146. /**
  147. * Shuffles the elements within the specified {@code Array}.
  148. *
  149. * @param a the array to shuffle
  150. * @return the shuffled array
  151. */
  152. public static void shuffle(int[] a) {
  153. for (int i = 0; i < a.length; i++)
  154. swap(i, r.nextInt(a.length), a);
  155. }
  156. /**
  157. * Shuffles the elements within the specified {@code Array}.
  158. *
  159. * @param a the array to shuffle
  160. * @return the shuffled array
  161. */
  162. public static void shuffle(short[] a) {
  163. for (int i = 0; i < a.length; i++)
  164. swap(i, r.nextInt(a.length), a);
  165. }
  166. /**
  167. * Shuffles the elements within the specified {@code Array}.
  168. *
  169. * @param a the array to shuffle
  170. * @return the shuffled array
  171. */
  172. public static void shuffle(float[] a) {
  173. for (int i = 0; i < a.length; i++)
  174. swap(i, r.nextInt(a.length), a);
  175. }
  176. /**
  177. * Shuffles the elements within the specified {@code Array}.
  178. *
  179. * @param a the array to shuffle
  180. * @return the shuffled array
  181. */
  182. public static void shuffle(byte[] a) {
  183. for (int i = 0; i < a.length; i++)
  184. swap(i, r.nextInt(a.length), a);
  185. }
  186. /**
  187. * Shuffles the elements within the specified {@code Array}.
  188. *
  189. * @param a the array to shuffle
  190. * @return the shuffled array
  191. */
  192. public static void shuffle(boolean[] a) {
  193. for (int i = 0; i < a.length; i++)
  194. swap(i, r.nextInt(a.length), a);
  195. }
  196. /**
  197. * Shuffles the elements within the specified {@code Array}.
  198. *
  199. * @param a the array to shuffle
  200. * @return the shuffled array
  201. */
  202. public static void shuffle(long[] a) {
  203. for (int i = 0; i < a.length; i++)
  204. swap(i, r.nextInt(a.length), a);
  205. }
  206. /**
  207. * Shuffles the elements within the specified {@code Array}.
  208. *
  209. * @param a the array to shuffle
  210. * @return the shuffled array
  211. */
  212. public static void shuffle(double[] a) {
  213. for (int i = 0; i < a.length; i++)
  214. swap(i, r.nextInt(a.length), a);
  215. }
  216. /**
  217. * Shuffles the elements within the specified {@code Array}.
  218. *
  219. * @param a the array to shuffle
  220. * @return the shuffled array
  221. */
  222. public static void shuffle(char[] a) {
  223. for (int i = 0; i < a.length; i++)
  224. swap(i, r.nextInt(a.length), a);
  225. }
  226. /**
  227. * Swaps the specified indicies {@code i} and {@code j} in the specified
  228. * array.
  229. *
  230. * @param i the first index to swap
  231. * @param j the second index to swap
  232. * @param a the array to swap the indicies
  233. */
  234. public static <T> void swap(int i, int j, T[] a) {
  235. T old = a[i];
  236. a[i] = a[j];
  237. a[j] = old;
  238. }
  239. /**
  240. * Swaps the specified indicies {@code i} and {@code j} in the specified
  241. * array.
  242. *
  243. * @param i the first index to swap
  244. * @param j the second index to swap
  245. * @param a the array to swap the indicies
  246. */
  247. public static void swap(int i, int j, int[] a) {
  248. int old = a[i];
  249. a[i] = a[j];
  250. a[j] = old;
  251. }
  252. /**
  253. * Swaps the specified indicies {@code i} and {@code j} in the specified
  254. * array.
  255. *
  256. * @param i the first index to swap
  257. * @param j the second index to swap
  258. * @param a the array to swap the indicies
  259. */
  260. public static void swap(int i, int j, short[] a) {
  261. short old = a[i];
  262. a[i] = a[j];
  263. a[j] = old;
  264. }
  265. /**
  266. * Swaps the specified indicies {@code i} and {@code j} in the specified
  267. * array.
  268. *
  269. * @param i the first index to swap
  270. * @param j the second index to swap
  271. * @param a the array to swap the indicies
  272. */
  273. public static void swap(int i, int j, float[] a) {
  274. float old = a[i];
  275. a[i] = a[j];
  276. a[j] = old;
  277. }
  278. /**
  279. * Swaps the specified indicies {@code i} and {@code j} in the specified
  280. * array.
  281. *
  282. * @param i the first index to swap
  283. * @param j the second index to swap
  284. * @param a the array to swap the indicies
  285. */
  286. public static void swap(int i, int j, byte[] a) {
  287. byte old = a[i];
  288. a[i] = a[j];
  289. a[j] = old;
  290. }
  291. /**
  292. * Swaps the specified indicies {@code i} and {@code j} in the specified
  293. * array.
  294. *
  295. * @param i the first index to swap
  296. * @param j the second index to swap
  297. * @param a the array to swap the indicies
  298. */
  299. public static void swap(int i, int j, boolean[] a) {
  300. boolean old = a[i];
  301. a[i] = a[j];
  302. a[j] = old;
  303. }
  304. /**
  305. * Swaps the specified indicies {@code i} and {@code j} in the specified
  306. * array.
  307. *
  308. * @param i the first index to swap
  309. * @param j the second index to swap
  310. * @param a the array to swap the indicies
  311. */
  312. public static void swap(int i, int j, long[] a) {
  313. long old = a[i];
  314. a[i] = a[j];
  315. a[j] = old;
  316. }
  317. /**
  318. * Swaps the specified indicies {@code i} and {@code j} in the specified
  319. * array.
  320. *
  321. * @param i the first index to swap
  322. * @param j the second index to swap
  323. * @param a the array to swap the indicies
  324. */
  325. public static void swap(int i, int j, double[] a) {
  326. double old = a[i];
  327. a[i] = a[j];
  328. a[j] = old;
  329. }
  330. /**
  331. * Swaps the specified indicies {@code i} and {@code j} in the specified
  332. * array.
  333. *
  334. * @param i the first index to swap
  335. * @param j the second index to swap
  336. * @param a the array to swap the indicies
  337. */
  338. public static void swap(int i, int j, char[] a) {
  339. char old = a[i];
  340. a[i] = a[j];
  341. a[j] = old;
  342. }
  343. /**
  344. * Returns true if the specified {@code Array} contains the specified
  345. * {@code type} using the specified {@code equals} filter to check if a type
  346. * is similar.
  347. *
  348. * @param a the array to search
  349. * @param t the object to check
  350. * @return true if the array contains the specified type; return false
  351. * otherwise
  352. */
  353. public static <T> boolean contains(T t, T[] a, Filter<T> equals) {
  354. for (Object o : a)
  355. if (o != null && equals.accept((T) o)) return true;
  356. return false;
  357. }
  358. /**
  359. * Returns true if the specified {@code Array} contains the specified
  360. * {@code type}; false is returned otherwise.
  361. *
  362. * @param a the array to search
  363. * @param t the object to check
  364. * @return true if the array contains the specified type; return false
  365. * otherwise
  366. */
  367. public static <T> boolean contains(T t, T[] a) {
  368. for (Object o : a)
  369. if (o != null && o.equals(t)) return true;
  370. return false;
  371. }
  372. /**
  373. * Returns true if the specified {@code Array} contains the specified
  374. * {@code type}; false is returned otherwise.
  375. *
  376. * @param a the array to search
  377. * @param t the object to check
  378. * @return true if the array contains the specified type; return false
  379. * otherwise
  380. */
  381. public static boolean contains(int t, int[] a) {
  382. for (int o : a)
  383. if (o == t) return true;
  384. return false;
  385. }
  386. /**
  387. * Returns true if the specified {@code Array} contains the specified
  388. * {@code type}; false is returned otherwise.
  389. *
  390. * @param a the array to search
  391. * @param t the object to check
  392. * @return true if the array contains the specified type; return false
  393. * otherwise
  394. */
  395. public static boolean contains(short t, short[] a) {
  396. for (short o : a)
  397. if (o == t) return true;
  398. return false;
  399. }
  400. /**
  401. * Returns true if the specified {@code Array} contains the specified
  402. * {@code type}; false is returned otherwise.
  403. *
  404. * @param a the array to search
  405. * @param t the object to check
  406. * @return true if the array contains the specified type; return false
  407. * otherwise
  408. */
  409. public static boolean contains(double t, double[] a) {
  410. for (double o : a)
  411. if (o == t) return true;
  412. return false;
  413. }
  414. /**
  415. * Returns true if the specified {@code Array} contains the specified
  416. * {@code type}; false is returned otherwise.
  417. *
  418. * @param a the array to search
  419. * @param t the object to check
  420. * @return true if the array contains the specified type; return false
  421. * otherwise
  422. */
  423. public static boolean contains(float t, float[] a) {
  424. for (float o : a)
  425. if (o == t) return true;
  426. return false;
  427. }
  428. /**
  429. * Returns true if the specified {@code Array} contains the specified
  430. * {@code type}; false is returned otherwise.
  431. *
  432. * @param a the array to search
  433. * @param t the object to check
  434. * @return true if the array contains the specified type; return false
  435. * otherwise
  436. */
  437. public static boolean contains(byte t, byte[] a) {
  438. for (byte o : a)
  439. if (o == t) return true;
  440. return false;
  441. }
  442. /**
  443. * Returns true if the specified {@code Array} contains the specified
  444. * {@code type}; false is returned otherwise.
  445. *
  446. * @param a the array to search
  447. * @param t the object to check
  448. * @return true if the array contains the specified type; return false
  449. * otherwise
  450. */
  451. public static boolean contains(char t, char[] a) {
  452. for (char o : a)
  453. if (o == t) return true;
  454. return false;
  455. }
  456. /**
  457. * Returns true if the specified {@code Array} contains the specified
  458. * {@code type}; false is returned otherwise.
  459. *
  460. * @param a the array to search
  461. * @param t the object to check
  462. * @return true if the array contains the specified type; return false
  463. * otherwise
  464. */
  465. public static boolean contains(long t, long[] a) {
  466. for (long o : a)
  467. if (o == t) return true;
  468. return false;
  469. }
  470. /**
  471. * Returns true if the specified {@code Array} contains the specified
  472. * {@code type}; false is returned otherwise.
  473. *
  474. * @param a the array to search
  475. * @param t the object to check
  476. * @return true if the array contains the specified type; return false
  477. * otherwise
  478. */
  479. public static boolean contains(boolean t, boolean[] a) {
  480. for (boolean o : a)
  481. if (o == t) return true;
  482. return false;
  483. }
  484. /**
  485. * Returns the index of the specified {@code o} argument within the
  486. * specified {@code a} Array. If there is no index, -1 is returned instead.
  487. *
  488. * @param o the value to search for
  489. * @param a the array to search
  490. * @return the index of the value; return -1 otherwise
  491. */
  492. public static <T> int indexOf(T o, T[] a) {
  493. for (int i = 0; i < a.length; i++)
  494. if (o == a[i]) return i;
  495. return -1;
  496. }
  497. /**
  498. * Returns the index of the specified {@code o} argument within the
  499. * specified {@code a} Array. If there is no index, -1 is returned instead.
  500. *
  501. * @param o the value to search for
  502. * @param a the array to search
  503. * @return the index of the value; return -1 otherwise
  504. */
  505. public static int indexOf(int o, int[] a) {
  506. for (int i = 0; i < a.length; i++)
  507. if (o == a[i]) return i;
  508. return -1;
  509. }
  510. /**
  511. * Returns the index of the specified {@code o} argument within the
  512. * specified {@code a} Array. If there is no index, -1 is returned instead.
  513. *
  514. * @param o the value to search for
  515. * @param a the array to search
  516. * @return the index of the value; return -1 otherwise
  517. */
  518. public static int indexOf(short o, short[] a) {
  519. for (int i = 0; i < a.length; i++)
  520. if (o == a[i]) return i;
  521. return -1;
  522. }
  523. /**
  524. * Returns the index of the specified {@code o} argument within the
  525. * specified {@code a} Array. If there is no index, -1 is returned instead.
  526. *
  527. * @param o the value to search for
  528. * @param a the array to search
  529. * @return the index of the value; return -1 otherwise
  530. */
  531. public static int indexOf(byte o, byte[] a) {
  532. for (int i = 0; i < a.length; i++)
  533. if (o == a[i]) return i;
  534. return -1;
  535. }
  536. /**
  537. * Returns the index of the specified {@code o} argument within the
  538. * specified {@code a} Array. If there is no index, -1 is returned instead.
  539. *
  540. * @param o the value to search for
  541. * @param a the array to search
  542. * @return the index of the value; return -1 otherwise
  543. */
  544. public static int indexOf(float o, float[] a) {
  545. for (int i = 0; i < a.length; i++)
  546. if (o == a[i]) return i;
  547. return -1;
  548. }
  549. /**
  550. * Returns the index of the specified {@code o} argument within the
  551. * specified {@code a} Array. If there is no index, -1 is returned instead.
  552. *
  553. * @param o the value to search for
  554. * @param a the array to search
  555. * @return the index of the value; return -1 otherwise
  556. */
  557. public static int indexOf(double o, double[] a) {
  558. for (int i = 0; i < a.length; i++)
  559. if (o == a[i]) return i;
  560. return -1;
  561. }
  562. /**
  563. * Returns the index of the specified {@code o} argument within the
  564. * specified {@code a} Array. If there is no index, -1 is returned instead.
  565. *
  566. * @param o the value to search for
  567. * @param a the array to search
  568. * @return the index of the value; return -1 otherwise
  569. */
  570. public static int indexOf(long o, long[] a) {
  571. for (int i = 0; i < a.length; i++)
  572. if (o == a[i]) return i;
  573. return -1;
  574. }
  575. /**
  576. * Returns the index of the specified {@code o} argument within the
  577. * specified {@code a} Array. If there is no index, -1 is returned instead.
  578. *
  579. * @param o the value to search for
  580. * @param a the array to search
  581. * @return the index of the value; return -1 otherwise
  582. */
  583. public static int indexOf(char o, char[] a) {
  584. for (int i = 0; i < a.length; i++)
  585. if (o == a[i]) return i;
  586. return -1;
  587. }
  588. /**
  589. * Returns the index of the specified {@code o} argument within the
  590. * specified {@code a} Array. If there is no index, -1 is returned instead.
  591. *
  592. * @param o the value to search for
  593. * @param a the array to search
  594. * @return the index of the value; return -1 otherwise
  595. */
  596. public static int indexOf(boolean o, boolean[] a) {
  597. for (int i = 0; i < a.length; i++)
  598. if (o == a[i]) return i;
  599. return -1;
  600. }
  601. /**
  602. * Returns the {@code Array} with the specified {@code index} removed from
  603. * the specified {@code a}.
  604. *
  605. * @param a the array to remove the index from
  606. * @param index the index to remove
  607. * @return the array with the removed index
  608. */
  609. public static <T> T[] removeIndex(T[] a, int index) {
  610. T[] n = (T[]) Array.newInstance(a[0].getClass(), a.length - 1);
  611. System.arraycopy(a, 0, n, 0, index);
  612. System.arraycopy(a, index + 1, n, index, a.length - index - 1);
  613. return n;
  614. }
  615. /**
  616. * Returns the {@code Array} with the specified {@code index} removed from
  617. * the specified {@code a}.
  618. *
  619. * @param a the array to remove the index from
  620. * @param index the index to remove
  621. * @return the array with the removed index
  622. */
  623. public static byte[] removeIndex(byte[] a, int index) {
  624. byte[] b = new byte[a.length - 1];
  625. System.arraycopy(a, 0, b, 0, index);
  626. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  627. return b;
  628. }
  629. /**
  630. * Returns the {@code Array} with the specified {@code index} removed from
  631. * the specified {@code a}.
  632. *
  633. * @param a the array to remove the index from
  634. * @param index the index to remove
  635. * @return the array with the removed index
  636. */
  637. public static int[] removeIndex(int[] a, int index) {
  638. int[] b = new int[a.length - 1];
  639. System.arraycopy(a, 0, b, 0, index);
  640. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  641. return b;
  642. }
  643. /**
  644. * Returns the {@code Array} with the specified {@code index} removed from
  645. * the specified {@code a}.
  646. *
  647. * @param a the array to remove the index from
  648. * @param index the index to remove
  649. * @return the array with the removed index
  650. */
  651. public static short[] removeIndex(short[] a, int index) {
  652. short[] b = new short[a.length - 1];
  653. System.arraycopy(a, 0, b, 0, index);
  654. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  655. return b;
  656. }
  657. /**
  658. * Returns the {@code Array} with the specified {@code index} removed from
  659. * the specified {@code a}.
  660. *
  661. * @param a the array to remove the index from
  662. * @param index the index to remove
  663. * @return the array with the removed index
  664. */
  665. public static long[] removeIndex(long[] a, int index) {
  666. long[] b = new long[a.length - 1];
  667. System.arraycopy(a, 0, b, 0, index);
  668. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  669. return b;
  670. }
  671. /**
  672. * Returns the {@code Array} with the specified {@code index} removed from
  673. * the specified {@code a}.
  674. *
  675. * @param a the array to remove the index from
  676. * @param index the index to remove
  677. * @return the array with the removed index
  678. */
  679. public static float[] removeIndex(float[] a, int index) {
  680. float[] b = new float[a.length - 1];
  681. System.arraycopy(a, 0, b, 0, index);
  682. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  683. return b;
  684. }
  685. /**
  686. * Returns the {@code Array} with the specified {@code index} removed from
  687. * the specified {@code a}.
  688. *
  689. * @param a the array to remove the index from
  690. * @param index the index to remove
  691. * @return the array with the removed index
  692. */
  693. public static double[] removeIndex(double[] a, int index) {
  694. double[] b = new double[a.length - 1];
  695. System.arraycopy(a, 0, b, 0, index);
  696. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  697. return b;
  698. }
  699. /**
  700. * Returns the {@code Array} with the specified {@code index} removed from
  701. * the specified {@code a}.
  702. *
  703. * @param a the array to remove the index from
  704. * @param index the index to remove
  705. * @return the array with the removed index
  706. */
  707. public static char[] removeIndex(char[] a, int index) {
  708. char[] b = new char[a.length - 1];
  709. System.arraycopy(a, 0, b, 0, index);
  710. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  711. return b;
  712. }
  713. /**
  714. * Returns the {@code Array} with the specified {@code index} removed from
  715. * the specified {@code a}.
  716. *
  717. * @param a the array to remove the index from
  718. * @param index the index to remove
  719. * @return the array with the removed index
  720. */
  721. public static boolean[] removeIndex(boolean[] a, int index) {
  722. boolean[] b = new boolean[a.length - 1];
  723. System.arraycopy(a, 0, b, 0, index);
  724. System.arraycopy(a, index + 1, b, index, a.length - index - 1);
  725. return b;
  726. }
  727. /**
  728. * Returns the {@code Array} with the specified {@code element} removed from
  729. * the specified {@code a}.
  730. *
  731. * @param a the array to remove the element from
  732. * @param element the element to remove
  733. * @return the array with the removed element
  734. */
  735. public static <T> T[] removeElement(T[] a, T element) {
  736. return removeIndex(a, ArrayUtility.indexOf(element, a));
  737. }
  738. /**
  739. * Returns the {@code Array} with the specified {@code element} removed from
  740. * the specified {@code a}.
  741. *
  742. * @param a the array to remove the element from
  743. * @param element the element to remove
  744. * @return the array with the removed element
  745. */
  746. public static boolean[] removeElement(boolean[] a, boolean element) {
  747. return removeIndex(a, ArrayUtility.indexOf(element, a));
  748. }
  749. /**
  750. * Returns the {@code Array} with the specified {@code element} removed from
  751. * the specified {@code a}.
  752. *
  753. * @param a the array to remove the element from
  754. * @param element the element to remove
  755. * @return the array with the removed element
  756. */
  757. public static byte[] removeElement(byte[] a, byte element) {
  758. return removeIndex(a, ArrayUtility.indexOf(element, a));
  759. }
  760. /**
  761. * Returns the {@code Array} with the specified {@code element} removed from
  762. * the specified {@code a}.
  763. *
  764. * @param a the array to remove the element from
  765. * @param element the element to remove
  766. * @return the array with the removed element
  767. */
  768. public static char[] removeElement(char[] a, char element) {
  769. return removeIndex(a, ArrayUtility.indexOf(element, a));
  770. }
  771. /**
  772. * Returns the {@code Array} with the specified {@code element} removed from
  773. * the specified {@code a}.
  774. *
  775. * @param a the array to remove the element from
  776. * @param element the element to remove
  777. * @return the array with the removed element
  778. */
  779. public static float[] removeElement(float[] a, float element) {
  780. return removeIndex(a, ArrayUtility.indexOf(element, a));
  781. }
  782. /**
  783. * Returns the {@code Array} with the specified {@code element} removed from
  784. * the specified {@code a}.
  785. *
  786. * @param a the array to remove the element from
  787. * @param element the element to remove
  788. * @return the array with the removed element
  789. */
  790. public static double[] removeElement(double[] a, double element) {
  791. return removeIndex(a, ArrayUtility.indexOf(element, a));
  792. }
  793. /**
  794. * Returns the {@code Array} with the specified {@code element} removed from
  795. * the specified {@code a}.
  796. *
  797. * @param a the array to remove the element from
  798. * @param element the element to remove
  799. * @return the array with the removed element
  800. */
  801. public static long[] removeElement(long[] a, long element) {
  802. return removeIndex(a, ArrayUtility.indexOf(element, a));
  803. }
  804. /**
  805. * Returns the {@code Array} with the specified {@code element} removed from
  806. * the specified {@code a}.
  807. *
  808. * @param a the array to remove the element from
  809. * @param element the element to remove
  810. * @return the array with the removed element
  811. */
  812. public static short[] removeElement(short[] a, short element) {
  813. return removeIndex(a, ArrayUtility.indexOf(element, a));
  814. }
  815. /**
  816. * Returns the {@code Array} with the specified {@code element} removed from
  817. * the specified {@code a}.
  818. *
  819. * @param a the array to remove the element from
  820. * @param element the element to remove
  821. * @return the array with the removed element
  822. */
  823. public static int[] removeElement(int[] a, int element) {
  824. return removeIndex(a, ArrayUtility.indexOf(element, a));
  825. }
  826. /**
  827. * Returns the {@code Array} with the specified {@code element} inserted at
  828. * the end of the specified {@code a}
  829. *
  830. * @param a the array to add the element to
  831. * @param element the element to add
  832. * @return the array with the added element
  833. */
  834. public static <T> T[] addElement(T[] a, T... element) {
  835. T[] b = Arrays.copyOf(a, a.length + element.length);
  836. for (int i = element.length - 1; i >= 0; i--)
  837. b[b.length - (i + 1)] = element[i];
  838. return b;
  839. }
  840. /**
  841. * Returns the {@code Array} with the specified {@code element} inserted at
  842. * the end of the specified {@code a}
  843. *
  844. * @param a the array to add the element to
  845. * @param element the element to add
  846. * @return the array with the added element
  847. */
  848. public static int[] addElement(int[] a, int... element) {
  849. int[] b = Arrays.copyOf(a, a.length + element.length);
  850. for (int i = element.length - 1; i >= 0; i--)
  851. b[b.length - (i + 1)] = element[i];
  852. return b;
  853. }
  854. /**
  855. * Returns the {@code Array} with the specified {@code element} inserted at
  856. * the end of the specified {@code a}
  857. *
  858. * @param a the array to add the element to
  859. * @param element the element to add
  860. * @return the array with the added element
  861. */
  862. public static short[] addElement(short[] a, short... element) {
  863. short[] b = Arrays.copyOf(a, a.length + element.length);
  864. for (int i = element.length - 1; i >= 0; i--)
  865. b[b.length - (i + 1)] = element[i];
  866. return b;
  867. }
  868. /**
  869. * Returns the {@code Array} with the specified {@code element} inserted at
  870. * the end of the specified {@code a}
  871. *
  872. * @param a the array to add the element to
  873. * @param element the element to add
  874. * @return the array with the added element
  875. */
  876. public static long[] addElement(long[] a, long... element) {
  877. long[] b = Arrays.copyOf(a, a.length + element.length);
  878. for (int i = element.length - 1; i >= 0; i--)
  879. b[b.length - (i + 1)] = element[i];
  880. return b;
  881. }
  882. /**
  883. * Returns the {@code Array} with the specified {@code element} inserted at
  884. * the end of the specified {@code a}
  885. *
  886. * @param a the array to add the element to
  887. * @param element the element to add
  888. * @return the array with the added element
  889. */
  890. public static float[] addElement(float[] a, float... element) {
  891. float[] b = Arrays.copyOf(a, a.length + element.length);
  892. for (int i = element.length - 1; i >= 0; i--)
  893. b[b.length - (i + 1)] = element[i];
  894. return b;
  895. }
  896. /**
  897. * Returns the {@code Array} with the specified {@code element} inserted at
  898. * the end of the specified {@code a}
  899. *
  900. * @param a the array to add the element to
  901. * @param element the element to add
  902. * @return the array with the added element
  903. */
  904. public static double[] addElement(double[] a, double... element) {
  905. double[] b = Arrays.copyOf(a, a.length + element.length);
  906. for (int i = element.length - 1; i >= 0; i--)
  907. b[b.length - (i + 1)] = element[i];
  908. return b;
  909. }
  910. /**
  911. * Returns the {@code Array} with the specified {@code element} inserted at
  912. * the end of the specified {@code a}
  913. *
  914. * @param a the array to add the element to
  915. * @param element the element to add
  916. * @return the array with the added element
  917. */
  918. public static byte[] addElement(byte[] a, byte... element) {
  919. byte[] b = Arrays.copyOf(a, a.length + element.length);
  920. for (int i = element.length - 1; i >= 0; i--)
  921. b[b.length - (i + 1)] = element[i];
  922. return b;
  923. }
  924. /**
  925. * Returns the {@code Array} with the specified {@code element} inserted at
  926. * the end of the specified {@code a}
  927. *
  928. * @param a the array to add the element to
  929. * @param element the element to add
  930. * @return the array with the added element
  931. */
  932. public static char[] addElement(char[] a, char... element) {
  933. char[] b = Arrays.copyOf(a, a.length + element.length);
  934. for (int i = element.length - 1; i >= 0; i--)
  935. b[b.length - (i + 1)] = element[i];
  936. return b;
  937. }
  938. /**
  939. * Returns the {@code Array} with the specified {@code element} inserted at
  940. * the end of the specified {@code a}
  941. *
  942. * @param a the array to add the element to
  943. * @param element the element to add
  944. * @return the array with the added element
  945. */
  946. public static boolean[] addElement(boolean[] a, boolean... element) {
  947. boolean[] b = Arrays.copyOf(a, a.length + element.length);
  948. for (int i = element.length - 1; i >= 0; i--)
  949. b[b.length - (i + 1)] = element[i];
  950. return b;
  951. }
  952. /**
  953. * Returns the {@code Array} with the specified {@code element} inserted at
  954. * the specified {@code index} of the specified {@code a}.
  955. *
  956. * @param a the array to insert the element to
  957. * @param index the index to insert the element at
  958. * @param element the element to insert
  959. * @return the array with the inserted element
  960. */
  961. public static <T> T[] insertElement(T[] a, int index, T element) {
  962. T[] b = Arrays.copyOf(a, a.length + 1);
  963. System.arraycopy(b, index, b, index + 1, a.length - index);
  964. b[index] = element;
  965. return b;
  966. }
  967. /**
  968. * Returns the {@code Array} with the specified {@code element} inserted at
  969. * the specified {@code index} of the specified {@code a}.
  970. *
  971. * @param a the array to insert the element to
  972. * @param index the index to insert the element at
  973. * @param element the element to insert
  974. * @return the array with the inserted element
  975. */
  976. public static int[] insertElement(int[] a, int index, int element) {
  977. int[] b = Arrays.copyOf(a, a.length + 1);
  978. System.arraycopy(b, index, b, index + 1, a.length - index);
  979. b[index] = element;
  980. return b;
  981. }
  982. /**
  983. * Returns the {@code Array} with the specified {@code element} inserted at
  984. * the specified {@code index} of the specified {@code a}.
  985. *
  986. * @param a the array to insert the element to
  987. * @param index the index to insert the element at
  988. * @param element the element to insert
  989. * @return the array with the inserted element
  990. */
  991. public static long[] insertElement(long[] a, int index, long element) {
  992. long[] b = Arrays.copyOf(a, a.length + 1);
  993. System.arraycopy(b, index, b, index + 1, a.length - index);
  994. b[index] = element;
  995. return b;
  996. }
  997. /**
  998. * Returns the {@code Array} with the specified {@code element} inserted at
  999. * the specified {@code index} of the specified {@code a}.
  1000. *
  1001. * @param a the array to insert the element to
  1002. * @param index the index to insert the element at
  1003. * @param element the element to insert
  1004. * @return the array with the inserted element
  1005. */
  1006. public static short[] insertElement(short[] a, int index, short element) {
  1007. short[] b = Arrays.copyOf(a, a.length + 1);
  1008. System.arraycopy(b, index, b, index + 1, a.length - index);
  1009. b[index] = element;
  1010. return b;
  1011. }
  1012. /**
  1013. * Returns the {@code Array} with the specified {@code element} inserted at
  1014. * the specified {@code index} of the specified {@code a}.
  1015. *
  1016. * @param a the array to insert the element to
  1017. * @param index the index to insert the element at
  1018. * @param element the element to insert
  1019. * @return the array with the inserted element
  1020. */
  1021. public static byte[] insertElement(byte[] a, int index, byte element) {
  1022. byte[] b = Arrays.copyOf(a, a.length + 1);
  1023. System.arraycopy(b, index, b, index + 1, a.length - index);
  1024. b[index] = element;
  1025. return b;
  1026. }
  1027. /**
  1028. * Returns the {@code Array} with the specified {@code element} inserted at
  1029. * the specified {@code index} of the specified {@code a}.
  1030. *
  1031. * @param a the array to insert the element to
  1032. * @param index the index to insert the element at
  1033. * @param element the element to insert
  1034. * @return the array with the inserted element
  1035. */
  1036. public static char[] insertElement(char[] a, int index, char element) {
  1037. char[] b = Arrays.copyOf(a, a.length + 1);
  1038. System.arraycopy(b, index, b, index + 1, a.length - index);
  1039. b[index] = element;
  1040. return b;
  1041. }
  1042. /**
  1043. * Returns the {@code Array} with the specified {@code element} inserted at
  1044. * the specified {@code index} of the specified {@code a}.
  1045. *
  1046. * @param a the array to insert the element to
  1047. * @param index the index to insert the element at
  1048. * @param element the element to insert
  1049. * @return the array with the inserted element
  1050. */
  1051. public static float[] insertElement(float[] a, int index, float element) {
  1052. float[] b = Arrays.copyOf(a, a.length + 1);
  1053. System.arraycopy(b, index, b, index + 1, a.length - index);
  1054. b[index] = element;
  1055. return b;
  1056. }
  1057. /**
  1058. * Returns the {@code Array} with the specified {@code element} inserted at
  1059. * the specified {@code index} of the specified {@code a}.
  1060. *
  1061. * @param a the array to insert the element to
  1062. * @param index the index to insert the element at
  1063. * @param element the element to insert
  1064. * @return the array with the inserted element
  1065. */
  1066. public static double[] insertElement(double[] a, int index, double element) {
  1067. double[] b = Arrays.copyOf(a, a.length + 1);
  1068. System.arraycopy(b, index, b, index + 1, a.length - index);
  1069. b[index] = element;
  1070. return b;
  1071. }
  1072. /**
  1073. * Returns the {@code Array} with the specified {@code element} inserted at
  1074. * the specified {@code index} of the specified {@code a}.
  1075. *
  1076. * @param a the array to insert the element to
  1077. * @param index the index to insert the element at
  1078. * @param element the element to insert
  1079. * @return the array with the inserted element
  1080. */
  1081. public static boolean[] insertElement(boolean[] a, int index, boolean element) {
  1082. boolean[] b = Arrays.copyOf(a, a.length + 1);
  1083. System.arraycopy(b, index, b, index + 1, a.length - index);
  1084. b[index] = element;
  1085. return b;
  1086. }
  1087. /**
  1088. * Returns a pseudorandom element from the specified {@code a}.
  1089. *
  1090. * @param a the array to retrieve the pseudorandomly retrieved element from
  1091. * @return the pseudorandomly retrieved element
  1092. */
  1093. public static <T> T randomElement(T[] a) {
  1094. return a[r.nextInt(a.length)];
  1095. }
  1096. /**
  1097. * Returns a pseudorandom element from the specified {@code a}.
  1098. *
  1099. * @param a the array to retrieve the pseudorandomly retrieved element from
  1100. * @return the pseudorandomly retrieved element
  1101. */
  1102. public static int randomElement(int[] a) {
  1103. return a[r.nextInt(a.length)];
  1104. }
  1105. /**
  1106. * Returns a pseudorandom element from the specified {@code a}.
  1107. *
  1108. * @param a the array to retrieve the pseudorandomly retrieved element from
  1109. * @return the pseudorandomly retrieved element
  1110. */
  1111. public static short randomElement(short[] a) {
  1112. return a[r.nextInt(a.length)];
  1113. }
  1114. /**
  1115. * Returns a pseudorandom element from the specified {@code a}.
  1116. *
  1117. * @param a the array to retrieve the pseudorandomly retrieved element from
  1118. * @return the pseudorandomly retrieved element
  1119. */
  1120. public static long randomElement(long[] a) {
  1121. return a[r.nextInt(a.length)];
  1122. }
  1123. /**
  1124. * Returns a pseudorandom element from the specified {@code a}.
  1125. *
  1126. * @param a the array to retrieve the pseudorandomly retrieved element from
  1127. * @return the pseudorandomly retrieved element
  1128. */
  1129. public static byte randomElement(byte[] a) {
  1130. return a[r.nextInt(a.length)];
  1131. }
  1132. /**
  1133. * Returns a pseudorandom element from the specified {@code a}.
  1134. *
  1135. * @param a the array to retrieve the pseudorandomly retrieved element from
  1136. * @return the pseudorandomly retrieved element
  1137. */
  1138. public static float randomElement(float[] a) {
  1139. return a[r.nextInt(a.length)];
  1140. }
  1141. /**
  1142. * Returns a pseudorandom element from the specified {@code a}.
  1143. *
  1144. * @param a the array to retrieve the pseudorandomly retrieved element from
  1145. * @return the pseudorandomly retrieved element
  1146. */
  1147. public static double randomElement(double[] a) {
  1148. return a[r.nextInt(a.length)];
  1149. }
  1150. /**
  1151. * Returns a pseudorandom element from the specified {@code a}.
  1152. *
  1153. * @param a the array to retrieve the pseudorandomly retrieved element from
  1154. * @return the pseudorandomly retrieved element
  1155. */
  1156. public static char randomElement(char[] a) {
  1157. return a[r.nextInt(a.length)];
  1158. }
  1159. /**
  1160. * Returns a pseudorandom element from the specified {@code a}.
  1161. *
  1162. * @param a the array to retrieve the pseudorandomly retrieved element from
  1163. * @return the pseudorandomly retrieved element
  1164. */
  1165. public static boolean randomElement(boolean[] a) {
  1166. return a[r.nextInt(a.length)];
  1167. }
  1168. /**
  1169. * Reverses the elements of the specified {@code a}.
  1170. *
  1171. * @param a the array to reverse
  1172. * @return the reversed array
  1173. */
  1174. public static <T> T[] reverse(T[] a) {
  1175. T[] reverse = (T[]) Array.newInstance(a[0].getClass(), a.length);
  1176. for (int i = a.length - 1; i >= 0; i--)
  1177. reverse[i] = a[a.length - 1 - i];
  1178. return reverse;
  1179. }
  1180. /**
  1181. * Reverses the elements of the specified {@code a}.
  1182. *
  1183. * @param a the array to reverse
  1184. * @return the reversed array
  1185. */
  1186. public static int[] reverse(int[] a) {
  1187. int[] reverse = new int[a.length];
  1188. for (int i = a.length - 1; i >= 0; i--)
  1189. reverse[i] = a[a.length - 1 - i];
  1190. return reverse;
  1191. }
  1192. /**
  1193. * Reverses the elements of the specified {@code a}.
  1194. *
  1195. * @param a the array to reverse
  1196. * @return the reversed array
  1197. */
  1198. public static short[] reverse(short[] a) {
  1199. short[] reverse = new short[a.length];
  1200. for (int i = a.length - 1; i >= 0; i--)
  1201. reverse[i] = a[a.length - 1 - i];
  1202. return reverse;
  1203. }
  1204. /**
  1205. * Reverses the elements of the specified {@code a}.
  1206. *
  1207. * @param a the array to reverse
  1208. * @return the reversed array
  1209. */
  1210. public static long[] reverse(long[] a) {
  1211. long[] reverse = new long[a.length];
  1212. for (int i = a.length - 1; i >= 0; i--)
  1213. reverse[i] = a[a.length - 1 - i];
  1214. return reverse;
  1215. }
  1216. /**
  1217. * Reverses the elements of the specified {@code a}.
  1218. *
  1219. * @param a the array to reverse
  1220. * @return the reversed array
  1221. */
  1222. public static byte[] reverse(byte[] a) {
  1223. byte[] reverse = new byte[a.length];
  1224. for (int i = a.length - 1; i >= 0; i--)
  1225. reverse[i] = a[a.length - 1 - i];
  1226. return reverse;
  1227. }
  1228. /**
  1229. * Reverses the elements of the specified {@code a}.
  1230. *
  1231. * @param a the array to reverse
  1232. * @return the reversed array
  1233. */
  1234. public static float[] reverse(float[] a) {
  1235. float[] reverse = new float[a.length];
  1236. for (int i = a.length - 1; i >= 0; i--)
  1237. reverse[i] = a[a.length - 1 - i];
  1238. return reverse;
  1239. }
  1240. /**
  1241. * Reverses the elements of the specified {@code a}.
  1242. *
  1243. * @param a the array to reverse
  1244. * @return the reversed array
  1245. */
  1246. public static double[] reverse(double[] a) {
  1247. double[] reverse = new double[a.length];
  1248. for (int i = a.length - 1; i >= 0; i--)
  1249. reverse[i] = a[a.length - 1 - i];
  1250. return reverse;
  1251. }
  1252. /**
  1253. * Reverses the elements of the specified {@code a}.
  1254. *
  1255. * @param a the array to reverse
  1256. * @return the reversed array
  1257. */
  1258. public static char[] reverse(char[] a) {
  1259. char[] reverse = new char[a.length];
  1260. for (int i = a.length - 1; i >= 0; i--)
  1261. reverse[i] = a[a.length - 1 - i];
  1262. return reverse;
  1263. }
  1264. /**
  1265. * Reverses the elements of the specified {@code a}.
  1266. *
  1267. * @param a the array to reverse
  1268. * @return the reversed array
  1269. */
  1270. public static boolean[] reverse(boolean[] a) {
  1271. boolean[] reverse = new boolean[a.length];
  1272. for (int i = a.length - 1; i >= 0; i--)
  1273. reverse[i] = a[a.length - 1 - i];
  1274. return reverse;
  1275. }
  1276. /**
  1277. * Filters the specified {@code a} using the specified {@code predicate} for
  1278. * filtering.
  1279. *
  1280. * @param a the array to filter
  1281. * @param predicate the predicate to use for filtering
  1282. * @return the filtered array
  1283. */
  1284. public static <T> T[] filter(T[] a, Filter<T> predicate) {
  1285. T[] arr = (T[]) Array.newInstance(a[0].getClass(), 0);
  1286. for (T t : a)
  1287. if (predicate.accept(t)) arr = addElement(arr, t);
  1288. return arr;
  1289. }
  1290. /**
  1291. * Filters the specified {@code a} using the specified {@code predicate} for
  1292. * filtering.
  1293. *
  1294. * @param a the array to filter
  1295. * @param predicate the predicate to use for filtering
  1296. * @return the filtered array
  1297. */
  1298. public static int[] filter(int[] a, Filter<Integer> filter) {
  1299. int[] arr = new int[0];
  1300. for (int t : a)
  1301. if (filter.accept(t)) arr = addElement(arr, t);
  1302. return arr;
  1303. }
  1304. /**
  1305. * Filters the specified {@code a} using the specified {@code predicate} for
  1306. * filtering.
  1307. *
  1308. * @param a the array to filter
  1309. * @param predicate the predicate to use for filtering
  1310. * @return the filtered array
  1311. */
  1312. public static short[] filter(short[] a, Filter<Short> filter) {
  1313. short[] arr = new short[0];
  1314. for (short t : a)
  1315. if (filter.accept(t)) arr = addElement(arr, t);
  1316. return arr;
  1317. }
  1318. /**
  1319. * Filters the specified {@code a} using the specified {@code predicate} for
  1320. * filtering.
  1321. *
  1322. * @param a the array to filter
  1323. * @param predicate the predicate to use for filtering
  1324. * @return the filtered array
  1325. */
  1326. public static long[] filter(long[] a, Filter<Long> filter) {
  1327. long[] arr = new long[0];
  1328. for (long t : a)
  1329. if (filter.accept(t)) arr = addElement(arr, t);
  1330. return arr;
  1331. }
  1332. /**
  1333. * Filters the specified {@code a} using the specified {@code predicate} for
  1334. * filtering.
  1335. *
  1336. * @param a the array to filter
  1337. * @param predicate the predicate to use for filtering
  1338. * @return the filtered array
  1339. */
  1340. public static byte[] filter(byte[] a, Filter<Byte> filter) {
  1341. byte[] arr = new byte[0];
  1342. for (byte t : a)
  1343. if (filter.accept(t)) arr = addElement(arr, t);
  1344. return arr;
  1345. }
  1346. /**
  1347. * Filters the specified {@code a} using the specified {@code predicate} for
  1348. * filtering.
  1349. *
  1350. * @param a the array to filter
  1351. * @param predicate the predicate to use for filtering
  1352. * @return the filtered array
  1353. */
  1354. public static float[] filter(float[] a, Filter<Float> filter) {
  1355. float[] arr = new float[0];
  1356. for (float t : a)
  1357. if (filter.accept(t)) arr = addElement(arr, t);
  1358. return arr;
  1359. }
  1360. /**
  1361. * Filters the specified {@code a} using the specified {@code predicate} for
  1362. * filtering.
  1363. *
  1364. * @param a the array to filter
  1365. * @param predicate the predicate to use for filtering
  1366. * @return the filtered array
  1367. */
  1368. public static double[] filter(double[] a, Filter<Double> filter) {
  1369. double[] arr = new double[0];
  1370. for (double t : a)
  1371. if (filter.accept(t)) arr = addElement(arr, t);
  1372. return arr;
  1373. }
  1374. /**
  1375. * Filters the specified {@code a} using the specified {@code predicate} for
  1376. * filtering.
  1377. *
  1378. * @param a the array to filter
  1379. * @param predicate the predicate to use for filtering
  1380. * @return the filtered array
  1381. */
  1382. public static char[] filter(char[] a, Filter<Character> filter) {
  1383. char[] arr = new char[0];
  1384. for (char t : a)
  1385. if (filter.accept(t)) arr = addElement(arr, t);
  1386. return arr;
  1387. }
  1388. /**
  1389. * Filters the specified {@code a} using the specified {@code predicate} for
  1390. * filtering.
  1391. *
  1392. * @param a the array to filter
  1393. * @param predicate the predicate to use for filtering
  1394. * @return the filtered array
  1395. */
  1396. public static boolean[] filter(boolean[] a, Filter<Boolean> filter) {
  1397. boolean[] arr = new boolean[0];
  1398. for (boolean t : a)
  1399. if (filter.accept(t)) arr = addElement(arr, t);
  1400. return arr;
  1401. }
  1402. }