Java List集合

我們先看一下jdk1.9對其的描述:

什么是List,也就是一個有序集合(序列)。

1.List接口

List集合代表一個有序集合,集合中每個元素都有其對應的順序索引。List集合允許使用重復元素,可以通過索引來訪問指定位置的集合元素。

List接口繼承于Collection接口,它可以定義一個允許重復有序集合。因為List中的元素是有序的,所以我們可以通過使用索引(元素在List中的位置,類似于數組下標)來訪問List中的元素,這類似于Java的數組。

List接口為Collection直接接口。List所代表的是有序的Collection,即它用某種特定的插入順序來維護元素順序。用戶可以對列表中每個元素的插入位置進行精確地控制,同時可以根據元素的整數索引(在列表中的位置)訪問元素,并搜索列表中的元素。實現List接口的集合主要有:ArrayList、LinkedList、Vector、Stack。

(1)ArrayList

????? ArrayList是一個動態數組,也是我們最常用的集合。它允許任何符合規則的元素插入甚至包括null。每一個ArrayList都有一個初始容量(10),該容量代表了數組的大小。隨著容器中的元素不斷增加,容器的大小也會隨著增加。在每次向容器中增加元素的同時都會進行容量檢查,當快溢出時,就會進行擴容操作。所以如果我們明確所插入元素的多少,最好指定一個初始容量值,避免過多的進行擴容操作而浪費時間、效率

????? size、isEmpty、get、set、iterator 和 listIterator 操作都以固定時間運行。add 操作以分攤的固定時間運行,也就是說,添加 n 個元素需要 O(n) 時間(由于要考慮到擴容,所以這不只是添加元素會帶來分攤固定時間開銷那樣簡單)。

??????ArrayList擅長于隨機訪問。同時ArrayList是非同步的。

(2)LinkedList

????? 同樣實現List接口的LinkedList與ArrayList不同,ArrayList是一個動態數組,而LinkedList是一個雙向鏈表。所以它除了有ArrayList的基本操作方法外還額外提供了get,remove,insert方法在LinkedList的首部或尾部。

????? 由于實現的方式不同,LinkedList不能隨機訪問,它所有的操作都是要按照雙重鏈表的需要執行。在列表中索引的操作將從開頭或結尾遍歷列表(從靠近指定索引的一端)。這樣做的好處就是可以通過較低的代價在List中進行插入和刪除操作。

????? 與ArrayList一樣,LinkedList也是非同步的。如果多個線程同時訪問一個List,則必須自己實現訪問同步。一種解決方法是在創建List時構造一個同步的List:?
List list = Collections.synchronizedList(new LinkedList(...));

(3)Vector

????? 與ArrayList相似,但是Vector是同步的。所以說Vector是線程安全的動態數組。它的操作與ArrayList幾乎一樣。

(4)Stack

???? Stack繼承自Vector,實現一個后進先出的堆棧。Stack提供5個額外的方法使得Vector得以被當作堆棧使用。基本的push和pop 方法,還有peek方法得到棧頂的元素,empty方法測試堆棧是否為空,search方法檢測一個元素在堆棧中的位置。Stack剛創建后是空棧。

以下來自JDK1.9 關于List

Public interface List<E> extends Collection<E>

An ordered collection (also known as a?sequence). The user of this interface has precise control over where in the list each element is inserted. The user can access elements by their integer index (position in the list), and search for elements in the list.
一個有序集合(也稱為序列)。這個接口的用戶可以精確控制每個元素插入的列表的位置。用戶可以通過他們的整數索引(在列表中的位置)訪問元素,并搜索列表中的元素。

Unlike sets, lists typically allow duplicate elements. More formally, lists typically allow pairs of elements?e1?and?e2?such that?e1.equals(e2), and they typically allow multiple null elements if they allow null elements at all. It is not inconceivable that someone might wish to implement a list that prohibits duplicates, by throwing runtime exceptions when the user attempts to insert them, but we expect this usage to be rare.

與集合不同,列表通常允許重復的元素。更正式的說,列表通常允許成對的元素e1和e2,比如e1.equals(e2),如果它們允許零元素,它們通常允許多個空元素。有人可能希望實現一個禁止重復的列表,在用戶試圖插入時拋出運行時異常,這不是不可想象的,但我們希望這種用法非常少見。

The?List?interface places additional stipulations, beyond those specified in the?Collection?interface, on the contracts of the?iterator,?add,?remove,?equals, andhashCode?methods. Declarations for other inherited methods are also included here for convenience.

List接口將額外的規定放置在迭代器的契約、添加、移除、equals和hashcode方法上,超出了集合接口中指定的條款。為了方便起見,這里還包括了其他繼承方法的聲明。

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.

List接口提供了四種方法來定位(索引)對列表元素的訪問。列表(如Java數組)是基于0的。請注意,這些操作可能會與某些實現的索引值成比例(例如,LinkedList類)。因此,如果調用者不知道實現,那么遍歷列表中的元素通常更可取。


The?List?interface provides a special iterator, called a?ListIterator, that allows element insertion and replacement, and bidirectional access in addition to the normal operations that the?Iterator?interface provides. A method is provided to obtain a list iterator that starts at a specified position in the list.

List接口提供了一個特殊的迭代器,稱為ListIterator,除了Iterator接口提供的常規操作之外,它還允許元素插入和替換,以及雙向訪問。提供了一種方法來獲得列表迭代器,它從列表中指定的位置開始。

The?List?interface provides two methods to search for a specified object. From a performance standpoint, these methods should be used with caution. In many implementations they will perform costly linear searches.

List接口提供了兩種搜索指定對象的方法。從性能的角度來看,這些方法應該謹慎使用。在許多實現中,它們將執行代價高昂的線性搜索。



The?List?interface provides two methods to efficiently insert and remove multiple elements at an arbitrary point in the list.

List接口提供了兩種方法,可以有效地在列表中的任意一點插入和刪除多個元素。

Note: While it is permissible for lists to contain themselves as elements, extreme caution is advised: the?equals?and?hashCode?methods are no longer well defined on such a list.

注意:雖然列表可以將自己作為元素來包含,但是要特別注意:在這樣的列表中,equals和hashCode方法不再有很好的定義。



Some list implementations have restrictions on the elements that they may contain. For example, some implementations prohibit null elements, and some have restrictions on the types of their elements. Attempting to add an ineligible element throws an unchecked exception, typically?NullPointerException?or?ClassCastException. Attempting to query the presence of an ineligible element may throw an exception, or it may simply return false; some implementations will exhibit the former behavior and some will exhibit the latter. More generally, attempting an operation on an ineligible element whose completion would not result in the insertion of an ineligible element into the list may throw an exception or it may succeed, at the option of the implementation. Such exceptions are marked as "optional" in the specification for this interface。

一些列表實現對它們可能包含的元素有限制。 例如,有些實現禁止零元素,有些實現對其元素的類型有限制。 試圖添加一個不合格的元素會拋出一個未經檢查的異常,通常是NullPointerException或ClassCastException。 試圖查詢不合格元素的存在可能會拋出異常,或者它可能只是返回false; 一些實現將展示前者的行為,而有些實現將展示后者。 更一般的情況是,嘗試對一個不符合條件的元素進行操作,其完成不會導致將一個不合格的元素插入到列表中,這可能會拋出異常,或者在實現的選項中可能成功。 在該接口的規范中,這些異常被標記為“可選”。




本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/news/274060.shtml
繁體地址,請注明出處:http://hk.pswp.cn/news/274060.shtml
英文地址,請注明出處:http://en.pswp.cn/news/274060.shtml

如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!

相關文章

winform錯誤提示 :窗口類名無效(Window class name is not valid)

winfrom 程序在 xp 操作系統上報錯提示 窗口類名無效(Window class name is not valid) 解決方法 注釋 Program類 里 這句 Application.EnableVisualStyles(); 解決轉載于:https://www.cnblogs.com/z_lb/p/3288850.html

如何在linux下通過ssh運行X圖形軟件

服務器端&#xff1a;編輯/etc/ssh/sshd_config中的以下內容 啟用AllowTcpForwarding 啟用X11Forwarding 將X11DisplayOffset設定為10. 啟用X11UseLocalhost 客戶機端&#xff1a;編輯/etc/ssh/ssh_config中的以下內容 啟用X11Forwarding 連接時ssh -X或者ssh -Y就可以了…

Java Set集合

Set接口什么是Set&#xff0c;就是不包含重復元素的集合。Set是一種不包括重復元素的Collection。它維持它自己的內部排序&#xff0c;所以隨機訪問沒有任何意義。與List一樣&#xff0c;它同樣允許null的存在但是僅有一個。由于Set接口的特殊性&#xff0c;所有傳入Set集合中的…

linux下制作win7安裝U盤

轉自:http://blog.csdn.net/pipisorry/article/details/41369821 http://blog.csdn.net/pipisorry/article/details/41369821 已裝Linux&#xff0c;再用U盤安裝win7(網絡安裝應該也可以)&#xff0c; 先要在linux里面制作一個win7安裝U盤&#xff08;windows下用ultraiso制…

Java Map集合

Map集合&#xff1a;Map接口Map與List、Set接口不同&#xff0c;它是由一系列鍵值對組成的集合&#xff0c;提供了key到Value的映射。同時它也沒有繼承Collection。在Map中它保證了key與value之間的一一對應關系。也就是說一個key對應一個value&#xff0c;所以它不能存在相同的…

gsettings命令使用簡介

1.gsettings創建項 應用程序可以使用gsettings來保存配置信息&#xff0c;可以通過代碼在程序中進行設置、修改gsettings的已有的項&#xff0c;但是不能通過程序代碼創建新的gsettings項&#xff0c;gsettings的項的在一個叫做schema的規范文件中創建&#xff0c;schema文檔其…

Collection 和 Collections區別

Collection 和 Collections區別&#xff08;1&#xff09;java.util.Collection 是一個集合接口&#xff08;集合類的一個頂級接口&#xff09;。它提供了對集合對象進行基本操作的通用接口方法。Collection接口在Java 類庫中有很多具體的實現。Collection接口的意義是為各種具…

Http狀態碼完整說明

在網站建設的實際應用中&#xff0c;容易出現很多小小的失誤&#xff0c;就像mysql當初優化不到位&#xff0c;影響整體網站的瀏覽效果一樣&#xff0c;其實&#xff0c;網站的常規http狀態碼的表現也是一樣&#xff0c; 一些常見的狀態碼為&#xff1a; 200 - 服務器成功返回網…

運用xlib進行事件響應(X11 API)的小例子

轉自&#xff1a;http://blog.csdn.net/linuxheik/article/details/7659090 File: x11_test.cxx #include <X11/Xlib.h> 每一個Xlib 程序都必須包含這個頭文件 #include <stdio.h>1. int main(void) {2. Display *display XopenDisplay(NULL);首先打開與server …

Java 之HashSet、LinkedHashSet、TreeSet比較

4.HashSet、LinkedHashSet、TreeSet比較 Set接口Set不允許包含相同的元素&#xff0c;如果試圖把兩個相同元素加入同一個集合中&#xff0c;add方法返回false。Set判斷兩個對象相同不是使用運算符&#xff0c;而是根據equals方法。也就是說&#xff0c;只要兩個對象用equals方法…

jquery1.9學習筆記 之選擇器(基本元素四)

ID選擇器("#id") 描述&#xff1a; 選擇與給出ID屬性匹配的單元標簽。 對于ID選擇器&#xff0c;jquery使用JS的函數document.getElementById()&#xff0c;當一個標簽附加到ID選擇器上時&#xff0c;也是非常有效的。如h2#pageTitle&#xff0c;jquery會在識別元素標…

Java(ArrayList和LinkedList)、(HashTable與HashMap)、(HashMap、Hashtable、LinkedHashMap和TreeMap比較)

1.ArrayList和LinkedList &#xff08;1&#xff09;ArrayList是實現了基于動態數組的數據結構&#xff0c;LinkedList基于鏈表的數據結構。 &#xff08;2&#xff09;對于隨機訪問get和set&#xff0c;ArrayList絕對優于LinkedList&#xff0c;因為LinkedList要移動指針。 &a…

oracle 事務測試

此文章是根據官方改變 模擬帳戶轉賬流程1.JOHN帳戶扣除-DAVID帳戶增加-記錄日志&#xff0d;事務提交三個操作必須全部完成此事務才完成&#xff0c;否則失敗創建帳戶余額表自增字段自增序列&#xff1b;createsequencesaving_seqincrementby1startwith1maxvalue99999999999999…

apt-get 獲取源碼的方法

apt-get source gconf-editor –allow-unauthenticated 注&#xff1a;gconf-editor是一個包名&#xff0c;根據自己的需求相應更改即可

Java 集合之自動打包和解包以及泛型

自動打包與解包&#xff1a;泛型&#xff1a;上栗子&#xff1a; TestMap1.java: package com.zhj.www; import java.util.*;public class TestMap {public static void main(String[] args) {Map m1 new HashMap();Map m2 new TreeMap();//m1.put("one", new Inte…

select * from dim.dim_area_no@to_dw

應該是建的有database linksdim是用戶名&#xff0c;dim_area_no是表名&#xff0c;to_dw 是建的database links的名&#xff0c;dim_area_no表屬于dim用戶創建database links的作用是連接其他數據庫的表select * from dim.dim_area_noto_dw 這個語句的作用是查詢屬于dim用戶的…

ios 內存管理 心得

- alloc, copy, retain會把引用計數1 - release會把引用計數-1 - 局部變量如果初始化時不是autorelease的&#xff0c;要及時調用release釋放&#xff0c;并且賦值為nil否則引用仍然存在導致下次無法用nil做是否有值的判斷 - 實例變量要在每次賦值時要先釋放當前引用的對象再賦…

error while loading shared libraries: xxx.so.x 錯誤的原因和解決辦法

一般我們在Linux下執行某些外部程序的時候可能會提示找不到共享庫的錯誤, 比如: tmux: error while loading shared libraries: libevent-1.4.so.2: cannot open shared object file: No such file or directory 原因一般有兩個, 一個是操作系統里確實沒有包含該共享庫(lib*.…

泗洪高薪行業

泗洪高薪行業轉載于:https://www.cnblogs.com/soundcode/p/3302297.html

libghttp 編譯及封裝使用實例

想用C語言寫個采集程序&#xff0c;涉及到http相關的東西&#xff0c;找了找&#xff0c;有現成的libghttp庫。 libghttp庫的官方網址google一下第一條結果一般就是的&#xff1a;http://lfs.linuxsir.org/htdocs/blfscvs/gnome/libghttp.html 將源碼包下載下來&#xff0c;進…