public static void stutter(Stack<Integer> st){
Stack<Integer> stack = new Stack<Integer>();
Iterator value = st.iterator();
Integer currValue;
while (value.hasNext()) {
currValue = value.next();
stack.push(currValue);
stack.push(currValue);
}
st.addAll(stack);
}```
so im trying to pop a stack like
1, 2 ,3 ,4
to 1 1 2 2 3 3 4 4
my error is cant convert object to integer
but if i store as an object
it cant convert integer to object
#Stacks issue
4 messages · Page 1 of 1 (latest)
⌛ This post has been reserved for your question.
Hey @misty granite! Please use
/closeor theClose Postbutton above when your problem is solved. Please remember to follow the help guidelines. This post will be automatically closed after 300 minutes of inactivity.
TIP: Narrow down your issue to simple and precise questions to maximize the chance that others will reply in here.
you are bound to run into issues with your current approach. Even if you correctly specify your iterator Iterator<Integer> value = st.iterator() , Stack#push will throw a ConcurrentModificationException. You can either create a new Stack object which you will push each element twice or if you really need to use the same object use a simple for-loop. Stack extends Vector so it inherits all methods.