产量返回在Java [英] Yield Return In Java

查看:192
本文介绍了产量返回在Java的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我创建使用泛型在Java中的链表,现在我希望能够遍历列表中的所有元素。在C#中,同时会在列表中包含的元素列表中,我会用收益回报率链接列表中。

I've created a linked list in java using generics, and now I want to be able to iterate over all the elements in the list. In C# I would use yield return inside the linked list while going over the list of elements contained in the list.

我将如何去创造上面,我可以通过包含在链表中的所有项目迭代的一个java版本?

How would I go about creating a java version of the above where I can iterate over all the items contained in the linked list?

我期待能够编写代码ALA

I'm looking to be able to write code ala

LinkedList<something> authors = new LinkedList<something>();
for (Iterator<something> i = authors.Values ; i.HasNext())
      doSomethingWith(i.Value);

和在想,值属性/法将类似于包含代码

And was thinking that the Value 'property'/method would consist of code resembling

LinkedListObject<something> current = first;
While (current != null){
 yield return current.getValue();
 current = current.getNext()
}



编辑:请注意,我没兴趣使用任何第三方的API。内置唯一的Java功能。

Notice that I'm not interested in using any 3rd party APIs. Built-in java functionality only.

推荐答案

我失去了一些东西在这里?已经有java.util.LinkedList中,它完全仿制药启用-,它有它返回一个迭代器的方法。

Am I missing something here? There is already java.util.LinkedList, it is fully generics-enabled, and it has a method which returns an Iterator.

如果你真的想重新发明轮,我建议你考虑创造一个Li​​nkedListIterator类,可能实现的ListIterator。它会记得链表内的当前位置和前进它每次连续调用。

If you really want to re-invent the wheel, I'd suggest you look into to creating a LinkedListIterator class, probably implementing ListIterator. It would remember its current position within the linked list and advance it on each successive call.

这篇关于产量返回在Java的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆