使用不兼容的类型实现堆栈

时间:2018-02-23 07:02:01

标签: java

我试图实现堆栈,不知怎的,当我尝试运行代码时,编译器说它是“不兼容的类型”。请帮忙

class MyArrayStack<E> extends ArrayList<E> implements Stack<E> {
   int key,data,size = 0; 
   int[] array = new int[12];
   int element;

   public void push(E e) {
      data = (int) e;
      array[size] = data;
      size++;
      System.out.println("The Element is inserted Successfully");
   }
}

1 个答案:

答案 0 :(得分:0)

java.util.Stack是一个类,而不是一个接口。您可以删除ArrayList

import java.util.Stack;

class MyArrayStack<E> extends Stack<E> {

    int key, data, size = 0;

    int[] array = new int[12];

    int element;

    public E push(E e) {

        data = (int) e;
        array[size] = data;
        size++;
        System.out.println("The Element is inserted Successfully");

        return e;
    }
}