java - 堆栈到 --> ArrayList Java

标签 java arraylist stack

我做了一个 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/

相关文章:

java - 如何从列表中删除重复项?

java - NullPointerException 将子字符串从字符串添加到数组列表

java - HashSet是如何排序的?

c++ - 没有 "const char*"的段错误

java - 在 Java 中使用数组创建 Stack 类

java - AspectJ 加载时编织 + Spring 3.0.5.RELEASE + Tomcat 7 不工作

java - 更新 JPA 或 Hibernate 中的多对多关系

java - 来自 Ionic 应用程序的 https 请求第一次返回 403 然后它就可以工作了

java - 无法转换为 int[][]

java - 递归:解决进口迷宫?