Reversing an Array in Java

JavaArraysReverse

Java Problem Overview


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?

Java Solutions


Solution 1 - Java

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]

Solution 2 - Java

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.

Solution 3 - Java

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;
}

Solution 4 - Java

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));
}

Solution 5 - Java

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

Solution 6 - Java

You could use org.apache.commons.lang.ArrayUtils : ArrayUtils.reverse(array)

Solution 7 - Java

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)

Solution 8 - Java

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;
}

Solution 9 - Java

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;
}

Solution 10 - Java

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];
}

Solution 11 - Java

 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;
}

Solution 12 - Java

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;
	    }
	}

Solution 13 - Java

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
}

Solution 14 - Java

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]);

Solution 15 - Java

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];


}

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionPHZE OXIDEView Question on Stackoverflow
Solution 1 - JavaVikdorView Answer on Stackoverflow
Solution 2 - JavaJordan DenisonView Answer on Stackoverflow
Solution 3 - Javakanhai shahView Answer on Stackoverflow
Solution 4 - JavaLuigi R. ViggianoView Answer on Stackoverflow
Solution 5 - JavaThe BadakView Answer on Stackoverflow
Solution 6 - JavaEFalcoView Answer on Stackoverflow
Solution 7 - JavaRNJView Answer on Stackoverflow
Solution 8 - JavaMathias BakView Answer on Stackoverflow
Solution 9 - JavaRodrigo KossmannView Answer on Stackoverflow
Solution 10 - Javauser2719349View Answer on Stackoverflow
Solution 11 - JavaHimanshu GuptaView Answer on Stackoverflow
Solution 12 - JavaDavid SorokoView Answer on Stackoverflow
Solution 13 - JavaiaLView Answer on Stackoverflow
Solution 14 - JavaJaipal KumarView Answer on Stackoverflow
Solution 15 - Javashayyy7View Answer on Stackoverflow