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

為什么迭代 List 比通過(guò)它索引更快?

Why would iterating over a List be faster than indexing through it?(為什么迭代 List 比通過(guò)它索引更快?)
本文介紹了為什么迭代 List 比通過(guò)它索引更快?的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

閱讀 ADT 列表的 Java 文檔 它說(shuō):

List 接口為列表元素的位置(索引)訪問(wèn)提供了四種方法.列表(如 Java 數(shù)組)是從零開(kāi)始的.請(qǐng)注意,對(duì)于某些實(shí)現(xiàn)(例如 LinkedList 類(lèi)),這些操作的執(zhí)行時(shí)間可能與索引值成正比.因此,如果調(diào)用者不知道實(shí)現(xiàn),則迭代列表中的元素通常比通過(guò)它索引更可取.

The List interface provides four methods for positional (indexed) access to list elements. Lists (like Java arrays) are zero based. Note that these operations may execute in time proportional to the index value for some implementations (the LinkedList class, for example). Thus, iterating over the elements in a list is typically preferable to indexing through it if the caller does not know the implementation.

這到底是什么意思?我不明白得出的結(jié)論.

What exactly does this mean? I don't understand the conclusion which is drawn.

推薦答案

在鏈表中,每個(gè)元素都有一個(gè)指向下一個(gè)元素的指針:

In a linked list, each element has a pointer to the next element:

head -> item1 -> item2 -> item3 -> etc.

要訪問(wèn)item3,你可以清楚地看到,你需要從頭部遍歷每個(gè)節(jié)點(diǎn),直到到達(dá)item3,因?yàn)槟悴荒苤苯犹D(zhuǎn).

To access item3, you can see clearly that you need to walk from the head through every node until you reach item3, since you cannot jump directly.

因此,如果我想打印每個(gè)元素的值,如果我這樣寫(xiě):

Thus, if I wanted to print the value of each element, if I write this:

for(int i = 0; i < 4; i++) {
    System.out.println(list.get(i));
}

發(fā)生了什么:

head -> print head
head -> item1 -> print item1
head -> item1 -> item2 -> print item2
head -> item1 -> item2 -> item3 print item3

效率極低,因?yàn)槊看文幹扑饕龝r(shí),它都會(huì)從列表的開(kāi)頭重新開(kāi)始并遍歷每個(gè)項(xiàng)目.這意味著您的復(fù)雜性實(shí)際上是 O(N^2) 只是為了遍歷列表!

This is horribly inefficient because every time you are indexing it restarts from the beginning of the list and goes through every item. This means that your complexity is effectively O(N^2) just to traverse the list!

如果我這樣做:

for(String s: list) {
    System.out.println(s);
}

然后發(fā)生的事情是這樣的:

then what happens is this:

head -> print head -> item1 -> print item1 -> item2 -> print item2 etc.

全部在一次遍歷中,即O(N).

all in a single traversal, which is O(N).

現(xiàn)在,轉(zhuǎn)到 List 的另一個(gè)實(shí)現(xiàn),即 ArrayList,它由一個(gè)簡(jiǎn)單的數(shù)組支持.在那種情況下,上述兩種遍歷都是等價(jià)的,因?yàn)閿?shù)組是連續(xù)的,所以它允許隨機(jī)跳轉(zhuǎn)到任意位置.

Now, going to the other implementation of List which is ArrayList, that one is backed by a simple array. In that case both of the above traversals are equivalent, since an array is contiguous so it allows random jumps to arbitrary positions.

這篇關(guān)于為什么迭代 List 比通過(guò)它索引更快?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來(lái)源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問(wèn)題,如果有圖片或者內(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)用失敗來(lái)自服務(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 檢測(cè)不正確的密鑰)
AES-256-CBC in Java(Java 中的 AES-256-CBC)
主站蜘蛛池模板: 国产伦理一区二区 | 精品第一页| www.com国产| 国产午夜精品久久 | 国产网站在线 | 免费av一区二区三区 | 这里只有精品视频在线观看 | 成人免费在线播放 | 久久怡红院 | 欧美日韩在线免费观看 | 国产精品日韩在线 | 99视频 | 黄色综合网| 亚洲视频精品 | 高清国产mv在线观看 | 欧美性猛交xxxx乱大交退制版 | 日韩av网站在线观看 | 国产亚洲视频在线观看 | 在线观看国产小视频 | 亚州精品视频 | 日韩免费看 | 天堂中文av | 老司机精品福利视频 | 91午夜精品亚洲一区二区三区 | 欧美午夜视频 | 欧美日韩激情视频 | 亚洲a在线观看 | 亚洲综合视频在线观看 | 黄色精品网站 | 黄色一级片免费看 | 四虎www| 黄网站视频 | 五月婷综合 | 午夜在线影院 | 国产黄色片视频 | 黄色一级在线观看 | 久久久久国产精品夜夜夜夜夜 | 一区二区不卡 | 青青草免费在线观看 | 丁香六月激情 | 欧美成人综合 |