淺析Java集合及LIst接口
一、集合的概念
1.概述:
在學習集合前,先回憶一下數(shù)組的一個特征---數(shù)組有固定的長度,定義一個數(shù)組:int[] array = new int[];而針對數(shù)據(jù)長度可變的情況,產生了集合,java集合就是為了應對動態(tài)增長數(shù)據(jù),在編譯時無法知道具體的數(shù)據(jù)量而產生的。
集合類又叫容器類。
2.集合和數(shù)組的區(qū)別
- 都是容器,數(shù)組時固定的長度,集合時可變的;
- 數(shù)組存放的數(shù)據(jù)都是基本數(shù)據(jù)類型(四類八種)集合存放的數(shù)據(jù)都是引用數(shù)據(jù)類型(String、Integer、自定義數(shù)據(jù)類型)
- 集合中對于基本數(shù)據(jù)類型會轉換位引用數(shù)據(jù)類型再存儲。
3.集合包含內容、集合的框架
- 接口:Collection,Map,Set,List等(其中Set和List繼承了Collection)
- 抽象類:AbstractCollection,AbstractList等(實現(xiàn)了部分方法)
- 實現(xiàn)類:ArrayList,LinkedList,HashMap等
- 迭代器:Iterator(集合的訪問迭代,返回集合中的元素的迭代器)
二、List集合
1.概述
List集合是一個有序的、可重復的集合,集合中每一個元素都有對應的順序索引。
List允許加入重復元素是應為可以通過索引來訪問指定位置的元素。
List集合默認按照元素的添加順序增加元素的索引。
2.ArrayList
1>概述
ArrayList是基于數(shù)組實現(xiàn)的List類,實現(xiàn)所有可選列表操作,允許所有元素包括null
2>初始化
ArrayList arrayList = new ArrayList(); =>初始容量為10的列表集合
ArrayList<E> arrayList = new ArrayList<E>(); =>數(shù)據(jù)類型為E,初始容量為10
3>主要方法
boolean add(E e) -->將指定的元素追加到此列表的末尾。
void add(int index, E element) -->在此列表中的指定位置插入指定的元素。
boolean addAll(Collection<? extends E> c) -->按指定集合的Iterator返回的順序將指定集合中的所有元素追加到此列表的末尾。
boolean addAll(int index, Collection<? extends E> c) -->將指定集合中的所有元素插入到此列表中,從指定的位置開始。
boolean contains(Object o) -->如果此列表包含指定的元素,則返回 true 。
E get(int index) -->返回此列表中指定位置的元素。
E remove(int index) -->刪除該列表中指定位置的元素。
E set(int index, E element) -->用指定的元素替換此列表中指定位置的元素。
Object[] toArray() -->以正確的順序(從第一個到最后一個元素)返回一個包含此列表中所有元素的數(shù)組。
/**
* @ author: PrincessHug
* @ date: 2019/2/10, 0:18
* @ Blog: https://www.cnblogs.com/HelloBigTable/
*/
public class ArrayListDemo01 {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
arr.add("123");
System.out.println(arr);
ArrayList<Person> person = new ArrayList<Person>();
Person p1 = new Person("Wyh",18);
Person p2 = new Person("Hunter", 40);
person.add(p1);
person.add(p2);
for (int i=0;i<person.size();i++) {
System.out.println(person.get(i));
}
System.out.println(person.contains(p2));
person.remove(1);
person.set(0,p2);
Person[] persons = new Person[2];
person.toArray(persons);
System.out.println(persons[0]);
System.out.println(persons[1]);
}
}
public class Person {
private String name;
private int age;
public Person(){}
public Person(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 "["+this.name+","+this.age+"]";
}
}
3.List集合遍歷的4種方法
- 通過List.size()方法作為for循環(huán)的條件,同數(shù)組遍歷
- 通過迭代器Iterator Iterator<Integer> it = arrayList.iterator(); while循環(huán),hasNext作為判斷條件,next()獲取集合元素再輸出。
- 增強for循環(huán)
- jdk1.8新特性foreach
/**
* @ author: PrincessHug
* @ date: 2019/2/12, 2:43
* @ Blog: https://www.cnblogs.com/HelloBigTable/
*/
public class PrintArrayList {
public static void main(String[] args) {
ArrayList<Student> students = new ArrayList<>();
Student s1 = new Student("001", "Wyh", '男');
Student s2 = new Student("002", "Fyh", '男');
Student s3 = new Student("003", "Zxy", '男');
students.add(s1);
students.add(s2);
students.add(s3);
System.out.println("通過size()方法作為for循環(huán)條件遍歷:");
for (int i=0;i<students.size();i++){
System.out.println(students.get(i));
}
System.out.println("通過迭代器遍歷集合:");
Iterator<Student> iterator = students.iterator();
while (iterator.hasNext()){
System.out.print(iterator.next() + "\t");
}
System.out.println("通過增強for循環(huán)遍歷集合:");
for (Student stu:students){
System.out.println(stu);
}
System.out.println("通過jdk1.8新特性forEach遍歷集合:");
students.forEach(student -> System.out.println(student));
}
}
4.LinkedList
1.概述:LinkedList指的是鏈表類的數(shù)據(jù)結構
2.LinkedList與ArrayList的區(qū)別:
a)鏈表中的元素可以任意的增加和刪除,但是查詢效率不如列表
b)鏈表將對象存放在獨立的空間中,而且每個空姐保存了下一個鏈接的索引
構造方法 LinkedList<E> linkedList = new LinkedList<E>();
3.主要方法
void addFirst(E e) -->在該列表開頭插入指定的元素。
void addLast(E e) -->將指定的元素追加到此列表的末尾。
E peekFirst() -->檢索但不刪除此列表的第一個元素,如果此列表為空,則返回 null 。
E peekLast() -->檢索但不刪除此列表的最后一個元素,如果此列表為空,則返回 null 。
E pollFirst() -->檢索并刪除此列表的第一個元素,如果此列表為空,則返回 null 。
E pop() -->從此列表表示的堆棧中彈出第一個元素。相似于removeFirst()
void push(E e) -->將元素推送到由此列表表示的堆棧上。相似于addFirst()
/**
* @ author: PrincessHug
* @ date: 2019/2/10, 2:12
* @ Blog: https://www.cnblogs.com/HelloBigTable/
*/
public class LinkedListDemo {
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
linkedList.addFirst("is");
linkedList.addFirst("Wyh");
linkedList.addLast("cool");
System.out.println(linkedList);
System.out.println(linkedList.peekFirst());
System.out.println(linkedList.pollFirst());
System.out.println(linkedList);
System.out.println(linkedList.pop());
System.out.println(linkedList);
linkedList.push("Wyh is");
System.out.println(linkedList);
}
}
以上所述是小編給大家介紹的Java集合及LIst接口詳解整合,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網站的支持!
相關文章
SpringBoot熔斷機制之CircuitBreaker詳解
這篇文章主要介紹了SpringBoot熔斷機制之CircuitBreaker詳解,SpringBoot的熔斷機制在微服務架構中扮演著重要角色,其中CircuitBreaker是其核心機制之一,用于防止服務的異常狀態(tài)影響到整個系統(tǒng)的運作,需要的朋友可以參考下2023-10-10
IDEA-SpringBoot項目Debug啟動不了(卡住不動)的原因分析
這篇文章主要介紹了IDEA-SpringBoot項目Debug啟動不了(卡住不動)的原因分析,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2023-11-11
SpringBoot 整合Tess4J庫實現(xiàn)圖片文字識別案例詳解
Tess4J是一個基于Tesseract OCR引擎的Java接口,可以用來識別圖像中的文本,說白了,就是封裝了它的API,讓Java可以直接調用,今天給大家分享一個SpringBoot整合Tess4j庫實現(xiàn)圖片文字識別的小案例2023-10-10
淺談Java異常的Exception e中的egetMessage()和toString()方法的區(qū)別
下面小編就為大家?guī)硪黄獪\談Java異常的Exception e中的egetMessage()和toString()方法的區(qū)別。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-07-07
SpringBoot用配置影響B(tài)ean加載@ConditionalOnProperty
這篇文章主要為大家介紹了SpringBoot用配置影響B(tài)ean加載@ConditionalOnProperty示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04

