-1

Possible Duplicate:
How do I reverse an int array in Java?

I need to reverse the order of the given numbers in the array without using a temp.

public class Reverse 
{
    public static void main (String[] args)
    {
        int[] num  = { 12, 34, 50, 67, 88 }, cl, cl2;

        for (i=0; i < a.length; i++)
        {
            cl = a[i];
            cl2 = a[(a.length - 1 )-1];
            System.out.println (cl1 + " " + cl2);
        }
    }
}
Community
  • 1
  • 1
Mike S
  • 23
  • 1
  • 2
  • 4

3 Answers3

5

Since it only seems that you're printing the values out, you can iterate over your array backwards.

for(int i = a.length - 1; i >= 0; i--) {
    // relevant code here
}
Makoto
  • 104,088
  • 27
  • 192
  • 230
3

you can use Collections#reverse to inline a reverse sort of the integer array,

Collections.reverse(Arrays.asList(num));
mre
  • 43,520
  • 33
  • 120
  • 170
1

The answers given are perfect. But i just added another answer in case if you want to store the reverse array into the original array without using any temp. use this.

public class Reverse 
{
    public static void main (String[] args)
    {
        int[] a = { 12, 34, 50, 67, 88 };
        int i, j;

        for (i = 0, j = a.length - 1; i < j; i++, j--)
       {
           a[i]=a[i]+a[j];
           a[j]=a[i]-a[j];
           a[i]=a[i]-a[j];
       }
    }
}
stinepike
  • 54,068
  • 14
  • 92
  • 112