Java集合

七七喜欢你發表於2024-03-08

1.集合概述

1.1集合和陣列的區別

陣列增加長度不方便、型別不能任意、增加刪除元素不方便
集合可以動態儲存多個物件、集合提供了一系列操作物件的方法:add、remove、set、get

1.2集合分類

大致可以分成兩類 單列和雙列

單列集合和雙列集合的區別:

雙列是鍵值對傳參 傳入的是K-V

Java集合 Java集合

!!注意:介面和類區分

1.3Collection介面特點

繼承於Iterable
Collection的常用方法(介面的方法 相當於抽象方法)
Java集合

重要

package com.hspedu.collection_;
import java.util.ArrayList;
import java.util.List;

public class CollectionMethod {
@SuppressWarnings({"all"})
public static void main(String[] args) {
List list = new ArrayList();
// add:新增單個元素
list.add("jack");
list.add(10);//list.add(new Integer(10))
list.add(true);
System.out.println("list=" + list);
// remove:刪除指定元素
//list.remove(0);//刪除第一個元素
list.remove(true);//指定刪除某個元素
System.out.println("list=" + list);
// contains:查詢元素是否存在
System.out.println(list.contains("jack"));//T
// size:獲取元素個數
System.out.println(list.size());//2
韓順平循序漸進學 Java 零基礎
第 604頁
// isEmpty:判斷是否為空
System.out.println(list.isEmpty());//F
// clear:清空
list.clear();
System.out.println("list=" + list);
// addAll:新增多個元素
ArrayList list2 = new ArrayList();
list2.add("紅樓夢");
list2.add("三國演義");
list.addAll(list2);
System.out.println("list=" + list);
// containsAll:查詢多個元素是否都存在
System.out.println(list.containsAll(list2));//T
// removeAll:刪除多個元素
list.add("聊齋");
list.removeAll(list2);
System.out.println("list=" + list);//[聊齋]
// 說明:以 ArrayList 實現類來演示. }
}

1.4Collection介面遍歷元素方式

1.4.1.使用Iterator迭代器(快捷鍵itit)

自己總結:所有實現Collection介面的集合類都有一個iterator()方法,用以返回一個實現了Iterator介面的物件,
即可以返回一個迭代器。
Iterator僅用於遍歷集合,Iterator本身不存放物件。
必須要使用hasnext()作為跳出條件

package com.hspedu.collection_;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class CollectionIterator {

@SuppressWarnings({"all"})
public static void main(String[] args) {
Collection col = new ArrayList();
col.add(new Book("三國演義", "羅貫中", 10.1));
col.add(new Book("小李飛刀", "古龍", 5.1));
col.add(new Book("紅樓夢", "曹雪芹", 34.6));
//System.out.println("col=" + col);
//現在老師希望能夠遍歷 col 集合
//1. 先得到 col 對應的 迭代器
Iterator iterator = col.iterator();
//2. 使用 while 迴圈遍歷
// while (iterator.hasNext()) {//判斷是否還有資料
// //返回下一個元素,型別是 Object
// Object obj = iterator.next();
// System.out.println("obj=" + obj);
// }
//老師教大家一個快捷鍵,快速生成 while => itit
//顯示所有的快捷鍵的的快捷鍵 ctrl + j
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println("obj=" + obj);

}
//3. 當退出 while 迴圈後 , 這時 iterator 迭代器,指向最後的元素
// iterator.next();//NoSuchElementException
//4. 如果希望再次遍歷,需要重置我們的迭代器
iterator = col.iterator();
System.out.println("===第二次遍歷===");
while (iterator.hasNext()) {
Object obj = iterator.next();
System.out.println("obj=" + obj);
}
}
}
class Book {
private String name;
private String author;
private double price;
public Book(String name, String author, double price) {
this.name = name;
this.author = author;
this.price = price;
}

public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public double getPrice() {
return price;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {

return "Book{" +
"name='" + name + '\'' +
", author='" + author + '\'' +
", price=" + price +
'}';
}
}

1.4.2for迴圈增強

增強for迴圈就是簡化的iterator,只能用於遍歷集合和陣列

List list = new ArrayList();
list.add(new Dog("小黑", 3));
list.add(new Dog("大黃", 100));
list.add(new Dog("大壯", 8));
//先使用 for 增強
for (Object dog : list) {
System.out.println("dog=" + dog);
}

2、List介面

2.1List介面基本

  1. List集合類中元素有序,新增和取出順序一致,而且可以重複。
  2. List每個元素有對應的索引,即支援索引。 (list.get(i) 索引從0開始 )
  3. List介面下有 ArrayList LinkedList Vector 三種。

常用的方法:
1.void add(int index, Object ele):在 index 位置插入 ele 元素
2.boolean addAll(int index, Collection eles):從 index 位置開始將 eles 中的所有元素新增進來
3.int indexOf(Object obj):返回 obj 在集合中首次出現的位置
4.int lastIndexOf(Object obj):返回 obj 在當前集合中末次出現的位置
5.Object remove(int index):移除指定 index 位置的元素,並返回此元素
6.Object set(int index, Object ele):設定指定 index 位置的元素為 ele , 相當於是替換
7.List subList(int fromIndex, int toIndex):返回從 fromIndex 到 toIndex 位置的子集合
List returnlist = list.subList(0,2)