Java 作为返回类型可迭代
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23162559/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Iterable as a return type
提问by Tolga Tamer
I'm having trouble understanding a method. I have methods that I need to fill out, but I don't really understand the first one. How can Iterable be a return type and how is it used ? An example would be great..
我无法理解一种方法。我有一些方法需要填写,但我不太了解第一个。Iterable 如何成为返回类型以及如何使用它?一个例子会很棒..
@Override
public Iterable<ClientInterface> getNeighbours() {
return null;
}
@Override
public void addNeighbour(ClientInterface newNeighbour){
//TODO Implement me!
}
@Override
public void removeNeighbour(String clientID) {
//TODO Implement me!
}
采纳答案by Mike
It looks like your class should have an implementation of Iterable<ClientInterface>
as a class member, like ArrayList.
看起来您的类应该具有Iterable<ClientInterface>
作为类成员的实现,例如 ArrayList。
Let's use this as an example:
让我们以此为例:
public class Bus {
private ArrayList<Person> riders;
... //constructors and other methods
public Iterable<Person> getRiders() {
return riders;
}
... //other methods
}
回答by d3dave
Iterable<T>
is an interface. What this means is that when you receive it as a return value from a method, you actually receive an implementationof it. So, although you don't know what the nameof that implementation is (the name of the class that implements the Iterable<T>
interface), you can still access the methods that it implements, and act upon it.
Iterable<T>
是一个接口。这意味着当您从方法接收它作为返回值时,您实际上会收到它的实现。所以,虽然你不知道什么名字,落实的是(类的名称实现了Iterable<T>
接口),您可以访问这些方法,它实施并就此采取行动。
Some built-in implementations include ArrayList
, LinkedList
, HashSet
, PriorityQueue
, and Stack
.
一些内置的实现包括ArrayList
,LinkedList
,HashSet
,PriorityQueue
,和Stack
。
When you need to return an interface type, you must return an implementation of it.
当你需要返回一个接口类型时,你必须返回它的一个实现。
回答by Marco Acierno
Everything can be a return type: an enum, a class, an interface, an int, an exception etc.
一切都可以是返回类型:枚举、类、接口、int、异常等。
Iterable
is an interface
which could be used to use the return in a foreach call (that's why you can use for(T something : AnArrayList)
because ArrayList<T>
implements Iterable<T>
)
Iterable
是一个interface
可用于在 foreach 调用中使用返回值(这就是您可以使用的for(T something : AnArrayList)
原因,因为ArrayList<T>
implements Iterable<T>
)
Now, my answer contains an example:
现在,我的回答包含一个例子:
We have a method getNeighbours
which returns Iterable<ClientInterface>
我们有一个getNeighbours
返回的方法Iterable<ClientInterface>
public Iterable<ClientInterface> getNeighbours()
{
return new Iterable<ClientInterface>();
}
Well, ok since Iterable
is just an interface we need to implement methods or use an implementation.
好吧,好吧,因为Iterable
它只是一个我们需要实现方法或使用实现的接口。
Since it's something which we should do manually we should implement the methods by ourself.
由于这是我们应该手动完成的事情,我们应该自己实现这些方法。
The only method (in Java8, there are 3 methods but we will ignore it) is iterator()
which returns an iterator
.
唯一的方法(在 Java8 中,有 3 个方法,但我们将忽略它)是iterator()
返回一个iterator
.
public Iterable<ClientInterface> getNeighbours()
{
return new Iterable<ClientInterface>()
{
@Override
public Iterator<ClientInterface> iterator()
{
return null;
}
};
}
Iterator
is another interface which is used to provide the logic to iterate over the collection, a list of items etc.
Iterator
是另一个接口,用于提供迭代集合、项目列表等的逻辑。
We are forced to implements two methods: hasNextand next
我们被迫实现两个方法:hasNext和next
hasNext
is used to determinate if there are more items to iterate over, next
is used to iterate over it.
hasNext
用于确定是否有更多项目要迭代,next
用于迭代它。
public Iterable<ClientInterface> getNeighbours()
{
return new Iterable<ClientInterface>()
{
@Override
public Iterator<ClientInterface> iterator()
{
return new Iterator<ClientInterface>()
{
@Override
public boolean hasNext()
{
return false;
}
@Override
public ClientInterface next()
{
return null;
}
};
}
};
}
We here need to remember which was our last position so we would create a field inside our Iterator.
我们在这里需要记住哪个是我们的最后一个位置,以便我们在迭代器中创建一个字段。
public Iterable<ClientInterface> getNeighbours()
{
return new Iterable<ClientInterface>()
{
@Override
public Iterator<ClientInterface> iterator()
{
return new Iterator<ClientInterface>()
{
private int position;
@Override
public boolean hasNext()
{
return false;
}
@Override
public ClientInterface next()
{
return null;
}
};
}
};
}
Here the problem: What we should iterate? It depends to you, an example could be:
这里的问题是:我们应该迭代什么?这取决于你,一个例子可能是:
public Iterable<ClientInterface> getNeighbours()
{
return new Iterable<ClientInterface>()
{
@Override
public Iterator<ClientInterface> iterator()
{
return new Iterator<ClientInterface>()
{
private int position;
private ClientInterface[] items = new ClientInterface[]{new ClientInterface(), new ClientInterface()};
@Override
public boolean hasNext()
{
return position != items.length;
}
@Override
public ClientInterface next()
{
if (!hasNext()) throw new NoSuchElementException();
return items[position++];
}
};
}
};
}
Note here how we created an array of items and used our two methods hasNext
and next
to provide a way to iterate over it.
请注意我们如何创建一个项目数组并使用我们的两种方法hasNext
并next
提供一种对其进行迭代的方法。
Every call of next
increment the internal pointer, and our hasNext method just checks if the pointer reached the end of the array.
每次调用next
increment 内部指针,我们的 hasNext 方法只检查指针是否到达数组的末尾。
Collections like ArrayList
, LinkedList
etc. already did the job for you and better (implements remove
method) you can get this iterator by using ArrayList.iterator()
收藏喜欢ArrayList
,LinkedList
等已经做的工作,为您和更好的(实现remove
方法),你可以通过使用得到这个迭代器ArrayList.iterator()
Now you could write something like:
现在你可以这样写:
for (ClientInterface el : yourClass.getNeighbours())
{
System.out.println(el);
}