Java中产量,产量返回在Java

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.

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?

I'm looking to be able to write code ala

LinkedList authors = new LinkedList();

for (Iterator i = authors.Values ; i.HasNext())

doSomethingWith(i.Value);

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

LinkedListObject current = first;

While (current != null){

yield return current.getValue();

current = current.getNext()

}

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

解决方案

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.

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.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值