[英]How to implement an interface for two classes with an iterator
I'm trying out Interfaces in java and I want to implement a common interface for a really simple stack, with pop()
and push()
methods and an iterator.我正在尝试 java 中的接口,我想为一个非常简单的堆栈实现一个通用接口,使用
pop()
和push()
方法和一个迭代器。
The problem is that I don't know how to specify the iterator in the interface.问题是我不知道如何在接口中指定迭代器。 No matter which way I try, I get
无论我尝试哪种方式,我都会得到
Main.java:32: error: for-each not applicable to expression type
for (Integer i : ss)
^
required: array or java.lang.Iterable
found: Stack<Integer>
The code is as follows:代码如下:
interface Stack<T> {
boolean push(T t);
boolean pop();
//Iterator<T> iterator(); // How to indicate it needs, and will have, an iterator?
}
public class DynamicStack<T> implements Iterable<T>, Stack<T>
{
// implementation-specific variables go here
public DynamicStack() {
//...
}
public boolean push(T t) {
//...
}
public boolean pop() {
//...
}
private class StackIterator implements Iterator<T> {
DynamicStack<T> stk;
//...
// Iterator constructor
private StackIterator(DynamicStack<T> stk)
{
//...
}
public boolean hasNext()
{
//...
}
public T next() throws NoSuchElementException
{
//...
}
public void remove() throws UnsupportedOperationException
{
throw new UnsupportedOperationException(); // I chose not to implement this one
}
}
// Iterator method
public Iterator<T> iterator()
{
return new StackIterator(this);
}
}
public class StaticStack<T> implements Iterable<T>, Stack<T>
{
// implementation-specific variables go here
public StaticStack()
{
//...
}
public boolean push(T t)
{
//...
}
public boolean pop()
{
//...
}
private class StackIterator implements Iterator<T>
{
StaticStack<T> stk;
//...
private StackIterator(StaticStack<T> stk)
{
//...
}
public boolean hasNext()
{
//...
}
public T next() throws NoSuchElementException
{
//...
}
public void remove() throws UnsupportedOperationException
{
//...
}
}
// Iterator method
public Iterator<T> iterator()
{
return new StackIterator(this);
}
}
Main simply does this, after creating a few stacks of each type and adding a few elements:在为每种类型创建几个堆栈并添加一些元素之后,Main 只是简单地执行此操作:
public static void showStuff(Stack<Integer> ss)
{
for (Integer i : ss)
System.out.print(i+" ");
System.out.println();
}
In your test class, you are operating against Stack
interface, so that is the one that needs to conform to Iterable
.在您的测试 class 中,您正在针对
Stack
接口进行操作,因此这是需要符合Iterable
的接口。 In this case it doesn't help if StaticStack
or DynamicStack
implement it if Stack
does not.在这种情况下,如果
Stack
没有, StaticStack
或DynamicStack
实现它也无济于事。
To get Stack to be able to be used as Iterable just change your Stack to extend Iterable:要使 Stack 能够用作 Iterable,只需更改您的 Stack 以扩展 Iterable:
public interface Stack<T> extends Iterable<T> {
boolean push(T t);
boolean pop();
}
and和
public class StaticStack<T> implements Stack<T>
and the code runs just fine:并且代码运行得很好:
public class Tester {
public static void main(String args[]) {
Stack<Integer> ss = new StaticStack<>();
for (Integer i : ss)
System.out.print(i+" ");
System.out.println();
}
}
You need you class to implement Iterable<T>
, which has the iterator()
method, which returns Iterator<T>
.您需要 class 来实现
Iterable<T>
,它具有iterator()
方法,该方法返回Iterator<T>
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.