If I have an array like this:
1 4 9 16 9 7 4 9 11
What is the best way to reverse the array so that it looks like this:
11 9 4 7 9 16 9 4 1
I have the code below, but I feel it is a little tedious:
public int[] reverse3(int[] nums) {
return new int[] { nums[8], nums[7], nums[6], nums[5], num[4],
nums[3], nums[2], nums[1], nums[0] };
}
Is there a simpler way?
I would do something like this:
public int[] reverse3(int[] nums) {
int[] numsReturn = new int[nums.length()];
int count = nums.length()-1;
for(int num : nums) {
numsReturn[count] = num;
count--;
}
return numsReturn;
}
public void swap(int[] arr,int a,int b)
{
int temp=arr[a];
arr[a]=arr[b];
arr[b]=temp;
}
public int[] reverseArray(int[] arr){
int size=arr.length-1;
for(int i=0;i<size;i++){
swap(arr,i,size--);
}
return arr;
}
Collections.reverse()
can do that job for you if you put your numbers in a List
of Integers
.
List<Integer> list = Arrays.asList(1, 4, 9, 16, 9, 7, 4, 9, 11);
System.out.println(list);
Collections.reverse(list);
System.out.println(list);
Output:
[1, 4, 9, 16, 9, 7, 4, 9, 11]
[11, 9, 4, 7, 9, 16, 9, 4, 1]
In case you don't want to use a temporary variable, you can also do like this:
final int len = arr.length;
for (int i=0; i < (len/2); i++) {
arr[i] += arr[len - 1 - i]; // a = a+b
arr[len - 1 - i] = arr[i] - arr[len - 1 - i]; // b = a-b
arr[i] -= arr[len - 1 - i]; // a = a-b
}
try this:
public int[] reverse3(int[] nums) {
int[] reversed = new int[nums.length];
for (int i=0; i<nums.length; i++) {
reversed[i] = nums[nums.length - 1 - i];
}
return reversed;
}
My input was:
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12
And the output I got:
12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1
In place reversal with minimum amount of swaps.
for (int i = 0; i < a.length / 2; i++) {
int tmp = a[i];
a[i] = a[a.length - 1 - i];
a[a.length - 1 - i] = tmp;
}
The following will reverse in place the array between indexes i
and j
(to reverse the whole array call reverse(a, 0, a.length - 1)
)
public void reverse(int[] a, int i , int j) {
int ii = i;
int jj = j;
while (ii < jj) {
swap(ii, jj);
++ii;
--jj;
}
}
This code would help:
int [] a={1,2,3,4,5,6,7};
for(int i=a.length-1;i>=0;i--)
System.out.println(a[i]);
Or you could loop through it backeards
int[] firstArray = new int[]{1,2,3,4};
int[] reversedArray = new int[firstArray.length];
int j = 0;
for (int i = firstArray.length -1; i > 0; i--){
reversedArray[j++] = firstArray[i];
}
(note: I have not compiled this but hopefully it is correct)
If you want to reverse the array in-place:
Collections.reverse(Arrays.asList(array));
It works since Arrays.asList returns a write-through proxy to the original array.
You could use org.apache.commons.lang.ArrayUtils :
ArrayUtils.reverse(array)
If you don't want to use Collections
then you can do this:
for (i = 0; i < array.length / 2; i++) {
int temp = array[i];
array[i] = array[array.length - 1 - i];
array[array.length - 1 - i] = temp;
}
you messed up
int[] firstArray = new int[]{1,2,3,4};
int[] reversedArray = new int[firstArray.length];
int j = 0;
for (int i = firstArray.length -1; i >= 0; i--){
reversedArray[j++] = firstArray[i];
}
you can send the original array to a method for example:
after that you create a new array to hold the reversed elements
public static void reverse(int[] a){
int[] reversedArray = new int[a.length];
for(int i = 0 ; i<a.length; i++){
reversedArray[i] = a[a.length -1 -i];
}
I like to keep the original array and return a copy. This is a generic version:
public static <T> T[] reverse(T[] array) {
T[] copy = array.clone();
Collections.reverse(Arrays.asList(copy));
return copy;
}
without keeping the original array:
public static <T> void reverse(T[] array) {
Collections.reverse(Arrays.asList(array));
}
Source: Stackoverflow.com