我了解堆栈是如何工作的,但我必须编写 push、pop 和 peek 的方法,然后在驱动程序类中实现它们。这就是令人困惑的地方。这是我的Stack
课:
public class Stack implements StackInterface
{
private final int ARRAY_SIZE = 9;
private String[] movies = new String[ARRAY_SIZE]; // Hold movie titles
private int top = 0;
/**
* Constructor
* @param moviesIn
*/
public Stack(String[] moviesIn)
{
movies = moviesIn;
}
/**
* Test for full stack
*/
public void push(String moviesIn)
{
if (top >= movies.length)
System.out.println("ERROR: Stack is full");
top++;
movies[top] = moviesIn;
}
/**
* Test for empty stack
*/
public String pop()
{
if (top == 0) {
System.out.println("ERROR: Stack is empty");
return " ";
}
top--;
return movies[top];
}
public void peek()
{
// ???
}
}
main()
到目前为止,这是我的方法中的内容:
public static void main(String[] args)
{
String[] movies = {"Amour", "*Argo", "Beasts of the Southern Wild", "Django Unchained", "Les Misérables", "Life of Pi", "Lincoln", "Silver Linings Playbook", "Zero Dark Thirty"};
Stack oscarStack = new Stack(movies);
oscarStack.push(movies);
}
我以为我可以将一个对象传递给堆栈,但它似乎并没有那样工作。那么如何将oscarStack
对象推入堆栈呢?还是我必须单独推动每个字符串?在继续我的在线研究中,似乎堆栈构造函数只能创建一个空堆栈。这就是我不能传递对象参数的原因吗?