0

I'm supposed to feed an Arraylist into a stack, and then obviously build a list from that stack. Theoretically this means that the list should be reversed, yes? I thought I had it all in the bag, but my output is thusly:

 [1, 2, 3, 4, 5]
 AAAAANNNDDDDD REVERSE!
 [1, 2, 3, 4, 5]

So obviously I goofed somewhere, but I can't tell where. Code follows, but be warned, I'm not horribly good at this, so if there's something that horribly offends you, but isn't actually the problem, I'd prefer it not be the focus of discussion.

package stackclass;

import java.util.ArrayList;
import java.util.Stack;

public class StackClass 
{
 static ArrayList<String> list = new ArrayList();
 static Stack<String> popper = new Stack();

public static ArrayList<String> reverse(ArrayList<String> n)
{
    for(int i = 0; i != n.size();)
    {
        popper.push(n.get(n.size() - 1));
        n.remove(n.size() - 1);
    }
    for(int i = 0; i != popper.size();)
    {
        n.add(popper.pop());
    }
    return n;
}
public static void main(String[] args)
{
    list.add("1");
    list.add("2");
    list.add("3");
    list.add("4");
    list.add("5");

    System.out.println(list);

    System.out.println("AAAAANNNDDDDD REVERSE!");

    ArrayList n = reverse(list);

    System.out.println(n);
}
}

Any and all help is appreciated.

2 Answers 2

1
public static ArrayList<String> reverse(ArrayList<String> n)
    {
        while(!n.isEmpty())
        {
            popper.push(n.get(0));
            n.remove(0);
        }
        while(!popper.isEmpty()){
            n.add(popper.pop());
        }

        return n;
    }

This works.

Sign up to request clarification or add additional context in comments.

Comments

1

In your first for-loop, you should take the first element rather than the last one from the list:

public static ArrayList<String> reverse(ArrayList<String> n)
{
    for(int i = 0; i < n.size(); i++)
    {
        popper.push(n.get(i));
        n.remove(0);
    }
    for(int i = 0; i != popper.size(); i++)
    {
        n.add(popper.pop());
    }
    return n;
}

you're also missing to increment your counters (i++)

3 Comments

Oh man, you're a genius and I'm retarded. That makes so much more sense, I was just thinking backwards. Thanks so much. EDIT: No need for the i counters because you're removing i every time, so it's naturally moving down one.
Are you supposed to create a new ArrayList or modify the old one? In the second case the return type of "reverse" should be void. Also it's very inefficient to remove one element at a time. Use ArrayList.clear() to remove everything at once.
@SpiderPig I just fixed a bug in the code of the OP, who was interested in why his/her code didn't work rather than finding the best approach to do it. Actually, reversing a list can be done with just one line of code: java.util.Collections.reverse(myList);

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.