实现Iterable接口 [英] Implementing the Iterable interface

查看:125
本文介绍了实现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接口的设计部分类。这个类的唯一目的是提供一个迭代属性的方法。

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.

在你的情况下,它必须保持数组中的当前迭代索引。您可以查看 <$ c来自commons-collections的$ c> ArrayIterator

In your case, it will have to hold the current iteration index in the array. You can look at ArrayIterator from commons-collections

这篇关于实现Iterable接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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