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

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

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

問題描述

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

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

在回答中,用戶這樣說:使用 ArrayLists 的迭代器的一個重要用例是當您想要在迭代時刪除元素".

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 方法來實現.我的問題是為什么我們需要 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.

推薦答案

正如您所說,當您想要在迭代數組內容時刪除內容時使用迭代器.如果您不使用迭代器,而只是有一個 for 循環并在其中使用 remove 方法,您將得到異常,因為在您迭代時數組的內容會發生變化.例如:你可能認為在 for 循環開始時數組大小是 10,但一旦你刪除東西就不會這樣了.所以當你到達最后一個循環時,可能會有 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.

這篇關于為什么我們需要在 Java 中的 ArrayList 上使用迭代器?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 国内精品一区二区三区 | 亚洲国产成人在线视频 | 午夜日韩 | 九九热精品在线 | 综合久久网 | 欧美日韩不卡合集视频 | 91精品麻豆日日躁夜夜躁 | 做a视频 | 亚洲第一成人av | 成人午夜精品一区二区三区 | 精品成人一区二区 | 国产一区二区黑人欧美xxxx | 欧美综合在线视频 | 91porn在线| 99riav3国产精品视频 | 亚洲高清视频在线观看 | 在线观看成年人视频 | 求个av网址 | 羞羞免费网站 | 日本福利视频免费观看 | 久久久久久久一区二区三区 | 午夜性色a√在线视频观看9 | 欧美视频 | 成人免费共享视频 | 毛片一级网站 | 精品久久久精品 | 日本手机在线 | 欧美成人二区 | www.亚洲一区| 欧美在线a | 久久综合久久久 | 黄色片视频网站 | 中文字幕在线观看成人 | 午夜伦理影院 | 日韩在线国产 | 拍真实国产伦偷精品 | 天天操天天舔 | 中文字幕一区二区三区在线视频 | 欧美日韩在线精品 | 欧美三区在线观看 | www亚洲精品|