*
* @author Robin Cheney
*
- * @since 1.1
+ * @since 1.4.1
*/
-public abstract class ArrayUtils<E> {
- public E[] reverse(E[] array) {
+public abstract class ArrayUtils {
+
+ /**
+ * Reverse and return {@code array}
+ *
+ * @apiNote This reassigns the original array that is passed to the function as
+ * the parameter. If you don't want this, use
+ * {@link #reversedCopy(Object[])}
+ *
+ * @param <E> The class/datatype of the array
+ * @param array The array to reverse
+ * @return the reversed array with the same datatype as the original
+ */
+ public static <E> E[] reverse(E[] array) {
ArrayList<E> outArray = new ArrayList<E>();
- for (int i = 0; i < array.length; i++) {
+ for (E i : array) {
+ // System.out.println(i);
+ outArray.add(i);
+ }
+
+ return outArray.reversed().toArray(array);
+ }
+
+ /**
+ * Reverse and return a copy of {@code array}
+ *
+ * @apiNote If you also want to reverse the original array that was passed as an
+ * argument, use {@link #reverse(Object[])}
+ *
+ * @param <E> The class/datatype of the array
+ * @param array The array to reverse
+ * @return the reversed array with the same datatype as the original
+ */
+ public static <E> E[] reversedCopy(E[] array) {
+ E[] arrayCopy = array.clone();
+
+ ArrayList<E> outArray = new ArrayList<E>();
- // prepend each character
- outArray.add(array[i]);
+ for (E i : array) {
+ // System.out.println(i);
+ outArray.add(i);
}
- return outArray.toArray(array);
+
+ return outArray.reversed().toArray(arrayCopy);
+ }
+
+ /**
+ * Reverse and return an {@code array}
+ *
+ * @param <E> The class/datatype of the array
+ * @param array The array to reverse
+ * @return the reversed array with the same datatype as the original
+ *
+ * @since 1.4.1
+ */
+ public static <E> ArrayList<E> toArrayList(E[] array) {
+
+ ArrayList<E> outArray = new ArrayList<E>();
+
+ for (E i : array) {
+ outArray.add(i);
+ }
+
+ return outArray;
}
}