Created
September 1, 2015 07:29
-
-
Save jweinst1/fb4f81ab19d673fa0b31 to your computer and use it in GitHub Desktop.
Returns a Reversed Version of an Array in Java
This file contains hidden or 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
| import java.util.Arrays; | |
| class Main { | |
| public static void main(String[] args) { | |
| System.out.println(Arrays.toString(reversed(lst))); | |
| } | |
| static int[] lst = {1, 2, 3, 4, 5}; | |
| static int[] reversed(int[] x){ | |
| int size = x.length; | |
| int[] xcopy = new int[size]; | |
| int counter = size - 1; | |
| int revcount = 0; | |
| while (counter >= 0) { | |
| xcopy[revcount] = x[counter]; | |
| counter -= 1; | |
| revcount ++; | |
| } | |
| return xcopy; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment