【问题标题】:Is my example a description for ADTs? (java) [closed]我的示例是 ADT 的描述吗? (java) [关闭]
【发布时间】:2020-09-20 13:00:31
【问题描述】:

我了解 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.");
    }
    
}

【问题讨论】:

  • 请关注contribution guidelines如何提出好问题。请不要插入代码的图像。请直接使用编码标签发布您的代码。
  • @flaxel 现在我相应地格式化了我的代码。所以现在值得投票吗? ;)
  • 非常好。我没有否决你的问题。
  • 附注:接口中的公共说明符是多余的

标签: java adt abstract-data-type


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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-31
    • 2012-10-19
    • 1970-01-01
    • 2017-09-05
    • 2017-10-25
    • 1970-01-01
    相关资源
    最近更新 更多