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

迭代器與 for

Iterator vs for(迭代器與 for)
本文介紹了迭代器與 for的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我在一次采訪中被問到使用迭代器比 for 循環有什么優勢,或者使用 for 循環比迭代器有什么優勢?

I was asked in an interview what is the advantage of using iterator over for loop or what is the advantage of using for loop over iterator?

任何人都可以回答這個問題嗎?

Can any body please answer this?

推薦答案

首先,有兩種for循環,它們的行為非常不同.一個使用索引:

First of all, there are 2 kinds of for loops, which behave very differently. One uses indices:

for (int i = 0; i < list.size(); i++) {
    Thing t = list.get(i);
    ...
}

這種循環并不總是可能的.例如,List 有索引,但 Set 沒有,因為它們是無序的集合.

This kind of loop isn't always possible. For example, Lists have indices, but Sets don't, because they're unordered collections.

另一個,foreach 循環在幕后使用了一個迭代器:

The other one, the foreach loop uses an Iterator behind the scenes:

for (Thing thing : list) {
    ...
}

這適用于各種 Iterable 集合(或數組)

This works with every kind of Iterable collection (or array)

最后,您可以使用 Iterator,它也適用于任何 Iterable:

And finally, you can use an Iterator, which also works with any Iterable:

for (Iterator<Thing> it = list.iterator(); it.hasNext(); ) {
    Thing t = it.next();
    ...
} 

所以你實際上有 3 個循環要比較.

So you in fact have 3 loops to compare.

您可以用不同的術語來比較它們:性能、可讀性、易錯性、能力.

You can compare them in different terms: performance, readability, error-proneness, capability.

迭代器可以做一些 foreach 循環不能做的事情.例如,如果迭代器支持,您可以在迭代時刪除元素:

An Iterator can do things that a foreach loop can't. For example, you can remove elements while you're iterating, if the iterator supports it:

for (Iterator<Thing> it = list.iterator(); it.hasNext(); ) {
    Thing t = it.next();
    if (shouldBeDeleted(thing) {
        it.remove();
    }
} 

列表還提供了可以雙向迭代的迭代器.foreach 循環只從頭到尾迭代.

Lists also offer iterators that can iterate in both directions. A foreach loop only iterates from the beginning to an end.

但迭代器更危險且可讀性更差.當你只需要一個 foreach 循環時,它是最易讀的解決方案.使用迭代器,您可以執行以下操作,這將是一個錯誤:

But an Iterator is more dangerous and less readable. When a foreach loop is all you need, it's the most readable solution. With an iterator, you could do the following, which would be a bug:

for (Iterator<Thing> it = list.iterator(); it.hasNext(); ) {
    System.out.println(it.next().getFoo());
    System.out.println(it.next().getBar());
} 

foreach 循環不允許發生此類錯誤.

A foreach loop doesn't allow for such a bug to happen.

對于由數組支持的集合,使用索引訪問元素的效率稍高一些.但是如果你改變主意并使用 LinkedList 而不是 ArrayList,性能會突然變得很糟糕,因為每次訪問 list.get(i) 時,鏈表都必須循環所有它的元素直到第 i 個.迭代器(以及因此的 foreach 循環)沒有這個問題.它總是使用最好的方法來遍歷給定集合的元素,因為集合本身有它自己的 Iterator 實現.

Using indices to access elements is slightly more efficient with collections backed by an array. But if you change your mind and use a LinkedList instead of an ArrayList, suddenly the performance will be awful, because each time you access list.get(i), the linked list will have to loop though all its elements until the ith one. An Iterator (and thus the foreach loop) doesn't have this problem. It always uses the best possible way to iterate through elements of the given collection, because the collection itself has its own Iterator implementation.

我的一般經驗法則是:使用 foreach 循環,除非您真的需要迭代器的功能.當我需要訪問循環內的索引時,我只會使用帶有數組索引的 for 循環.

My general rule of thumb is: use the foreach loop, unless you really need capabilities of an Iterator. I would only use for loop with indices with arrays, when I need access to the index inside the loop.

這篇關于迭代器與 for的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 久久不卡 | 中文字幕在线第一页 | 理论片午午伦夜理片影院 | 四虎首页 | 久久精品aaa| 欧美一区二区三区在线观看 | 国产一区二区三区视频在线观看 | 91在线观看| 嫩草视频在线 | 黄色电影在线免费观看 | 成年人国产在线观看 | 日韩黄a| 综合久久综合久久 | 欧美嘿咻 | 亚洲欧美日韩在线不卡 | av在线播放一区二区 | 国产一区二区三区久久久久久久久 | 精品国产一区二区三区久久久蜜月 | 欧美激情在线精品一区二区三区 | 亚洲精品视频在线观看免费 | 亚洲精品乱码久久久久久蜜桃 | 人人九九精| 黄色片视频 | 91网在线观看 | 国产真实精品久久二三区 | 欧美日韩国产一区二区 | 久久国产精品免费 | 免费在线观看h片 | 日本在线网站 | 国产清纯白嫩初高生视频在线观看 | 日韩成人一区 | 国产日韩一区二区 | 亚洲欧美一区二区三区国产精品 | 亚洲毛片一区二区 | 日本免费在线 | 91精品国产综合久久福利软件 | 亚洲网站在线播放 | 日韩高清黄色 | 国产亚洲精品美女久久久久久久久久 | 国产成人精品一区二区 | 天堂一区二区三区 |