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

在迭代列表時將元素添加到列表中.(爪哇)

Add elements to a List while iterating over it. (Java)(在迭代列表時將元素添加到列表中.(爪哇))
本文介紹了在迭代列表時將元素添加到列表中.(爪哇)的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

可能重復(fù):
Java:在迭代期間向集合添加元素

我的問題是我想在迭代時用新元素擴(kuò)展一個列表,并且我希望迭代器繼續(xù)使用我剛剛添加的元素.

My problem is that I want to expand a list with new elements while iterating over it and I want the iterator to continue with the elements that I just added.

據(jù)我了解,ListIterator.add() 在列表中的當(dāng)前元素之前添加一個元素,而不是在它之后.是否有可能以其他方式實現(xiàn)這一目標(biāo)?

From my understanding the ListIterator.add() adds an element before the current element in the list, not after it. Is it possible to achieve this in some other way?

推薦答案

您不能在使用 Iterator 迭代時修改 Collection,除了 Iterator.remove().

You can't modify a Collection while iterating over it using an Iterator, except for Iterator.remove().

但是,如果您使用 listIterator() 方法,它返回一個 ListIterator,并迭代你有更多的選項可以修改.來自 add() 的 javadoc:

However, if you use the listIterator() method, which returns a ListIterator, and iterate over that you have more options to modify. From the javadoc for add():

新元素被插入到隱式游標(biāo)之前:... 對 previous() 的后續(xù)調(diào)用將返回新元素

The new element is inserted before the implicit cursor: ... a subsequent call to previous() would return the new element

鑒于此,這段代碼應(yīng)該可以將新元素設(shè)置為迭代中的下一個:

Given that, this code should work to set the new element as the next in the iteration:

ListIterator<T> i;
i.add(e);
i.previous(); // returns e
i.previous(); // returns element before e, and e will be next

這將起作用,除非列表開始迭代為空,在這種情況下將沒有前一個元素.如果這是一個問題,您將不得不維護(hù)某種標(biāo)志來指示這種極端情況.

This will work except when the list starts iteration empty, in which case there will be no previous element. If that's a problem, you'll have to maintain a flag of some sort to indicate this edge case.

這篇關(guān)于在迭代列表時將元素添加到列表中.(爪哇)的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

Java Remove Duplicates from an Array?(Java從數(shù)組中刪除重復(fù)項?)
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(如何修復(fù)調(diào)用失敗來自服務(wù)器的意外響應(yīng):在 Android 工作室中未經(jīng)授權(quán))
How to add GitLab repository in Eclipse?(如何在 Eclipse 中添加 GitLab 存儲庫?)
AES encryption, got extra trash characters in decrypted file(AES 加密,解密文件中有多余的垃圾字符)
主站蜘蛛池模板: 在线播放国产一区二区三区 | 久久精品中文字幕 | 久久国产精品视频 | 黄色大片免费播放 | 免费不卡av | 欧美综合一区 | 91久久夜色精品国产网站 | yiren22 亚洲综合 | 免费xxxx大片国产在线 | 97色在线观看免费视频 | 99在线精品视频 | 欧美一级二级视频 | 欧美精品一区三区 | 欧美精品一区二区三区在线四季 | 中文字幕亚洲欧美日韩在线不卡 | 国产a级黄色录像 | 中文字幕在线人 | 国产真实精品久久二三区 | 国产一区二区三区四区区 | 亚洲福利网站 | 欧美日高清 | 日日噜噜夜夜爽爽狠狠 | 紧缚调教一区二区三区视频 | 亚洲成人网在线播放 | 欧洲毛片 | 国产黄色麻豆视频 | 免费看爱爱视频 | 国产精品中文字幕在线播放 | 亚洲精品一区国产精品 | 天天草天天操 | 国产一二区免费视频 | 美日韩精品 | 久久久精品网站 | 日本不卡高字幕在线2019 | 日韩视频在线播放 | 国产视频二区在线观看 | 国产福利资源在线 | 男女羞羞视频在线免费观看 | 在线观看h视频 | 天堂久久天堂综合色 | 国产精品无码专区在线观看 |