Java ArrayList與Vector和LinkedList的使用及源碼分析
ArrayList是List接口實現(xiàn)類中的其中一個(重點):
- 數(shù)組結(jié)構(gòu)實現(xiàn),查詢快,增刪慢
- jdk1.2版本,運行效率快、線程不安全。
ArrayList的使用:和之前使用的類似。
直接看代碼:
主類:
package com.collections.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.ListIterator;
public class Demo02 {
public static void main(String[] args) {
ArrayList arrayList = new ArrayList();
Student s1 = new Student("aaa",18);
Student s2 = new Student("bbb",19);
Student s3 = new Student("ccc",20);
// 添加
arrayList.add(s1);
arrayList.add(s2);
arrayList.add(s3);
System.out.println("元素個數(shù):"+arrayList.size());
System.out.println(arrayList.toString());
// 刪除
// arrayList.remove(s1);
// 重寫equal方法,使equal方法比較的是屬性后,可以進行如下刪除
arrayList.remove(new Student("aaa",18));
System.out.println("刪除后:"+arrayList.size());
System.out.println(arrayList.toString());
// 遍歷
// 使用迭代器
System.out.println("----------使用迭代器--------------");
Iterator it = arrayList.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s);
}
System.out.println("----------列表迭代器正序--------------");
// 列表迭代器
ListIterator listIterator = arrayList.listIterator();
while (listIterator.hasNext()){
Student s = (Student) listIterator.next();
System.out.println(s);
}
System.out.println("----------列表迭代器逆序--------------");
while (listIterator.hasPrevious()){
Student s = (Student) listIterator.previous();
System.out.println(s);
}
}
}Student類:重寫了equal方法,使比較方法變?yōu)閷傩灾g的比較。
package com.collections.test;
import java.util.Objects;
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
@Override
public boolean equals(Object obj) {
// 1.判斷是不是同一類型
if(this==obj){
return true;
}
if(obj==null){
return false;
}
// 3.判斷是否是Student類型
if(obj instanceof Student){
// 強制類型轉(zhuǎn)換
Student s = (Student)obj;
if(s.age == this.age && s.name == this.name){
return true;
}
}
return false;
}
}刪除和添加可以直接輸入屬性添加了。
運行結(jié)果:

ArrayList源碼分析:
1.進入ArrayList源碼中,先記住幾個變量。

往下還有個size,表示元素個數(shù)。
重點記住三個:
默認(rèn)容量DEFAULT_CAPACITY = 10、存放元素的數(shù)組:elemtData 和 實際元素個數(shù)size()
往下翻:
因為我們引用的是無參構(gòu)造方法,有參就跳過,直接看無參的。

也就是說,當(dāng)沒有向集合中添加任何元素時,集合容量為0
當(dāng)我們添加元素時,也就是add()方法,會是什么情況?進入到add源碼中查看。

我們再進入到這方法中來看:



也就是說,當(dāng)向集合中添加了1個元素時,集合容量就變?yōu)?0。
如果添加了超過10個元素,會進行擴容,還是和上述方法一樣,一步一步看下來,會發(fā)現(xiàn):
當(dāng)輸入第11個元素時,容量最后會變成15,也就是說,每次擴容會是原來的1.5倍。 其實就是上述第11步的右移一位,相當(dāng)于加上了原來值的二分之一,也就是1.5倍。
總結(jié)一下:
如果集合中沒有元素時,容量為0,如果添加了一個元素,集合會是默認(rèn)容量10,如果超出容量大小,會進行擴容,每次容量時原來的1.5倍。
源碼查看:ctrl+鼠標(biāo)左鍵。
Vector的使用:
直接看代碼:
package com.collections;
import java.util.Enumeration;
import java.util.Vector;
//Vector集合的使用
//存儲方式:數(shù)組
public class Demo04 {
public static void main(String[] args) {
Vector vector = new Vector();
// 添加
vector.add("蘋果");
vector.add("梨子");
vector.add("西瓜");
System.out.println("元素個數(shù)為:"+vector.size());
// 枚舉器遍歷
Enumeration elements = vector.elements();
while (elements.hasMoreElements()){
String s = (String) elements.nextElement();
System.out.println(s);
}
System.out.println("-----------------------------------");
// 其他方法firstElement 第一個元素 lastElement最后一個元素 elementAt()位置遍歷
System.out.println(vector.firstElement());
System.out.println(vector.lastElement());
System.out.println(vector.elementAt(2));
}
}LinkedList的使用:
package com.collections.test;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.ListIterator;
//LinkList的使用
public class Demo03 {
public static void main(String[] args) {
LinkedList linkedList = new LinkedList();
Student s1 = new Student("aaa",18);
Student s2 = new Student("bbb",19);
Student s3 = new Student("ccc",20);
// 添加
linkedList.add(s1);
linkedList.add(s2);
linkedList.add(s3);
System.out.println("元素個數(shù):"+linkedList.size());
System.out.println(linkedList.toString());
// 刪除
// linkedList.remove(s1);
System.out.println("刪除后:"+linkedList.size());
System.out.println(linkedList.toString());
// 遍歷
// for 循環(huán)
System.out.println("----------for循環(huán)--------------");
for (int i = 0; i <linkedList.size() ; i++) {
System.out.println(linkedList.get(i));
}
System.out.println("----------增強for循環(huán)--------------");
for (Object o:linkedList) {
System.out.println(o);
}
// 使用迭代器
System.out.println("----------使用迭代器--------------");
Iterator it = linkedList.iterator();
while (it.hasNext()){
Student s = (Student) it.next();
System.out.println(s);
}
System.out.println("----------列表迭代器正序--------------");
// 列表迭代器
ListIterator listIterator = linkedList.listIterator();
while (listIterator.hasNext()){
Student s = (Student) listIterator.next();
System.out.println(s);
}
System.out.println("----------列表迭代器逆序--------------");
while (listIterator.hasPrevious()){
Student s = (Student) listIterator.previous();
System.out.println(s);
}
}
}到此這篇關(guān)于Java ArrayList與Vector和LinkedList的使用及源碼分析的文章就介紹到這了,更多相關(guān)Java ArrayList內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Idea中如何查看SpringSecurity各Filter信息
這篇文章主要介紹了Idea中如何查看SpringSecurity各Filter信息,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01
java中PreparedStatement和Statement詳細(xì)講解
這篇文章主要介紹了java中PreparedStatement和Statement詳細(xì)講解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11
FP-Growth算法的Java實現(xiàn)+具體實現(xiàn)思路+代碼
FP-Growth算法比Apriori算法快很多(但是卻比不上時間,how time slipped away)。在網(wǎng)上搜索后發(fā)現(xiàn)Java實現(xiàn)的FP-Growth算法很少,且大多數(shù)不太能理解):太菜。所以就自己實現(xiàn)了一下。這篇文章重點介紹一下我的Java實現(xiàn)2021-06-06
Springboot優(yōu)化內(nèi)置服務(wù)器Tomcat優(yōu)化方式(underTow)
本文詳細(xì)介紹了Spring Boot中Tomcat和Undertow服務(wù)器的配置和優(yōu)化,包括初始線程數(shù)、最大線程數(shù)、最小備用線程數(shù)、最大請求數(shù)等參數(shù)的優(yōu)化建議,以及在高并發(fā)場景下Undertow相對于Tomcat的優(yōu)勢2024-12-12
SpringBoot解決同名類導(dǎo)致的bean名沖突bean name conflicts問題
這篇文章主要介紹了SpringBoot解決同名類導(dǎo)致的bean名沖突bean name conflicts問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06

