1 public class Student
2 {
3 // 成員變量
4 private String name;
5 private int age;
6
7 // 構造方法
8 public Student()
9 {
10 super();
11 }
12
13 public Student(String name, int age)
14 {
15 super();
16 this.name = name;
17 this.age = age;
18 }
19
20 // 成員方法
21 // getXxx()/setXxx()
22 public String getName()
23 {
24 return name;
25 }
26
27 public void setName(String name)
28 {
29 this.name = name;
30 }
31
32 public int getAge()
33 {
34 return age;
35 }
36
37 public void setAge(int age)
38 {
39 this.age = age;
40 }
41
42 @Override
43 public String toString()
44 {
45 return "Student [name=" + name + ", age=" + age + "]";
46 }
47 }
1 /**
2 把5個學生的資訊存儲到數組中,并周遊數組,擷取得到每一個學生資訊。
3 * 學生:Student
4 * 成員變量:name,age
5 * 構造方法:無參,帶參
6 * 成員方法:getXxx()/setXxx()
7 * 分析:
8 * A:建立學生類。
9 * B:建立學生數組(對象數組)。
10 * C:建立5個學生對象,并指派。
11 * D:把C步驟的元素,放到數組中。
12 * E:周遊學生數組。
13 * */
14
15 public class Practice
16 {
17 public static void main(String[] args)
18 {
19 // 建立學生數組(對象數組)。
20 Student[] students = new Student[5];
21 // for (int x = 0; x < students.length; x++)
22 // {
23 // System.out.println(students[x]);
24 // }
25 // System.out.println("---------------------");
26
27 // 建立5個學生對象,并指派。
28 Student s1 = new Student("小明", 27);
29 Student s2 = new Student("小紅", 30);
30 Student s3 = new Student("小強", 30);
31 Student s4 = new Student("旺财", 12);
32 Student s5 = new Student("張三", 35);
33
34 // 将對象放到數組中。
35 students[0] = s1;
36 students[1] = s2;
37 students[2] = s3;
38 students[3] = s4;
39 students[4] = s5;
40
41 // 周遊
42 for (int x = 0; x < students.length; x++)
43 {
44 //System.out.println(students[x]);
45 Student s = students[x];
46 System.out.println(s.getName()+"---"+s.getAge());
47 }
48 }
49 }
15.02 對象數組的記憶體圖解
15.03 集合的由來及與數組的差別
集合類的由來:面向對象語言對事物的展現都是以對象的形式,是以為了友善對多個對象的操作,Java就提供了集合類。
數組和集合類同的差別:
數組可以存儲同一種類型的基本資料也可以存儲同一種類型的對象,但長度是固定的
集合隻可以存儲不同類型的對象,長度是可變的
集合類的特點:集合隻用于存儲對象,集合長度是可變的,集合可以存儲不同類型的對象。
15.04 集合的繼承體系圖解
集合容器因為内部的資料結構不同,有多種具體容器,根據共性内容不斷的向上抽取,就形成了集合架構。
架構的頂層Collection接口
15.05 Collection集合的功能概述
Collection 層次結構中的根接口。Collection 表示一組對象,這些對象也稱為 collection 的元素。一些 collection 允許有重複的元素,而另一些則不允許。一些 collection 是有序的,而另一些則是無序的。JDK 不提供此接口的任何直接實作:它提供更具體的子接口(如 Set 和 List)實作。此接口通常用來傳遞 collection,并在需要最大普遍性的地方操作這些 collection。
15.06 Collection集合的基本功能測試
成員方法:
1. boolean add(Ee):確定此 collection 包含指定的元素(可選操作)。
2. boolean remove(Objecto):從此 collection 中移除指定元素的單個執行個體,如果存在的話(可選操作)。
3. void clear():移除此 collection 中的所有元素(可選操作)。
4. boolean contains(Objecto):如果此 collection 包含指定的元素,則傳回 true。
5. boolean isEmpty():如果此 collection 不包含元素,則傳回 true。
6. int size():傳回此 collection 中的元素數。
例:
1 // 建立集合對象
2 // Collection c = new Collection(); //錯誤,因為接口不能執行個體化
3 Collection c = new ArrayList();
4 c.add("hello");
5 c.add("world");
6 c.add("java");
7 // c.clear();//移除所有元素
8 // System.out.println("remove:" + c.remove("hello"));//移除一個元素
9 // System.out.println("remove:" + c.remove("javaee"));
10 // 判斷集合中是否包含指定的元素
11 System.out.println("contains:"+c.contains("hello"));//contains:true
12 System.out.println("contains:"+c.contains("android"));//contains:false
13 //判斷集合是否為空
14 System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false
15 //元素的個數
16 System.out.println("size:"+c.size());//size:3
17 System.out.println("c:" + c);//c:[hello, world, java]
15.07 Collection集合的進階功能測試
1. boolean addAll(Collection<? extends E> c):
将指定 collection 中的所有元素都添加到此 collection 中(可選操作)。
2. boolean removeAll(Collection<?> c):
移除此 collection 中那些也包含在指定 collection 中的所有元素(可選操作)。
3. boolean containsAll(Collection<?> c):
如果此 collection 包含指定 collection 中的所有元素,則傳回 true。
4. boolean retainAll(Collection<?> c):
僅保留此 collection 中那些也包含在指定 collection 的元素(可選操作)。換句話說,移除此 collection 中未包含在指定 collection 中的所有元素。
c1.addAll(c2);//将c2集合中的所有元素添加到c1集合中,c1變c2不變
c1.removeAll(c2);//将c1集合中與c2集合相同的所有元素删除,隻要有一個相同的就傳回true
c1.containsAll(c2);//判斷c1集合中的元素是否包含c2中的全部元素,全部包含則傳回true
c1.retainAll(c2);//将c1集合中與c2集合相同的元素保留,删除其他元素,傳回值表示c1集合是否發生變化,發生變化傳回true,沒有變化傳回false
15.08 集合的周遊之集合轉數組周遊
Object[] toArray():傳回包含此 collection 中所有元素的數組。
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 Collection c = new ArrayList();
7 c.add("hello");
8 c.add("world");
9 c.add("java");
10
11 Object[] objs = c.toArray();
12 for (int i = 0; i < objs.length; i++)
13 {
14 //向下轉為String類型
15 String s = (String)objs[i];
16 System.out.println(s+":"+s.length());
17 }
18 }
19 }
運作結果:
hello:5
world:5
java:4
15.09 Collection存儲自定義對象并周遊案例(使用數組)
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 Collection c = new ArrayList();
7 //建立學生對象并添加到集合
8 c.add(new Student("小明",23));
9 c.add(new Student("小紅",32));
10 c.add(new Student("小強",14));
11 c.add(new Student("旺财",8));
12 c.add(new Student("張三",16));
13
14 Object[] objs = c.toArray();
15 for (int i = 0; i < objs.length; i++)
16 {
17 Student s = (Student)objs[i];
18 System.out.println(s.getName()+":"+s.getAge());
19 }
20 }
21 }
小明:23
小紅:32
小強:14
旺财:8
張三:16
15.10 集合的周遊之疊代器周遊
Iterator<E> iterator():傳回在此 collection 的元素上進行疊代的疊代器。
1 // 建立集合
2 Collection c = new ArrayList();
3 //建立元素并添加到集合
4 c.add("hello");
5 c.add("world");
6 c.add("java");
7 //擷取疊代器,實際傳回的是子類對象,多态
8 Iterator it = c.iterator();
9 while(it.hasNext())
10 {
11 System.out.println(it.next());
12 }
15.11 Collection存儲自定義對象并周遊案例(使用疊代器)
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 Collection c = new ArrayList();
7 //建立學生對象并添加到集合
8 c.add(new Student("小明",23));
9 c.add(new Student("小紅",32));
10 c.add(new Student("小強",14));
11 c.add(new Student("旺财",8));
12 c.add(new Student("張三",16));
13
14 Iterator it = c.iterator();
15 while(it.hasNext())
16 {
17 Student s = (Student)it.next();
18 System.out.println(s.getName()+":"+s.getAge());
19 }
20 }
21 }
15.12 疊代器使用的問題探讨
1.使用疊代器擷取元素的兩種方式:
方式1:
Iterator it = c.iterator();
while(it.hasNext())
{
Student s = (Student)it.next();
System.out.println(s.getName()+":"+s.getAge());
}
方式2:
for(Iterator it = c.iterator();it.hasNext();)
{
Student s = (Student)it.next();
System.out.println(s.getName()+":"+s.getAge());
}
使用方式2的好處:it在for循環結束後就變成垃圾,效率較高
2.不要多次使用it.next()方法
Iterator it = c.iterator();
while(it.hasNext())
{
System.out.println(((Student)it.next()).getName());
System.out.println(((Student)it.next()).getAge());
}
上面的代碼表示擷取的是第1個學生的姓名,第2個學生的年齡,以此類推,如果集合中的元素是奇數個,則會報NoSuchElementException錯誤
15.13 集合的使用步驟圖解
集合的使用步驟:
1. 建立集合對象
2. 建立元素對象
3. 将元素添加到集合
4. 周遊集合
4.1 通過集合對象擷取疊代器對象
4.2 通過疊代器對象的hasNext()方法判斷是否有元素
4.3 通過疊代器對象的Next()方法擷取元素并移動到下一個位置
15.14 疊代器的原理及源碼解析
原理:
假設疊代器定義的是一個類,那麼就可以建立該類的對象,調用該類的方法來實作集合的周遊,但是Java中提供了很多的集合類,而這些集合類的資料結構是不同的,是以存儲的方式和周遊的方式也應該是不同的,進而它們的周遊方式也應該是不一樣的。最終就沒有定義疊代器類
而無論使用哪種集合都應該具備擷取元素的操作,并且最好再輔助與判斷功能,也就是說判斷功能和擷取功能應該是一個集合周遊所具備的,而每種集合的方式又不太一樣,是以将這兩個功能給提取出來,并不提供具體實作,這種方式就是接口,而真正具體的實作類在具體的子類中以内部類的方式展現的
源碼:
public interface Iterator
{
boolean hasNext();
Object next();
}
public interface Iterable
{
Iterator iterator();
}
public interface Collection extends Iterable
{
Iterator iterator();
}
public interface List extends Collection
{
Iterator iterator();
}
public class ArrayList implements List
{
public Iterator iterator()
{
return new Itr();
}
//内部類
private class Itr implements Iterator
{
public boolean hasNext() {}
public Object next(){}
}
}
Collection c = new ArrayList();
c.add("hello");
c.add("world");
c.add("java");
Iterator it = c.iterator(); //new Itr();
while(it.hasNext())
{
String s = (String)it.next();
System.out.println(s);
}
15.15 Collection存儲字元串并周遊
1 import java.util.ArrayList;
2 import java.util.Collection;
3 import java.util.Iterator;
4
5 public class Practice
6 {
7
8 public static void main(String[] args)
9 {
10
11 // 建立集合
12 Collection c = new ArrayList();
13
14 //添加字元串
15 c.add("hello");
16 c.add("你好");
17 c.add("world");
18 c.add("java");
19 c.add("旺财");
20 //通過集合對象擷取疊代器對象
21
22 Iterator it = c.iterator();
23 while(it.hasNext())
24
25 {
26 String s = (String)it.next();
27
28 System.out.println(s);
29 }
30
31 }
32
33 }
15.16 Collection存儲學生對象并周遊
1 import java.util.ArrayList;
2 import java.util.Collection;
3 import java.util.Iterator;
4
5 public class Practice
6 {
7 public static void main(String[] args)
8 {
9 // 建立集合
10 Collection c = new ArrayList();
11 //建立學生對象并添加到集合
12 c.add(new Student("小明",23));
13 c.add(new Student("小紅",32));
14 c.add(new Student("小強",14));
15 c.add(new Student("旺财",8));
16 c.add(new Student("張三",16));
17
18 Iterator it = c.iterator();
19 while(it.hasNext())
20 {
21 Student s = (Student)it.next();
22 System.out.println(s.getName()+":"+s.getAge());
23 }
24 }
25 }
15.17 List存儲字元串并周遊
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 List list = new ArrayList();
7 list.add("hello");
8 list.add("world");
9 list.add("java");
10
11 Iterator it = list.iterator();
12 while(it.hasNext())
13 {
14 String s = (String)it.next();
15 System.out.println(s);
16 }
17 }
18 }
15.18 List集合的特點
List接口概述:有序的(存取順序一緻)collection(也稱為序列)。此接口的使用者可以對清單中每個元素的插入位置進行精确地控制。使用者可以根據元素的整數索引(在清單中的位置)通路元素,并搜尋清單中的元素。
特點:與 set 不同,清單通常允許重複的元素。
15.19 List存儲學生對象并周遊
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 List list = new ArrayList();
7 //建立學生對象并添加到集合
8 list.add(new Student("小明",23));
9 list.add(new Student("小紅",32));
10 list.add(new Student("小強",14));
11 list.add(new Student("旺财",8));
12 list.add(new Student("張三",16));
13
14 Iterator it = list.iterator();
15 while(it.hasNext())
16 {
17 Student s = (Student)it.next();
18 System.out.println(s.getName()+":"+s.getAge());
19 }
20 }
21 }
15.20 List集合的特有功能概述和測試
1. void add(int index,Eelement):
在清單的指定位置插入指定元素(可選操作)。
2. Eremove(int index):
移除清單中指定位置的元素(可選操作)。
3. Eget(int index):
傳回清單中指定位置的元素。
4. Eset(int index, Eelement):
用指定元素替換清單中指定位置的元素(可選操作)。
list.add(2,"javaee");//在2的位置插入javaee,改變集合長度
list.get(2)//傳回集合中2位置上的元素,不改變集合長度
list.remove(1)//删除集合中1位置上的元素,傳回被删除的元素,改變集合長度
list.set(2, "javaee")//将集合中2位置上的元素替換為javaee,傳回被替換的元素,不改變集合長度
15.21 List集合的特有周遊功能
1 for (int i = 0; i < list.size(); i++)
2
3 {
4
5 String s = (String)list.get(i);
6
7 System.out.println(s);
8
9 }
15.22 List存儲自定義對象并周遊(使用List特有功能周遊)
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 List list = new ArrayList();
7 //建立學生對象并添加到集合
8 list.add(new Student("小明",23));
9 list.add(new Student("小紅",32));
10 list.add(new Student("小強",14));
11 list.add(new Student("旺财",8));
12 list.add(new Student("張三",16));
13
14 for (int i = 0; i < list.size(); i++)
15 {
16 Student s =(Student)list.get(i);
17 System.out.println(s.getName()+":"+s.getAge());
18 }
19 }
20 }
15.23 ListIterator的特有功能
ListIterator<E> listIterator():
傳回此清單元素的清單疊代器(按适當順序)。
注意:ListIterator可以實作逆向周遊,但是必須先正向周遊,才能逆向周遊
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 List list = new ArrayList();
7
8 list.add("hello");
9 list.add("world");
10 list.add("java");
11 //清單疊代器
12 ListIterator lit = list.listIterator();
13 //正向周遊
14 while(lit.hasNext())
15 {
16 String s = (String)lit.next();
17 System.out.println(s);
18 }
19 System.out.println("-----");
20 //逆向周遊
21 while(lit.hasPrevious())
22 {
23 //擷取上一個元素
24 String s = (String)lit.previous();
25 System.out.println(s);
26 }
27
28 }
29 }
hello
world
java
-----
15.24 并發修改異常的産生原因及解決方案
1 public class Practice
2 {
3 public static void main(String[] args)
4 {
5 // 建立集合
6 List list = new ArrayList();
7
8 list.add("hello");
9 list.add("world");
10 list.add("java");
11 Iterator it = list.iterator();
12 while(it.hasNext())
13 {
14 String s = (String)it.next();
15 if(s.equals("world"))
16 list.add("javaee");
17 }
18 System.out.println(list);
19 }
20 }
上面的代碼會運作錯誤,發生ConcurrentModificationException異常
錯誤産生原因:疊代器是依賴于集合存在的,在疊代的過程中使用集合的方法添加元素疊代器是不知道的,是以報錯,并發修改異常
解決方案:1.用疊代器疊代元素時使用疊代器修改元素(ListIterator清單疊代器),添加的元素在疊代的元素後面
2.用集合周遊元素,用集合修改元素(for循環),添加的元素在最後
15.25 資料結構之棧和隊列
資料結構:資料的組織方式
15.26 資料結構之數組和連結清單
數組:存儲同一種類型的多個元素的容器
連結清單:由一個鍊子把多個節點(資料和節點)連起來組成的資料
15.27 List的三個子類的特點
ArrayList:底層資料結構是數組,查詢快,增删慢,是不同步的,線程不安全,效率高
Vector:底層資料結構是數組,查詢快,增删慢,是同步的,線程安全,效率低
LinkedList:底層資料結構是連結清單,查詢慢,增删快,是不同步的,線程不安全,效率高