TheAmateurProgrammer TheAmateurProgrammer - 4 months ago 6x
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?


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.*