-7

I am a new bie to the world of java , I was going through array , in an interview it was being asked from me write a code to reverse an array , I have gone through the following approach.. using Apache commons ArrayUtils class , but please advise how can the same thing be achieved through java itself, below is the my approach

 int[] iArray = new int[] {101,102,103,104,105};
     String[] sArray = new String[] {"one", "two", "three", "four", "five"}; 
     System.out.println("Original int array : " + Arrays.toString(iArray));
     ArrayUtils.reverse(iArray); System.out.println("reversed int array : " + Arrays.toString(iArray)); 
     System.out.println("Original String array : " + Arrays.toString(sArray)); ArrayUtils.reverse(sArray); System.out.println("reversed String array in Java : " + Arrays.toString(sArray)); 

Output :-

Original int array : [101, 102, 103, 104, 105] reversed int array : [105, 104, 103, 102, 101] Original String array : [one, two, three, four, five] reversed String array in Java : [five, four, three, two, one]

Please advise how we can achieve the same thing in java itself.

  • What have you tried *without* using a utility library? I assume you know how to set a value in an array and how to retrieve one... think how you can go from there. – Jon Skeet Mar 22 '13 at 14:48
  • This question has all sort of answers. http://stackoverflow.com/questions/12678781/reversing-an-array-in-java?rq=1. Do some search before you put a questions. – Subir Kumar Sao Mar 22 '13 at 14:49
  • 3
    You know, Apache commons is open source, you can check the ArrayUtils' source code in order to understand how they do it: http://kickjava.com/src/org/apache/commons/lang/ArrayUtils.java.htm – pabrantes Mar 22 '13 at 14:50

5 Answers5

2

You can reverse an array like this:

public void reverse(Object [] a){
    for(int i = 0; i < a.length / 2; i++){
        Object temp = a[i]; // swap using temporary storage
        a[i] = a[a.length - i - 1];
        a[a.length - i - 1] = temp;
    }
}

It's worthy to note that it doesn't matter if the array length is an odd number, as the median value will remain unchanged. I have to admit that I haven't tested this but it should work.

Working Example

rtheunissen
  • 7,347
  • 5
  • 34
  • 65
1

Here i wrote an example:

http://ideone.com/OUA4r9

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        String[] array = {"one", "two", "three"};
        String[] reverse = new String[array.length];
        for (int i = 0; i < array.length; i++) {
            reverse[i] = array[array.length - i - 1];
        }
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(reverse));
    }
}

Output

[one, two, three]
[three, two, one]

Zelldon
  • 5,396
  • 3
  • 34
  • 46
0
for(int i=0; i<iArray.length/2; i++)
{
    int temp;
    iArray[i] = temp;
    iArray[i] = iArray[jArray.length-i-1];
    iArray[jArray.length-i-1] = temp;
}
0

It should be simple to write, just think through the task logically.

The array has indices from 0 to N. To reverse the order, the element at index 0 needs to swap places with index N, 1 with N-1 and so on.

You are finished when every pair of indices i(0...N) k(0...N) has experienced exactly one swapping operation.

This can be expressed with very minimal logic in code:

for (int i=0, j=array.length-1; i<j; ++i, --j) {
    temp = array[i];
    array[i] = array[j];
    array[j] = temp;
}

And thats it.

Durandal
  • 19,919
  • 4
  • 36
  • 70
0

You can try something like this in Java to reverse the given array.

Collections.reverse(Arrays.asList(sArray));
Achintya Jha
  • 12,735
  • 2
  • 27
  • 39
  • Here's an example of yours working http://ideone.com/46Ups9. Only other issue is that it won't work with primitive types. So in order for the int array to work it will have to be Integer objects - I +1 you also – wirey00 Mar 22 '13 at 15:26