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

Java:為什么不能迭代迭代器?

Java: why can#39;t iterate over an iterator?(Java:為什么不能迭代迭代器?)
本文介紹了Java:為什么不能迭代迭代器?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我閱讀了 為什么 Java 的 Iterator 不是 Iterable? 和為什么枚舉不能迭代?,但我還是不明白為什么會(huì)這樣:

I read Why is Java's Iterator not an Iterable? and Why aren't Enumerations Iterable?, but I still don't understand why this:

void foo(Iterator<X> it) {
  for (X x : it) {
    bar(x);
    baz(x);
  }
}

無法實(shí)現(xiàn).換句話說,除非我遺漏了什么,否則上述內(nèi)容可能是很好且有效的語法糖:

was not made possible. In other words, unless I'm missing something, the above could have been nice and valid syntactic sugar for:

void foo(Iterator<X> it) {
  for (X x; it.hasNext();) {
    x = it.next();
    bar(x);
    baz(x);
  }
}

推薦答案

但我仍然不明白為什么這個(gè) [...] 無法實(shí)現(xiàn).

我可以看到幾個(gè)原因:

  1. Iterator 不可重用,因此 for/each 會(huì)消耗迭代器 - 也許不是不正確的行為,但對(duì)于那些不知道如何減少 for/each 糖分的人來說是不直觀的.
  2. Iterator 不會(huì)經(jīng)常在代碼中裸露",因此這會(huì)使 JLS 變得復(fù)雜而收益甚微(for/each 結(jié)構(gòu)本身就已經(jīng)夠糟糕了,兩者都適用Iterables 和數(shù)組).
  3. 有一個(gè)簡(jiǎn)單的解決方法.僅僅為此分配一個(gè)新對(duì)象似乎有點(diǎn)浪費(fèi),但是分配很便宜,并且在大多數(shù)情況下,逃逸分析甚至可以消除您的小成本.(不過,為什么他們沒有在 Iterables 實(shí)用程序類中包含這種解決方法,類似于 CollectionsArrays,我無法理解.)
  4. (可能不是真的 - 請(qǐng)參閱評(píng)論.)我似乎記得 JLS 只能引用 java.lang[需要引用] 中的內(nèi)容,所以他們必須在 java.lang 中創(chuàng)建一個(gè) Iterator 接口,該接口 java.util.Iterator 擴(kuò)展而不添加任何內(nèi)容.現(xiàn)在我們有兩個(gè)功能等效的迭代器接口.50% 使用裸迭代器的新代碼將選擇 java.lang 版本,其余使用 java.util 中的版本.隨之而來的是混亂,兼容性問題比比皆是,等等.
  1. Iterators are not reusable, so a for/each would consume the iterator - not incorrect behavior, perhaps, but unintuitive to those who don't know how the for/each is desugared.
  2. Iterators don't appear "naked" in code all that often so it would be complicating the JLS with little gain (the for/each construct is bad enough as it is, working on both Iterables and arrays).
  3. There's an easy workaround. It may seem a little wasteful to allocate a new object just for this, but allocation is cheap as it is and escape analysis would rid you even of that small cost in most cases. (Why they didn't include this workaround in an Iterables utility class, analogous to Collections and Arrays, is beyond me, though.)
  4. (Probably not true - see the comments.) I seem to recall that the JLS can only reference things in java.lang[citation needed], so they'd have to create an Iterator interface in java.lang which java.util.Iterator extends without adding anything to. Now we have two functionally equivalent iterator interfaces. 50% of the new code using naked iterators will choose the java.lang version, the rest use the one in java.util. Chaos ensues, compatibility problems abound, etc.

我認(rèn)為第 1-3 點(diǎn)非常符合 Java 語言設(shè)計(jì)理念的走向:不要讓新手感到驚訝,如果沒有明顯的收益超過成本,不要使規(guī)范復(fù)雜化, 不要用語言特性來做圖書館可以做的事情.

I think points 1-3 are very much in line with how the Java language design philosophy seems to go: Don't surprise newcomers, don't complicate the spec if it doesn't have a clear gain that overshadows the costs, and don't do with a language feature what can be done with a library.

同樣的論點(diǎn)可以解釋為什么 java.util.Enumeration 也不是 Iterable.

The same arguments would explain why java.util.Enumeration isn't Iterable, too.

這篇關(guān)于Java:為什么不能迭代迭代器?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項(xiàng)?)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
AES Error: Given final block not properly padded(AES 錯(cuò)誤:給定的最終塊未正確填充)
Detecting incorrect key using AES/GCM in JAVA(在 JAVA 中使用 AES/GCM 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 精品久久久久国产免费第一页 | 亚洲中午字幕 | 国产精品久久国产精品 | 草久免费视频 | 中文字幕福利视频 | 午夜丰满少妇一级毛片 | 欧美中文字幕一区二区 | 国产精品一区二区久久精品爱微奶 | 欧美综合一区二区 | 国产蜜臀97一区二区三区 | 九九亚洲| 狠狠躁躁夜夜躁波多野结依 | www.av在线| 亚洲少妇综合网 | av影音在线 | 免费一区二区三区 | 成人h视频在线 | 99精品热视频 | 欧美区在线 | 日韩精品久久一区二区三区 | 亚洲精品乱码久久久久久按摩 | 午夜视频在线观看一区二区 | 亚洲国产一区二区三区在线观看 | 麻豆精品久久 | 色橹橹欧美在线观看视频高清 | 97久久精品午夜一区二区 | 亚洲网站在线观看 | 午夜无码国产理论在线 | 亚洲一二视频 | 国产欧美在线视频 | 国产一区二区在线播放 | 蜜桃视频在线观看免费视频网站www | 欧美a在线 | 久久狼人天堂 | 夜夜干夜夜操 | 日韩美av | 精品日韩一区二区 | 黄色片免费看视频 | 精品一区二区三区四区视频 | 国产三级大片 | 日韩成人在线免费观看 |