-1

这是我到目前为止所拥有的:

System.out.println("\fStack - ArrayList Demo\n");

        ArrayList<String> al = new ArrayList<String>();
        Stack<String> st = new Stack<String>();

        // Start with a few elements to the ArrayList
        al.add("A");
        al.add("B");
        al.add("C");

        st.addAll(al); // Retrieve ArrayList elements for the stack and add them there
        al.clear(); // Clear arraylist for storage later


        // Pop out the last element in the stack.
        System.out.println("\nPop out the last element: "+al.add(st.pop()));
        System.out.println("What's left of the stack: "+st+"\n");
        System.out.println("ArrayList, now: "+al);

        // Keep popping..
        int count = st.search("A");
       while (count != -1 && count >= 1) {
           System.out.println("Popping next.."+al.add(st.pop()));
           count--;
        }

        // Is the Stack empty?
        System.out.println("\nStack Empty? "+st.empty());
        System.out.println("ArrayList now: "+al);

当我运行程序时,使用al.add(st.pop()));返回“true”。有没有办法让它产生被弹出的实际元素(A,B,C)?

4

1 回答 1

0

用于添加状态的javadoc :

Returns:
true (as specified by Collection.add(E))

听起来您想打印要添加的元素。所以:

   String elem = st.pop();
   System.out.println("adding " +elem);
   al.add(elem)

如果您打算成为一名软件开发人员,那么阅读和理解文档是一项宝贵的实践技能。

于 2014-06-23T22:11:20.763 回答