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

為什么我們需要在 Java 中的 ArrayList 上使用迭代器

Why do we need to use iterator on ArrayList in Java?(為什么我們需要在 Java 中的 ArrayList 上使用迭代器?)
本文介紹了為什么我們需要在 Java 中的 ArrayList 上使用迭代器?的處理方法,對(duì)大家解決問題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

我正在閱讀問題中提到的答案"我們是否需要在 ArrayList 上使用迭代器?".

I was reading the answer mentioned to the question "Do we ever need to use Iterators on ArrayList?".

在回答中,用戶這樣說:使用 ArrayLists 的迭代器的一個(gè)重要用例是當(dāng)您想要在迭代時(shí)刪除元素".

In the answer, the user stated something like this: "A big use case of iterators with ArrayLists is when you want to remove elements while iterating".

這甚至可以在 Java 中使用 ArrayList 的 remove 方法來實(shí)現(xiàn).我的問題是為什么我們需要 ArrayList 中的迭代器?

This could be achieved even using remove method of ArrayList in Java. My question is why we need iterator in ArrayList?

考慮代碼:

import java.util.*;
public class ocajp66 {
    public static void main(String[] args) {
        ArrayList a = new ArrayList();
        for (int i = 0; i < 10; i++) {
            a.add(i);
        }
        System.out.printf("BEFORE ITERATOR
");
        for (int i = 0; i < a.size(); i++) {
            System.out.printf("I:%d
", a.get(i));
        }
        System.out.printf("AFTER ITERATOR
");
        Iterator i = a.iterator();
        while (i.hasNext()) {
            System.out.printf("I:%d
", i.next());
        }
    }
}

誰能解釋一下迭代器的意義?如果你能用代碼解釋我,那就太好了.

Can anybody explain the significance of the iterator? It would be wonderful if you could explain me with code.

推薦答案

正如您所說,當(dāng)您想要在迭代數(shù)組內(nèi)容時(shí)刪除內(nèi)容時(shí)使用迭代器.如果您不使用迭代器,而只是有一個(gè) for 循環(huán)并在其中使用 remove 方法,您將得到異常,因?yàn)樵谀鷷r(shí)數(shù)組的內(nèi)容會(huì)發(fā)生變化.例如:你可能認(rèn)為在 for 循環(huán)開始時(shí)數(shù)組大小是 10,但一旦你刪除東西就不會(huì)這樣了.所以當(dāng)你到達(dá)最后一個(gè)循環(huán)時(shí),可能會(huì)有 IndexOutofBoundsException 等.

As you have stated iterator is used when you want to remove stuff whilst you iterate over the array contents. If you don't use an iterator but simply have a for loop and inside it use the remove method you will get exceptions because the contents of the array changes while you iterate through. e.g: you might think array size is 10 at the start of the for loop but it wont be the case once you remove stuff.. so when u reach the last loops probably there will be IndexOutofBoundsException etc.

這篇關(guān)于為什么我們需要在 Java 中的 ArrayList 上使用迭代器?的文章就介紹到這了,希望我們推薦的答案對(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 檢測不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 91精品国产综合久久久久久 | 午夜精品久久久久久 | 成人乱人乱一区二区三区软件 | 99精品久久| 三级视频久久 | 国产欧美一区二区三区日本久久久 | 欧美黄a | 久久高清国产视频 | 99re99| 91在线视频一区 | 中文字幕在线看 | 中文字幕国 | 欧美成人精品在线观看 | 免费h在线 | 极品国产视频 | 国产激情视频在线免费观看 | 免费看的av| 成人性视频在线 | 亚洲狠狠丁香婷婷综合久久久 | 超碰精品在线观看 | 91久久综合亚洲鲁鲁五月天 | 国内精品久久精品 | 欧美mv日韩mv国产网站91进入 | 天天操天天拍 | 一区二区三区欧美大片 | 欧美综合久久久 | 欧美日韩成人一区二区 | 国产精品国产精品国产专区不卡 | 久久久久久综合 | 中文字幕日韩在线 | 亚洲精品一二三区 | 日本免费视频 | 亚洲精品9999 | 欧美vide | 久久69精品久久久久久国产越南 | 99riav国产一区二区三区 | 久久中文字幕av | 国产伦精品一区二区三区高清 | 日韩福利 | 国产亚洲精品精品国产亚洲综合 | 国产美女h视频 |