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

在迭代器上調用 next 一次 vs 多次

Calling next on an Iterator once vs multiple times(在迭代器上調用 next 一次 vs 多次)
本文介紹了在迭代器上調用 next 一次 vs 多次的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

為什么第一個版本的代碼不起作用

Why this first version of the code does not work

          // returns the longest string in the list (does not work!)

         public static String longest(LinkedList<String> list) {
              Iterator<String> itr = list.iterator();
              String longest = itr.next(); // initialize to first element

              while (itr.hasNext()) {
                  if (itr.next().length() > longest.length()) {
                     longest = itr.next();
                  } 
              }
              return longest;
         }

但是第二個版本的代碼會嗎?

but the second version of the code will ?

       // this version of the code is correct

       while (itr.hasNext()) {
           String current = itr.next();
           if (current.length() > longest.length()) {
               longest = current;
           }
       }

推薦答案

當你的 if 條件為 true 時,你正在調用 next() 兩次:

When your if condition is true, you are calling next() twice:

if (itr.next().length() > longest.length()) {
    longest = itr.next();
...

因此,在 if 正文中,您將 next 值的長度(而不是當前值)分配給 longest.

Thus, inside the if body, you are assigning the length of the next value, not the current one, to longest.

Iterator.next() 從集合中返回當前值,但同時將迭代器前進到下一個元素.

Iterator.next() returns the current value from the collection, but at the same time, advances the iterator to the next element.

請注意,如果沒有下一個元素,您對 itr.next() 的第二次調用可能會拋出 NoSuchElementException.在您使用 Iterator.hasNext() 檢查是否有可用的下一個元素之后,始終只調用 Iterator.next() 一次.

Note that your second call to itr.next() might throw a NoSuchElementException if there is no next element. Always call Iterator.next() only once after you have checked with Iterator.hasNext() whether there is a next element available.

更好的是,使用 foreach 循環來處理樣板:

Even better, use the foreach loop which handles all the boilerplate:

for (String current : list) {
    ....
    // "current" now points to the current element
}

這篇關于在迭代器上調用 next 一次 vs 多次的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Java Remove Duplicates from an Array?(Java從數組中刪除重復項?)
egit plugin for Eclipse(Eclipse 的 egit 插件)
Gitlab cannot open git-upload-pack error(Gitlab 無法打開 git-upload-pack 錯誤)
How to fix Invocation failed Unexpected Response from Server: Unauthorized in Android studio(如何修復調用失敗來自服務器的意外響應:在 Android 工作室中未經授權)
How to add GitLab repository in Eclipse?(如何在 Eclipse 中添加 GitLab 存儲庫?)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
主站蜘蛛池模板: 性高湖久久久久久久久 | 久久久国产一区二区三区四区小说 | 国产精品久久久久久久久婷婷 | 皇色视频在线 | jvid精品资源在线观看 | 久久久久久久久久久久久久av | 成人av电影在线 | 免费观看日韩精品 | 午夜影院网站 | 亚洲免费在线视频 | 国产高清在线精品一区二区三区 | 国产毛片久久久久久久久春天 | 黄色三级毛片 | 国产视频一区二区在线观看 | 天天干com| 亚洲+变态+欧美+另类+精品 | 亚洲精品4| 国产精品久久久久久影院8一贰佰 | 99精品在线观看 | 亚洲一区二区三区在线视频 | 亚洲五码在线 | 国产精品日韩欧美 | 欧美日韩国产在线 | 精品伊人久久 | 丝袜美腿一区二区三区动态图 | 久草成人 | 日本视频免费 | 日韩免费av网站 | 91大神在线看 | 日日摸夜夜添夜夜添特色大片 | 国产精品中文字幕在线播放 | 国产 日韩 欧美 中文 在线播放 | 国产精品久久久亚洲 | 日韩一区二区三区视频在线观看 | 日韩中文字幕 | 免费v片在线观看 | 久久综合九色综合欧美狠狠 | 99爱在线观看 | 二区三区视频 | 国产你懂的在线观看 | 中文在线一区二区 |