久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

是一個“無限"的迭代器設計不好?

Is an quot;infinitequot; iterator bad design?(是一個“無限的迭代器設計不好?)
本文介紹了是一個“無限"的迭代器設計不好?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

提供無限"的 Iterator 實現通常被認為是不好的做法嗎?即對 hasNext() always(*) 的調用在哪里返回 true?

Is it generally considered bad practice to provide Iterator implementations that are "infinite"; i.e. where calls to hasNext() always(*) return true?

通常我會說是",因為調用代碼可能會出現異常行為,但在下面的實現中,hasNext() 將返回 true,除非調用者從列表中刪除迭代器所在的所有元素初始化為;即有一個終止條件.你認為這是對 Iterator 的合法使用嗎?盡管我想有人可能會說它不直觀,但它似乎并沒有違反合同.

Typically I'd say "yes" because the calling code could behave erratically, but in the below implementation hasNext() will return true unless the caller removes all elements from the List that the iterator was initialised with; i.e. there is a termination condition. Do you think this is a legitimate use of Iterator? It doesn't seem to violate the contract although I suppose one could argue it's unintuitive.

public class CyclicIterator<T> implements Iterator<T> {
  private final List<T> l;
  private Iterator<T> it;

  public CyclicIterator<T>(List<T> l) {
    this.l = l;
    this.it = l.iterator();
  }

  public boolean hasNext() {
    return !l.isEmpty();
  }

  public T next() {
    T ret;

    if (!hasNext()) {
      throw new NoSuchElementException();
    } else if (it.hasNext()) {
      ret = it.next();
    } else {
      it = l.iterator();
      ret = it.next();
    }

    return ret;
  }

  public void remove() {
    it.remove();
  }
}

(迂腐)編輯

有些人評論了如何使用 Iterator 從無界序列(如斐波那契序列)生成值.但是,Java Iterator 文檔指出 Iterator 是:

Some people have commented how an Iterator could be used to generate values from an unbounded sequence such as the Fibonacci sequence. However, the Java Iterator documentation states that an Iterator is:

集合上的迭代器.

現在你可以說斐波那契數列是一個無限集合,但在 Java 中我會將集合等同于 java.util.Collection 接口,它提供了諸如 size() 暗示一個集合必須是有界的.因此,使用 Iterator 作為無界序列的值生成器是否合法?

Now you could argue that the Fibonacci sequence is an infinite collection but in Java I would equate collection with the java.util.Collection interface, which offers methods such as size() implying that a collection must be bounded. Therefore, is it legitimate to use Iterator as a generator of values from an unbounded sequence?

推薦答案

我認為它完全合法 - Iterator 只是一個東西"流.為什么流必須有界?

I think it is entirely legitimate - an Iterator is just a stream of "stuff". Why should the stream necessarily be bounded?

許多其他語言(例如 Scala)都內置了無限流的概念,并且可以對其進行迭代.例如,使用 scalaz

Plenty of other languages (e.g. Scala) have the concept of unbounded streams built in to them and these can be iterated over. For example, using scalaz

scala> val fibs = (0, 1).iterate[Stream](t2 => t2._2 -> (t2._1 + t2._2)).map(_._1).iterator
fibs: Iterator[Int] = non-empty iterator

scala> fibs.take(10).mkString(", ") //first 10 fibonnacci numbers
res0: String = 0, 1, 1, 2, 3, 5, 8, 13, 21, 34

就最小意外原則而言,我認為這完全取決于上下文.例如,我希望這個方法返回什么?

public Iterator<Integer> fibonacciSequence();

這篇關于是一個“無限"的迭代器設計不好?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 欧美特级黄色 | 精品国产黄a∨片高清在线 成人区精品一区二区婷婷 日本一区二区视频 | 亚洲国产欧美一区二区三区久久 | 日韩视频一区二区 | 韩日一区二区 | 亚洲在线| 国产成人一区 | 一区二区三区四区在线视频 | 中文字幕久久精品 | 蜜臀久久 | 亚洲九色 | 亚洲香蕉 | 毛片一级片| 国产电影一区二区在线观看 | 爱综合| 久久69精品久久久久久国产越南 | 久久高清国产 | 国产午夜精品一区二区三区嫩草 | 国产亚洲精品精品国产亚洲综合 | 亚洲国产视频一区二区 | 日本一区高清 | 国产美女黄色片 | 日本不卡一二三 | 久久无毛| 欧美三区视频 | 四虎影院欧美 | 国外成人在线视频网站 | 亚洲精品在线免费看 | 瑟瑟激情 | 国产在线精品一区二区三区 | 91av免费观看| 亚洲综合一区二区三区 | 超碰在线97国产 | 亚洲欧美一区在线 | 日韩av成人 | 日韩免费视频一区二区 | 日韩爱爱网站 | 国产在线观看不卡一区二区三区 | 99re视频在线 | 欧美高清视频在线观看 | 欧美中文字幕在线观看 |