0

Hello I am trying to reverse an ArrayList without the reverse method. I just wanted to make it work without the method. I can't seem to get it right. This is what i have so far:

for (int x = nums.size()-1; x>=0; x--)
    {

        for(int z =0; z<nums.size();z++)
        {
            nums.set(z, x);

        }
    }

This is my output: run: 0 1 2 3

1 1 1 1 BUILD SUCCESSFUL (total time: 0 seconds)

8
  • you are doing it twice and rearranging it back to the way it was Commented Oct 6, 2013 at 23:35
  • @VirtualBaseClass - Actually, he's not. If he was, the output would be "0 1 2 3" not "1 1 1 1". Commented Oct 7, 2013 at 0:10
  • @StephenC yeah.. another problem is that he is setting the value x, not getting it from the array. Dunno why its 1111 though, shouldn't it be the last value that x takes which is 0. Commented Oct 7, 2013 at 0:14
  • @VirtualBaseClass - "Dunno why its 1111 though". I suggest that you get a pencil and paper and "hand execute" the code. Then you'll understand. Commented Oct 7, 2013 at 0:50
  • @StephenC Don't really need a pen and paper for it, thanks for the remark though. And I still think its 0000. Would be grateful if you point out what I am missing. Commented Oct 7, 2013 at 0:57

2 Answers 2

1

You can ascend from the bottom & simultaneously descend from the top (size() - 1) swapping elements, and stop when you meet in the middle.

int i = 0;
int j = nums.size()-1;
while (i < j) {
    int temp = nums.get(i);
    nums.set( i, nums.get(j));
    nums.set( j, temp);
    i++; j--;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Do you mind putting it in a for loop? i wanna understand it better.
@JB - You should attempt to understand it in its current form. It is easier to understand this way than as a for loop.
Since there are two loop indexes you need two declarations, two initializations and two advancements. Putting the syntax in a "for" loop can be done (except perhaps for the declarations), but it would reduce clarity.
0

You can swap in pairs at a time, coming in from both ends toward the center:

for (int i = 0; i < nums.size()/2; i++) {
    Integer left = nums.get(i);
    Integer right = nums.get(nums.size()-1-i);
    nums.set(i, right);
    nums.set(nums.size()-1-i, left);
}

By using Integer instead of int for left and right, Java doesn't have to keep converting between int and Integer values (the first being a primitive, the second being an actual object). This improves performance.

2 Comments

You don't know if autoboxing is happening here
ArrayList can only store objects, so in this case Integer instances would be stored and not int. When you read the documentation at docs.oracle.com/javase/7/docs/api/java/util/… you will notice that the return type is given as E, which in this case refers to Integer. If this answer said int left = nums.get(i), then the Integer returned would be converted to int and then stored in left. Then it would be converted into Integer in nums.set(... again.

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.