TheAmateurProgrammer TheAmateurProgrammer - 1 year ago 109
Java Question

How do I clone a stack of Integers?

This seemingly extremely easy problem has baffled me for hours. I'm trying to clone a stack of integer objects. I've tried

Stack<Integer> newStack = (Stack<Integer>)oldStack.clone();

However that gives me an error saying
clone() has protected access in java.lang.Object

My best guess is that because Integer doesn't implement
, so I'm getting that error. So how should I clone a stack of integers then?

Answer Source

Somehow there's another Stack class hidden somewhere. Don't know why, but import Stack using

import java.util.Stack

instead of relying on

import java.util.*
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download