Collection
集合體系結(jié)構(gòu).png
Collection 集合概述
- 是單例集合的頂層接口,它表示一組對象,這些對象也稱為Collection的元素
- JDK 不提供此接口的任何直接實現(xiàn),它提供更具體的子接口(如Set和List) 實現(xiàn)
創(chuàng)建Collection 集合的對象 - 多態(tài)的方式
- 具體的實現(xiàn)類ArrayList
public static void main(String[] args) {
// 創(chuàng)建Collection集合的對象
Collection<String> c = new ArrayList<>();
// 添加元素
c.add("hello");
c.add("hello");
c.add("hello");
System.out.println(c);
}
Collection 集合常用方法
Collection集合常用方法.png
Collection 集合的遍歷
Iterator: 迭代器,集合的專用遍歷方式
- Iterator<E>iterator(): 返回此集合中元素的迭代器,通過集合的Iterator()方法得到
- 迭代器是通過集合的Iterator()方法得到的,所以我們說它是依賴于集合而存在的
Iterator中的常用方法 -
E next()
: 返回迭代中的下一個元素 - boolean hasNext()`: 如果迭代具有更多元素,則返回 true
public static void main(String[] args) {
// 創(chuàng)建Collection集合的對象
Collection<String> c = new ArrayList<>();
// 添加元素
c.add("hello");
c.add("world");
c.add("java");
Iterator<String> it = c.iterator();
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next());
// System.out.println(it.next()); // NoSuchElementException 表示被請求的元素不存在
while(it.hasNext()){
System.out.println(it.next());
}
集合的使用步驟
集合的使用步驟.png
案例: Collection集合存儲學(xué)生對象并遍歷
public static void main(String[] args) {
// 創(chuàng)建Collection集合的對象
Collection<Student> c = new ArrayList<Student>();
// 創(chuàng)建學(xué)生對象
Student s1 = new Student("孫悟空",30);
Student s2 = new Student("豬八戒",31);
Student s3 = new Student("沙悟凈",32);
// 把學(xué)生添加到集合
c.add(s1);
c.add(s2);
c.add(s3);
// 遍歷集合(迭代器方式)
Iterator<Student> it = c.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
}
List集合
List集合概述
- 有序集合(也稱為序列),用戶可以精確控制列表中每個元素的插入位置.用戶可以通過整數(shù)索引訪問元素,并搜索列表中的元素
- 與Set 集合不同,列表通常允許重復(fù)的元素
List集合特點 - 有序: 存儲和取出的元素順序一致
- 可重復(fù): 存儲的元素可以重復(fù)
public static void main(String[] args) {
// 創(chuàng)建集合的對象
List<String> list = new ArrayList<String>();
// 添加元素
list.add("hello");
list.add("world");
list.add("world");
list.add("java");
// 輸出集合對象
System.out.println(list);
System.out.println("--------");
// 采用迭代器方法遍歷
Iterator<String> it = list.iterator();
while (it.hasNext()) {
String s = it.next();
System.out.println(s);
}
}
List集合特有方法
List集合特有方法.png
案例: List集合存儲學(xué)生對象并遍歷
public static void main(String[] args) {
// 創(chuàng)建Collection集合的對象
List<Student> list = new ArrayList<Student>();
// 創(chuàng)建學(xué)生對象
Student s1 = new Student("孫悟空",30);
Student s2 = new Student("豬八戒",31);
Student s3 = new Student("沙悟凈",32);
// 把學(xué)生添加到集合
list.add(s1);
list.add(s2);
list.add(s3);
// 遍歷集合(迭代器方式)
Iterator<Student> it = list.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
System.out.println("--------");
// for循環(huán)遍歷
for (int i=0; i<list.size(); i++){
Student s = list.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
}
并發(fā)修改異常
并發(fā)修改異常
- ConcurrentModificationException
產(chǎn)生原因 - 迭代器遍歷過程中,通過集合對象修改了集合中元素的長度,造成了迭代器獲取元素中判斷預(yù)期修改值和實際修改值不一致
解決方案 - 用 for 循環(huán)遍歷,然后用集合對象做對應(yīng)操作即可
列表迭代器
ListIterator: 列表迭代器
- 通過List集合的listIterator()方法得到,所以說它是List集合特有的迭代器
- 用于允許程序員沿任一方向遍歷列表的列表迭代器,在迭代期間修改列表,并獲取列表中迭代器的當(dāng)前位置
ListIterator中的常用方法
- E next(): 返回迭代中的下一個元素
- boolean hasNext(): 如果迭代具有更多元素,則返回true
- boolean hasprevious(): 如果此列表迭代器在相反方向遍歷列表時具有更多元素,則返回true
- void add(E e): 將指定的元素插入列表
public static void main(String[] args) {
// 創(chuàng)建集合的對象
List<String> list = new ArrayList<String>();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
// 輸出集合對象
System.out.println(list);
System.out.println("--------");
// 采用迭代器方法遍歷
/* ListIterator<String> lit = list.listIterator();
while (lit.hasNext()) {
String s = lit.next();
System.out.println(s);
}
System.out.println("------------");
while (lit.hasPrevious()){
String s = lit.previous();
System.out.println(s);
}*/
ListIterator<String> lit = list.listIterator();
while (lit.hasNext()){
String s = lit.next();
if (s.equals("world")){
lit.add("javaee");
}
}
System.out.println(list);
}
增強for循環(huán)
增強for: 簡化數(shù)組和Collection 集合的遍歷
- 實現(xiàn)Iterable接口的類允許其對象成為增強型for語句的目標(biāo)
- 它是JDK5之后出現(xiàn)的,其內(nèi)部原理是一個Iterator迭代器
增強for的格式 - 格式:
for(元素數(shù)據(jù)類型 變量名 : 數(shù)組或Collection集合) {
//在此處使用變量即可,該變量就是元素
} - 范例:
int[] arr = {1,2,3,4,5};
for(int i : arr) {
System.out.println(i);
}
public static void main(String[] args) {
// 創(chuàng)建集合的對象
List<String> list = new ArrayList<String>();
// 添加元素
list.add("hello");
list.add("world");
list.add("java");
for (String s : list) {
System.out.println(s);
}
System.out.println("------------");
int[] arr = {1,2,3,4,5};
for(int i : arr) {
System.out.println(i);
}
System.out.println("------------");
String[] strArray = {"hello","world","java"};
for (String s1 : strArray){
System.out.println(s1);
}
System.out.println("------------");
/* // 內(nèi)部原理是一個Iterator迭代器
for (String s : list){
if (s.equals("world")){
list.add("javaee"); // ConcurrentModificationException,所以是一個Iterator迭代器
}
}*/
}
案例: List集合存儲學(xué)生對象用三種方式遍歷
public static void main(String[] args) {
// 創(chuàng)建集合的對象
List<Student> list = new ArrayList<Student>();
// 創(chuàng)建學(xué)生對象
Student s1 = new Student("孫悟空",500);
Student s2 = new Student("豬八戒",500);
Student s3 = new Student("沙悟凈",500);
// 把他們添加進集合
list.add(s1);
list.add(s2);
list.add(s3);
// 遍歷集合
// 迭代器,集合特有
Iterator<Student> it = list.iterator();
while (it.hasNext()){
Student s = it.next();
System.out.println(s.getName() + "," + s.getAge());
}
// 普通for,帶有索引
for (int i=0;i<list.size();i++){
Student s = list.get(i);
System.out.println(s.getName() + "," + s.getAge());
}
// 增強for,最方便的
for (Student s : list){
System.out.println(s.getName() + "," + s.getAge());
}
}