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;
}
popremoves items from aStack, andpushadds items to aStack. Why are you adding items to yourStackwhile copying to theList? What do you want in yourListwhen the method completes?Stackwhile you copy it... that is probably not what you want.