Implementing the Iterable interface(实现 Iterable 接口)
问题描述
我刚刚在一份旧试卷中发现了这个试题,并且正在为即将到来的考试做准备.我想不通:
I just found this exam question in an old exam paper and am readying myself for an upcoming exam. I cannot figure it out :
下面描述了一个人为的部分类,它实现了 Iterable 接口.此类的唯一目的是提供一种方法来迭代属性 things.
The following depicts a contrived partial class which implements the Iterable interface. The only purpose of this class is to provide a method to iterate over the attribute things.
我们需要在课堂上填写两件事来完成它.这里是类
There are two things we need to fill in in the class to finish it. Here is the class
private class PartialIterableClass /*FILL IN */ {
private String[] things;
public PartialIterableClass( String[] things ){
this.things = things;
}
/*FILL IN 2*/
}
我猜它应该类似于:
private class PartialIterableClass implements Iterable<PrivateIterableClass> {
private String[] things;
public PartialIterableClass( String[] things ){
this.things = things;
}
public Iterator<PartialIterableClass> iterator( ){
return new Iterator<PartialIterableClass>( ) {
}
}
}
不过,我不确定如何充实这个问题的答案,有人可以帮忙吗?
I'm not really sure how to flesh out an answer to this question though, can anybody help?
推荐答案
你的 Iterator
必须实现 Iterator
接口中的所有方法,才能封装迭代逻辑.
Your Iterator
must implement all the methods from the Iterator
interface in order to encapsulate the iteration logic.
在您的情况下,它必须保存数组中的当前迭代索引.您可以查看 ArrayIterator
来自 commons-collections
In your case, it will have to hold the current iteration index in the array. You can look at ArrayIterator
from commons-collections
这篇关于实现 Iterable 接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:实现 Iterable 接口


基础教程推荐
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01