我做了一个 Stack 和一个 ArrayList 来研究一下。实际上我现在想用 ArrayList 替换我的 Stack,但是如何将 Stack 转换为 ArrayList? 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;
}
最佳答案
Stack是一个Collection,可以使用ArrayList(Collection)构造函数
list = new ArrayList(stack);
关于java - 堆栈到 --> ArrayList Java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28313917/