java - 使用接口(interface)创建 Stack Generic ArrayList

标签 java arraylist interface stack

这是一个关于泛型和实现堆栈(LIFO)的练习问题,不用于现实生活,而是用于教育目的。

所有接口(interface)方法不能采用不同的参数,并且必须按所示方式实现。

接口(interface)堆栈

import java.util.Iterator;
interface Stack<E>{
    public boolean push(E x);
    public boolean pop();
    public E top();
    public boolean empty();
    public boolean full();
    public Iterator<E> iterator();
}

我不知道如何使用 iterator 方法,也不知道为什么我在这里需要它,因为我的方法都没有使用它?

我已经完成了下面代码中的所有其他方法

MyStack

import java.util.ArrayList;
import java.util.Iterator;


public class MyStack<E> implements Stack<E>,Iterable<E> {

    //Creating a ArrayList which is bound using a int
    //in the constructor and size keeps track of the 
    //amount of elements in the ArrayLIst
    private int size;
    private ArrayList<E> stack;

    public MyStack(int size) {
        stack = new ArrayList<E>(size);
        size = 0;
    }

    @Override
    public boolean push(E x) {
        //Check to make sure the size of the bounded Arraylist
        //has not been exceeded, if not add element 
        if(size == stack.size()) {
            stack.add(x);
            size++;
            return true;
        }

        return false;

    }

    @Override

    public boolean pop() {
        //If the list is not empty remove the last element 
        if(this.empty()) {
            System.out.println("No elements to remove");
            return false;
        }

        stack.remove(stack.size()-1);
        size--;
        return true;
    }

    @Override
    public E top() {
        //If the stack is not empty return the top element without removing it
        if (empty()) {
            return null;
        }       
        else
            return stack.get(stack.size()-1);       
    }

    @Override
    public boolean empty() {

        return stack.isEmpty();
    }

    @Override
    public boolean full() {

        //If size is equal to the size of the stack
        //it is deemed full
        if(size == stack.size()) {
            System.out.println("The stack is full");
            return true;
        }

        return false;
    }

    @Override
    public Iterator iterator() {
        // TODO Auto-generated method stub
        return null;
    }


    @Override
    public String toString() {
        return "MyStack [size=" + size + ", stack=" + stack + "]";
    }

}

测试代码

    stack.push(2);
    stack.push(4);
    stack.push(5);
    stack.push(7);

    System.out.println(stack.toString());

    System.out.println(stack.top());
    stack.pop();
    stack.empty();

    System.out.println(stack.toString());

所有方法似乎都能正常工作

MyStack [大小=4,堆栈=[2,4,5,7]] 7 MyStack [size=4, stack=[2, 4, 5]]

我唯一的困惑是在我的书中,它显示了一个添加到头部的图表,其中我的 Arraylist 添加到尾部,但将尾部视为头部,这样可以吗?

这有点令人困惑,因为他们在本章中还讨论了一个LinkedList,这就是这个daigram对我来说代表的东西,而不是一个ArrayList

enter image description here

书中使用的方法

@Override
    public Iterator<E> iterator() {
          ArrayList<E> tmp = new ArrayList<>();

            int ind = stack.size();
            while(ind > 0){
              tmp.add(stack.get(ind-1));
                ind--;
            }
            System.out.println(tmp.toString());
            return tmp.iterator();
    }

最佳答案

正如 dan1st 在评论中已经提到的,迭代器用于遍历/迭代整个堆栈并提供一些与堆栈元素交互的函数。

查看 Iterator interface 的文档.

一般来说,接口(interface)(您的堆栈)提供的方法不一定必须在您自己的实现中使用,而是为用户提供一些与您的实现交互的操作。

The only other confusion I have is in my book it shows a diagram of adding to the head where my Arraylist adds to the tail but treats the tail like it was the head is this ok?

如果需要向头部添加新元素,那就这么做吧。它改变堆栈元素的顺序。 这是如何在书中使用 iterate() 实现的一个很好的提示。

关于java - 使用接口(interface)创建 Stack Generic ArrayList,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60636436/

相关文章:

java - 短路评估的性能影响

java - 仅在代码的特定位置检测nfc

java - Jersey : How to get POST parameters on server side?

java - Java 中无法访问的代码

java - 在不实例化(未知)具体类型的情况下从方法返回接口(interface)对象

java - 泛型 "method does not override any method from its superclass"

go - 如何解决导入周期不允许的问题,尽管我正在使用界面?

java - 比较数组并删除重复项

java - 无法使用 Collections.copy() 方法将数据从一个列表复制到另一个列表

android - 如何将图像传递到 Android 中的 ArrayList<String>?