17

I made a Stack and an ArrayList to make a research. Actually I want now to make my Stack replaced by an ArrayList, but how to transform a Stack into an ArrayList ? How is it going with push, pop ... ?

public static ArrayList<State> search(State finalstate)
{
    ArrayList<State> toreturn = new ArrayList<State>();
    Stack<State>mystack=new Stack<State>();
    mystack.push(initState);
    State currState;
    currState=initState;
    while(!mystack.isEmpty() && !currState.equals(finalstate) )
    {
        currState=mystack.pop();
        toreturn.add(currState);
        if(currState.vecinos.containsKey("up"))
        {
            mystack.push(currState).vecinos.get("up");
        }
        if(currState.vecinos.containsKey("down"))
        {
            mystack.push(currState).vecinos.get("down");
        }
        if(currState.vecinos.containsKey("left"))
        {
            mystack.push(currState).vecinos.get("left");
        }
        if(currState.vecinos.containsKey("right"))
        {
            mystack.push(currState).vecinos.get("right");
        }
    }
    
    return toreturn;
}
4
  • Well pop removes items from a Stack, and push adds items to a Stack. Why are you adding items to your Stack while copying to the List? What do you want in your List when the method completes? Commented Feb 4, 2015 at 5:14
  • Just to keep a trace of it in the List Commented Feb 4, 2015 at 5:16
  • just to keep a track* sorry Commented Feb 4, 2015 at 5:16
  • What does that mean? You're modifying the Stack while you copy it... that is probably not what you want. Commented Feb 4, 2015 at 5:17

5 Answers 5

35

Stack is a Collection, you can use ArrayList(Collection) constructor

list = new ArrayList(stack);
Sign up to request clarification or add additional context in comments.

3 Comments

This is problematic. If we pushed stuff into Stack in this order: 1, 2, 3, 4, 5, then surprisingly the ArrayList will also end up as [1, 2, 3, 4, 5] whereas we probably expect [5, 4, 3, 2, 1]
Yup, @Wei is right, this method results in reverse order for stack.
Collections.reverse(stack); // resolves the issue :)
6

The simplest way I've found to convert a Stack into a List is using the following line:

List<Integer> stackToList = new ArrayList(stack);

However, this yields the stack reversed. Meaning, if your stack was 1, 2, 3 you would expect in the list 3, 2, 1 because that's the order the stack objects would be "popped". That's not the case though instead, you get 1, 2, 3. So, in order to get the expected output, you need to execute

Collections.reverse (stackToList);

This will reverse the list inline and give you 3, 2, 1

Comments

1

The above answer is not right, as the order will be reverse.
Instead you can just iterate like this:

Stack<State> stack = new Stack<>();
List<State> list = new ArrayList<>();
while(!stack.isEmpty()) { 
    list.add(stack.pop()); 
}

Comments

0

ArrayDeque from Deque interface can be used for this type of operation.

ArrayDeque is better to used then Stack.

Deque<Integer> d = new ArrayDeque<Integer>();
d.push(10);
d.push(20);
d.push(30);

ArrayList<Integer> list = new ArrayList<Integer>(d);
System.out.println(d.toString());
System.out.println(list.toString());

Comments

-1

Use Deque instead,

Deque<Integer> deque = new ArrayDeque<>();
deque.push(1);
deque.push(2);
deque.push(3);
System.out.println(new ArrayList<>(deque)); // 3, 2, 1

1 Comment

I can convert the stack into an array-list with this line.

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.