我的示例描述了ADT吗? java

问题描述

我了解ADT(抽象数据类型)会隐藏/抽象方法的实现。那么,接口是ADT,对吗?所以我的问题是:

  1. 我的示例是否将接口 MyStack 演示为ADT及其在类 ImplementationOfMyStack 中的实现,对吗?
  2. 如果问题1是,那么为什么Java库中有一个 Stack 类?我的困惑是我可以实例化 Stack 类以使用push(),pop(),peek()而不用像我的示例那样编码实现。因此,我认为 Stack 类具有其实现,因此是数据结构而不是ADT。
public interface MyStack {  
    public void push();
    public void pop();
    public void peek();
}
public class ImplementationOfMyStack implements MyStack {

    public void push() {
        System.out.println("Code an implementation here to push a new item on top.");
        System.out.println("The implementation is a data structure e.g. linked List.");
    }

    public void pop() {
        System.out.println("Code an implementation here to pop a new item from top.");
        System.out.println("The implementation is a data structure e.g. linked List.");
    }

    public void peek() {
        System.out.println("Code an implementation here to peek a new item from top.");
        System.out.println("The implementation is a data structure e.g. linked List.");
    }
    
}

解决方法

  1. 是的,接口是abstract data type
  2. 您的实现是正确的。
  3. java库中始终有一个Stack class。堆栈是一种通用的数据结构,表示对象的LIFO(后进先出)集合,允许在恒定时间内推送/弹出元素。 (我建议使用Deque interface。Deque定义了一套更完整和一致的LIFO操作。)

相关问答

错误1:Request method ‘DELETE‘ not supported 错误还原:...
错误1:启动docker镜像时报错:Error response from daemon:...
错误1:private field ‘xxx‘ is never assigned 按Alt...
报错如下,通过源不能下载,最后警告pip需升级版本 Requirem...