Skip to content

Commit 167bd7d

Browse files
authored
Improve $.reverse() for array.
1 parent 082d6d7 commit 167bd7d

File tree

1 file changed

+8
-2
lines changed
  • src/main/java/com/github/underscore

1 file changed

+8
-2
lines changed

src/main/java/com/github/underscore/$.java

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2614,9 +2614,15 @@ public static <T> List<T> reverse(final Iterable<T> iterable) {
26142614
return result;
26152615
}
26162616

2617-
@SuppressWarnings("unchecked")
26182617
public static <T> T[] reverse(final T ... array) {
2619-
return (T[]) reverse(Arrays.asList(array)).toArray();
2618+
T temp;
2619+
final T[] newArray = array.clone();
2620+
for (int index = 0; index < array.length / 2; index += 1) {
2621+
temp = newArray[index];
2622+
newArray[index] = newArray[array.length - 1 - index];
2623+
newArray[array.length - 1 - index] = temp;
2624+
}
2625+
return newArray;
26202626
}
26212627

26222628
public static List<Integer> reverse(final int[] array) {

0 commit comments

Comments
 (0)