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

為什么迭代 List 比通過它索引更快?

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

問題描述

閱讀 ADT 列表的 Java 文檔 它說:

List 接口為列表元素的位置(索引)訪問提供了四種方法.列表(如 Java 數組)是從零開始的.請注意,對于某些實現(例如 LinkedList 類),這些操作的執行時間可能與索引值成正比.因此,如果調用者不知道實現,則迭代列表中的元素通常比通過它索引更可取.

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.

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

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

推薦答案

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

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

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

要訪問item3,你可以清楚地看到,你需要從頭部遍歷每個節點,直到到達item3,因為你不能直接跳轉.

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.

因此,如果我想打印每個元素的值,如果我這樣寫:

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));
}

發生了什么:

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

效率極低,因為每次您編制索引時,它都會從列表的開頭重新開始并遍歷每個項目.這意味著您的復雜性實際上是 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);
}

然后發生的事情是這樣的:

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).

現在,轉到 List 的另一個實現,即 ArrayList,它由一個簡單的數組支持.在那種情況下,上述兩種遍歷都是等價的,因為數組是連續的,所以它允許隨機跳轉到任意位置.

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.

這篇關于為什么迭代 List 比通過它索引更快?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持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)
主站蜘蛛池模板: 成人影院av| 久久av.com| 国产精品成人一区二区三区 | 伊人操| 97高清国语自产拍 | 精品亚洲一区二区三区 | 国产一区久久精品 | 日韩一区二区视频 | 国产国产精品 | 日韩一区二区在线播放 | 成人免费福利 | 夜夜爽99久久国产综合精品女不卡 | 精品欧美一区二区三区久久久 | 国产一区二区三区免费 | 欧美日韩在线一区二区 | 免费观看日韩av | 九九九久久国产免费 | 亚洲国产aⅴ精品 | 天天摸天天干 | 国产亚洲第一页 | 看一级毛片视频 | 日韩中文字幕一区二区三区 | 免费观看一级特黄欧美大片 | 国产成人精品福利 | 日本黄色免费视频 | 欧美jizzhd精品欧美巨大免费 | 女人精96xxx免费网站p | 国产成人精品免高潮在线观看 | 亚洲精品久久久一区二区三区 | 国产亚洲精品久久午夜玫瑰园 | 成人黄色av网址 | 成人精品视频在线观看 | 91小视频在线 | 精品久久久久久国产 | 亚洲精品国产一区 | 天天干天天爱天天爽 | a级在线免费观看 | 国产欧美一区二区三区久久人妖 | 国产精品久久久久久一区二区三区 | 久久新| 三级在线观看 |