Java ArrayList與Vector和LinkedList的使用及源碼分析
ArrayList是List接口實(shí)現(xiàn)類中的其中一個(重點(diǎn)):
- 數(shù)組結(jié)構(gòu)實(shí)現(xiàn),查詢快,增刪慢
- jdk1.2版本,運(yùn)行效率快、線程不安全。
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方法比較的是屬性后,可以進(jìn)行如下刪除 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){ // 強(qiáng)制類型轉(zhuǎn)換 Student s = (Student)obj; if(s.age == this.age && s.name == this.name){ return true; } } return false; } }
刪除和添加可以直接輸入屬性添加了。
運(yùn)行結(jié)果:
ArrayList源碼分析:
1.進(jìn)入ArrayList源碼中,先記住幾個變量。
往下還有個size,表示元素個數(shù)。
重點(diǎn)記住三個:
默認(rèn)容量DEFAULT_CAPACITY = 10、存放元素的數(shù)組:elemtData 和 實(shí)際元素個數(shù)size()
往下翻:
因?yàn)槲覀円玫氖菬o參構(gòu)造方法,有參就跳過,直接看無參的。
也就是說,當(dāng)沒有向集合中添加任何元素時(shí),集合容量為0
當(dāng)我們添加元素時(shí),也就是add()方法,會是什么情況?進(jìn)入到add源碼中查看。
我們再進(jìn)入到這方法中來看:
也就是說,當(dāng)向集合中添加了1個元素時(shí),集合容量就變?yōu)?0。
如果添加了超過10個元素,會進(jìn)行擴(kuò)容,還是和上述方法一樣,一步一步看下來,會發(fā)現(xiàn):
當(dāng)輸入第11個元素時(shí),容量最后會變成15,也就是說,每次擴(kuò)容會是原來的1.5倍。 其實(shí)就是上述第11步的右移一位,相當(dāng)于加上了原來值的二分之一,也就是1.5倍。
總結(jié)一下:
如果集合中沒有元素時(shí),容量為0,如果添加了一個元素,集合會是默認(rèn)容量10,如果超出容量大小,會進(jìn)行擴(kuò)容,每次容量時(shí)原來的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("----------增強(qiáng)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信息,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-01-01Spring boot Rabbitmq消息防丟失實(shí)踐
這篇文章主要介紹了Spring boot Rabbitmq消息防丟失實(shí)踐,文章圍繞主題展開詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-09-09Spring-Security實(shí)現(xiàn)登錄接口流程
Security?是?Spring?家族中的一個安全管理框架,SpringSecurity的原理其實(shí)就是一個過濾器鏈,內(nèi)部包含了提供各種功能的過濾器,這篇文章主要介紹了Spring-Security實(shí)現(xiàn)登錄接口,需要的朋友可以參考下2023-05-05java中PreparedStatement和Statement詳細(xì)講解
這篇文章主要介紹了java中PreparedStatement和Statement詳細(xì)講解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-11-11FP-Growth算法的Java實(shí)現(xiàn)+具體實(shí)現(xiàn)思路+代碼
FP-Growth算法比Apriori算法快很多(但是卻比不上時(shí)間,how time slipped away)。在網(wǎng)上搜索后發(fā)現(xiàn)Java實(shí)現(xiàn)的FP-Growth算法很少,且大多數(shù)不太能理解):太菜。所以就自己實(shí)現(xiàn)了一下。這篇文章重點(diǎn)介紹一下我的Java實(shí)現(xiàn)2021-06-06Springboot優(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-12Java中的遞增i++與++i的實(shí)現(xiàn)原理詳解
這篇文章主要介紹了Java中的i++與++i的實(shí)現(xiàn)原理詳解,在Java中,i++是一種常見的遞增操作符,用于將變量i的值增加1,它是一種簡潔且方便的方式來實(shí)現(xiàn)循環(huán)和計(jì)數(shù)功能,i++可以用于各種情況,本文來看一下其實(shí)現(xiàn)原理,需要的朋友可以參考下2023-10-10SpringBoot解決同名類導(dǎo)致的bean名沖突bean name conflicts問題
這篇文章主要介紹了SpringBoot解決同名類導(dǎo)致的bean名沖突bean name conflicts問題,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2023-06-06