List是有序、可重復(fù)的容器。
有序:
List中每個元素都有索引標記。可以根據(jù)元素的索引標記(在List中的位置)訪問 元素,從而精確控制這些元素。
可重復(fù):
List允許加入重復(fù)的元素。更確切地講,List通常允許滿足 e1.equals(e2) 的元素重復(fù)加入容器。
List接口常用的實現(xiàn)類有3個:ArrayList、LinkedList和Vector。
1.ArrayList:
ArrayList底層是用數(shù)組實現(xiàn)的存儲。 特點:查詢效率高,增刪效率低,線程不安全。我們一般使用它。
ArrayList底層使用Object數(shù)組來存儲元素數(shù)據(jù)。所有的方法,都圍繞這個核心的Object數(shù)組來開展。
2.LinkedList:
LinkedList底層用雙向鏈表實現(xiàn)的存儲。特點:查詢效率低,增刪效率高,線程不安全。
雙向鏈表也叫雙鏈表,是鏈表的一種,它的每個數(shù)據(jù)節(jié)點中都有兩個指針,分別指向前一個節(jié)點和后一個節(jié)點。 所以,從雙向鏈表中的任意一個節(jié)點開始,都可以很方便地找到所有節(jié)點。
3.Vector:
Vector底層是用數(shù)組實現(xiàn)的List,相關(guān)的方法都加了同步檢查,因此“線程安全,效率低”。 比如,copyInto方法就增加了synchronized同步標記。
使用原則:ArrayList、LinkedList、Vector
- 需要線程安全時,用Vector。
- 不存在線程安全問題時,并且查找較多用ArrayList(一般使用它)。
- 不存在線程安全問題時,增加或刪除元素較多用LinkedList。
測試案例:文章來源:http://www.zghlxwxcb.cn/news/detail-682304.html
import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.Vector;
public class day17 {
public static void main(String[] args) {
List<String> arrayList = new ArrayList<>();
List<String> linkedList = new LinkedList<>();
List<String> vector = new Vector<>();
List<Integer> arrayList2 = new ArrayList<>();
List<Integer> linkedList2 = new LinkedList<>();
List<Integer> vector2 = new Vector<>();
List<String> arrayList3 = new ArrayList<>();
List<String> linkedList3 = new LinkedList<>();
List<String> vector3 = new Vector<>();
List<String> list1 = Arrays.asList("黃","河","之","水","天","上","來","奔","流","到","海","不","復(fù)","回",
"黃","河","之","水","天","上","來","奔","流","到","海","不","復(fù)","回");
List<Integer> list2 = Arrays.asList(2 , 1 , 4 , 3 , 6 , 5 ,7 , 14, 13 ,12 , 11 ,10 , 9 , 8,
2 , 1 , 4 , 3 , 6 , 5 ,7 , 8 , 9 ,10 , 11 ,12 , 13 , 14);
List<String> list3 = Arrays.asList("2" , "1" , "4" , "3" , "6" , "5" ,"7" , "14", "13" ,"12" , "11" ,"10" , "9" , "8",
"2" , "1" , "4" , "3" , "6" , "5" ,"7" , "8" , "9" ,"10" , "11" ,"12" , "13" , "14");
arrayList.addAll(list1);
System.out.println("arrayList:"+arrayList);
arrayList2.addAll(list2);
System.out.println("arrayList2:"+arrayList2);
arrayList3.addAll(list3);
System.out.println("arrayList3:"+arrayList3);
System.out.println("-------------------------------------------");
linkedList.addAll(list1);
System.out.println("linkedList:"+linkedList);
linkedList2.addAll(list2);
System.out.println("linkedList2:"+linkedList2);
linkedList3.addAll(list3);
System.out.println("linkedList3:"+linkedList3);
System.out.println("-------------------------------------------");
vector.addAll(list1);
System.out.println("vector:"+vector);
vector2.addAll(list2);
System.out.println("vector2:"+vector2);
vector3.addAll(list3);
System.out.println("vector3:"+vector3);
System.out.println("-------------------------------------------");
}
}
測試輸出:文章來源地址http://www.zghlxwxcb.cn/news/detail-682304.html
arrayList:[黃, 河, 之, 水, 天, 上, 來, 奔, 流, 到, 海, 不, 復(fù), 回, 黃, 河, 之, 水, 天, 上, 來, 奔, 流, 到, 海, 不, 復(fù), 回]
arrayList2:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
arrayList3:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
-------------------------------------------
linkedList:[黃, 河, 之, 水, 天, 上, 來, 奔, 流, 到, 海, 不, 復(fù), 回, 黃, 河, 之, 水, 天, 上, 來, 奔, 流, 到, 海, 不, 復(fù), 回]
linkedList2:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
linkedList3:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
-------------------------------------------
vector:[黃, 河, 之, 水, 天, 上, 來, 奔, 流, 到, 海, 不, 復(fù), 回, 黃, 河, 之, 水, 天, 上, 來, 奔, 流, 到, 海, 不, 復(fù), 回]
vector2:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
vector3:[2, 1, 4, 3, 6, 5, 7, 14, 13, 12, 11, 10, 9, 8, 2, 1, 4, 3, 6, 5, 7, 8, 9, 10, 11, 12, 13, 14]
-------------------------------------------
到了這里,關(guān)于【List】List集合有序測試案例:ArrayList,LinkedList,Vector(123)的文章就介紹完了。如果您還想了解更多內(nèi)容,請在右上角搜索TOY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!