-1

I'm creating an array based stack class in java, but I'm also forced to use the class

public class MyStack<E> implements Stack<E>. 

My pop and peek are also in form of public E pop()/ public E peek().

I can't figure out how to use formats and create an array.

The size of array has to be 128, and I tried setting

private E[] Stack = new Object[128]

but it gives bunch of warnings and I can't search how to deal with this Object type. I tried using it by typecasting (int) or (char) each time I use item in the stack, but I'm getting a ClassCastException. For example, I tried using (int) tmp = (int)MyStack.pop(); and am getting the ClassCastException when I use the Object type.

Can anyone help me understand how this Stack, or works (or give a directing link to some well-written explanation) or tell me anywhere else I'm misguided?

Thanks!

MalaKa
  • 3,574
  • 2
  • 16
  • 31
Kwon
  • 69
  • 7
  • 1
    Your question isn't very clear, what exactly do you want to do? – Nicholas K Oct 07 '18 at 10:12
  • I read through the link, and I think that answers most of my question, although I still don't understand how to solve the ClassCastException issue. I'll have to look up for that. Thanks for the link! – Kwon Oct 07 '18 at 10:34

1 Answers1

0

Assuming you are using java.util.Stack, which is a class and not an interface, the code would be:

public class MyStack extends Stack<TypeName>

Susmit Agrawal
  • 3,280
  • 2
  • 10
  • 25