一、集合的概念
1.概述:
在學習集合前,先回憶一下數組的一個特征---數組有固定的長度,定義一個數組:
int[] array = new int[];
而針對數據長度可變的情況,產生了集合,
java集合就是為了應對動態增長數據,在編譯時無法知道具體的數據量而產生的。
集合類又叫容器類。
2.集合和數組的區別
1>都是容器,數組時固定的長度,集合時可變的;
2>數組存放的數據都是基本數據類型(四類八種)
集合存放的數據都是引用數據類型(String、Integer、自定義數據類型)
3>集合中對於基本數據類型會轉換位引用數據類型再存儲。
3.集合包含內容、集合的框架
1>接口:Collection,Map,Set,List等(其中Set和List繼承了Collection)
2>抽象類:AbstractCollection,AbstractList等(實現了部分方法)
3>實現類:ArrayList,LinkedList,HashMap等
4>迭代器:Iterator(集合的訪問迭代,返回集合中的元素的迭代器)
二、List集合
1.概述
List集合是一個有序的、可重復的集合,集合中每一個元素都有對應的順序索引。
List允許加入重復元素是應為可以通過索引來訪問指定位置的元素。
List集合默認按照元素的添加順序增加元素的索引。
2.ArrayList
1>概述
ArrayList是基於數組實現的List類,實現所有可選列表操作,允許所有元素包括null
2>初始化
ArrayList arrayList = new ArrayList(); =>初始容量為10的列表集合
ArrayList<E> arrayList = new ArrayList<E>(); =>數據類型為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() -->以正確的順序(從第一個到最后一個元素)返回一個包含此列表中所有元素的數組。
/**
* @ 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種方法
1>通過List.size()方法作為for循環的條件,同數組遍歷
2>通過迭代器Iterator
Iterator<Integer> it = arrayList.iterator();
while循環,hasNext作為判斷條件,next()獲取集合元素再輸出。
3>增強for循環
4>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循環條件遍歷:");
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循環遍歷集合:");
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指的是鏈表類的數據結構
LinkedList與ArrayList的區別:
a)鏈表中的元素可以任意的增加和刪除,但是查詢效率不如列表
b)鏈表將對象存放在獨立的空間中,而且每個空姐保存了下一個鏈接的索引
2>構造方法
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);
}
}
