Created
August 21, 2021 12:12
-
-
Save daanta-real/19ad2188523205d47d91d29083feadbb to your computer and use it in GitHub Desktop.
Array reverse - for int type
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
// A method of returning an array which has reversed order of the original int[] array | |
public static int[] array_reverse_int(int[] arr1) { | |
int[] arr2 = new int[arr1.length]; | |
for(int i = 0; i < arr1.length; i++) arr2[i] = arr1[arr1.length - 1 - i]; | |
return arr2; | |
} | |
// Let's run a sample code | |
public static void main(String[] args) { | |
// Declare and initalize a sample int array | |
int[] arr1 = {1, 2, 3, 4, 5, 6}; | |
// Reverse it and show the result! | |
arr1 = array_reverse_int(arr1); | |
System.out.println("Reversed array:\n" + Arrays.toString(arr1)); | |
} | |
/* | |
[[Result]] | |
Reversed array: | |
[6, 5, 4, 3, 2, 1] | |
*/ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment