1

I'm trying to create a method that takes in an array and then returns that array in reverse. The code I wrote returns the array in reverse, but, the first two values are now 0. Anyone know what I did wrong?

public static int[] reverse(int[] x)
{     
    int []d = new int[x.length];

    for (int i = 0; i < x.length/2; i++)  // for loop, that checks each array slot
    {
        d[i] = x[i];
        x[i] = x[x.length-1-i];  // creates a new array that is in reverse order of the original
        x[x.length-1-i] = d[i];
    }
    return d;      // returns the new reversed array  
}
4
  • 1
    Maybe you should rethink your solution and try your algorithm on paper before you write some code.. Commented Jul 1, 2014 at 19:53
  • Note that you're directly writing in x when you should use it to read the data only. Commented Jul 1, 2014 at 19:54
  • 1
    @Eran because you don't need to iterate over the whole array to access every element. Note how OP goes for the array data: x[i] and x[x.length - 1 - i]. Commented Jul 1, 2014 at 19:59
  • 2
    It looks to me like x is reversed in place, I don't see why d would be returned. Commented Jul 1, 2014 at 19:59

4 Answers 4

7

You are assigning values from an uninitialized array d to x - that's where the zeroes (default value for an int in Java) are coming from.

IIUC, you're mixing two reversing strategies.

If you're creating a new array, you needn't run over half of the original array, but over all of it:

public static int[] reverse(int[] x) {

    int[] d = new int[x.length];


    for (int i = 0; i < x.length; i++) {
        d[i] = x[x.length - 1 -i];
    }
    return d;
}

Alternatively, if you want to reverse the array in place, you don't need a temp array, only a single variable (at most - there are also ways to switch two ints without an additional variable, but that's a different question):

public static int[] reverseInPlace(int[] x) {
    int tmp;    

    for (int i = 0; i < x.length / 2; i++) {
        tmp = x[i];
        x[i] = x[x.length - 1 - i];
        x[x.length - 1 - i] = tmp;
    }
    return x; // for completeness, not really necessary.
}
Sign up to request clarification or add additional context in comments.

1 Comment

I changed it to you first suggestions, but what happens is that when it prints, the first and last values are switched around, any idea why?
3

Here is a short way to do it.

public static int[] reverse(int[] x)
   {

       int[] d = new int[x.length];            //create new array


       for (int i=x.length-1; i >= 0; i--)      // revered loop
       {
        d[(x.length-i-1)]=x[i];                 //setting values              

       }
        return d;                            // returns the new reversed array

   }

Comments

1

Its simple mistake; you are coping reversed data in x; and returning d. If you will return x, you will get complete revered data.

    d[i] = x[i];    // you are copying first element to some temp value
    x[i] = x[x.length-1-i];  // copied last element to first; and respective...
    x[x.length-1-i] = d[i]; // copied temp element to first element; and temp elements are nothing but array d

So ultimately you have created revered array inside x and not in d. If you will return x you got your answer. And d which is just half baked; so you get default value of 0 for remainign half array. :)

Comments

1
int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    System.out.println("The original Array: ");
    for (int i = 0; i < array.length; i++) {
        System.out.print(array[i] + " ");
    }
    System.out.println();
        System.out.println("The Reverse Array is: ");
        for (int i = array.length - 1; i >= 0; i--) {
            System.out.print(array[i] + " ");
       }

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.