How do I copy a stack in Java?

前端 未结 6 640
星月不相逢
星月不相逢 2020-12-03 21:03

I have a stack A and I want to create a stack B that is identical to stack A. I don\'t want stack B to simply be a pointer to A -- I actually want to create a new stack B th

相关标签:
6条回答
  • 2020-12-03 21:23

    Just use the clone() -method of the Stack-class (it implements Cloneable).

    Here's a simple test-case with JUnit:

    @Test   
    public void test()
    {
        Stack<Integer> intStack = new Stack<Integer>();
        for(int i = 0; i < 100; i++)        
        {
            intStack.push(i);
        }
    
        Stack<Integer> copiedStack = (Stack<Integer>)intStack.clone();
    
        for(int i = 0; i < 100; i++)            
        {
            Assert.assertEquals(intStack.pop(), copiedStack.pop());
        }
    }
    

    Edit:

    tmsimont: This creates a "unchecked or unsafe operations" warning for me. Any way to do this without generating this problem?

    I at first responded that the warning would be unavoidable, but actually it is avoidable using <?> (wildcard) -typing:

    @Test
    public void test()
    {
        Stack<Integer> intStack = new Stack<Integer>();
        for(int i = 0; i < 100; i++)
        {
            intStack.push(i);
        }
    
        //No warning
        Stack<?> copiedStack = (Stack<?>)intStack.clone();
    
        for(int i = 0; i < 100; i++)
        {
            Integer value = (Integer)copiedStack.pop(); //Won't cause a warning, no matter to which type you cast (String, Float...), but will throw ClassCastException at runtime if the type is wrong
            Assert.assertEquals(intStack.pop(), value);
        }
    }
    

    Basically I'd say you're still doing an unchecked cast from ? (unknown type) to Integer, but there's no warning. Personally, I'd still prefer to cast directly into Stack<Integer> and suppress the warning with @SuppressWarnings("unchecked").

    0 讨论(0)
  • 2020-12-03 21:27
     /**
         * Copy constructor for the Stack class
         * @param original the Stack to copy
         * @postcondition a new Stack object which is
         * an identical, but distinct, copy of original
         */
        public Stack(Stack<T> original) {
            if (original.length == 0)
            {
                length = 0;
                top = null;
            } else
            {
                Node temp = original.top;
                while (temp != null)
                {
                    push(temp.data); // inserts into this
                    temp = temp.next;
                }
                temp = top;
                temp = temp.next;
                top.next = null;
                while (temp != null){
                    push(temp.data); // inserts into this
                    temp = temp.next;
                }
    
            }
        }
    
    0 讨论(0)
  • 2020-12-03 21:31
     /**
     * Copy constructor for the Stack class
     * @param original the Stack to copy
     * @postcondition a new Stack object which is
     * an identical, but distinct, copy of original
     */
    public Stack(Stack<T> original) {
        if (original.length == 0)
        {
            length = 0;
            top = null;
        } else
        {
            Node temp = original.top;
            while (temp != null)
            {
                push(temp.data); // inserts into this
                temp = temp.next;
            }
            temp = top;
            temp = temp.next;
            top.next = null;
            while (temp != null){
                push(temp.data); // inserts into this
                temp = temp.next;
            }
    
        }
    }
    
    0 讨论(0)
  • 2020-12-03 21:36

    The Stack class is a sub-class of AbstractList.

    Simply treat it like an AbstractList, iterate through the elements in the stack using the get(int index) method, from 0 to the length of your list/stack, and add the elements to the new stack.

    This won't copy the elements - it will add the elements to the new stack. If you need to copy the elements as well, you'll need to go another level deep and create copies of the elements, and add those to the new stack.

    You can do full (or "deep") copies, by using the clone method, but note that the object must implement the Clonable interface in order to get deep copies of objects.

    0 讨论(0)
  • 2020-12-03 21:36

    You want to use the clone method.

    0 讨论(0)
  • 2020-12-03 21:43

    Stack extends Vector, so you can just new up a new Stack and use .addAll(...) to copy the items:

    Stack<Type> newStack = new Stack<Type>();
    newStack.addAll(oldStack);
    
    0 讨论(0)
提交回复
热议问题