Last active
December 13, 2016 19:00
-
-
Save pepe-romeros/ec9fd7ff769a9405d4ea5ea6a3d0cc97 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.suchowitzki.reversearray; | |
public class ReverseList { | |
public static void main(String... args) { | |
Character[] arr = new Character[]{'a', 'b', 'c', 'd', 'e', 'f'}; | |
reverse(arr); | |
System.out.println(Arrays.toString(arr)); | |
Integer[] arr2 = new Integer[]{1, 2, 3, 4, 5}; | |
reverse(arr2); | |
System.out.println(Arrays.toString(arr2)); | |
} | |
public static <T> void reverse(T[] arr) { | |
for (int i = 0; i < arr.length / 2; i++) { | |
swap(arr, i, arr.length - i - 1); | |
} | |
} | |
public static <T> void swap(T[] arr, int start, int end) { | |
T temp = arr[start]; | |
arr[start] = arr[end]; | |
arr[end] = temp; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment