------- android培訓(xùn)、java培訓(xùn)、期待與您交流! ----------
15.01 對(duì)象數(shù)組的概述和使用
1 public class Student 2 { 3 // 成員變量 4 PRivate String name; 5 private int age; 6 7 // 構(gòu)造方法 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 @Override43 public String toString() 44 {45 return "Student [name=" + name + ", age=" + age + "]";46 }47 } 1 /** 2 把5個(gè)學(xué)生的信息存儲(chǔ)到數(shù)組中,并遍歷數(shù)組,獲取得到每一個(gè)學(xué)生信息。 3 * 學(xué)生:Student 4 * 成員變量:name,age 5 * 構(gòu)造方法:無(wú)參,帶參 6 * 成員方法:getXxx()/setXxx() 7 * 分析: 8 * A:創(chuàng)建學(xué)生類(lèi)。 9 * B:創(chuàng)建學(xué)生數(shù)組(對(duì)象數(shù)組)。10 * C:創(chuàng)建5個(gè)學(xué)生對(duì)象,并賦值。11 * D:把C步驟的元素,放到數(shù)組中。12 * E:遍歷學(xué)生數(shù)組。13 * */14 15 public class Practice 16 {17 public static void main(String[] args)18 {19 // 創(chuàng)建學(xué)生數(shù)組(對(duì)象數(shù)組)。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 // 創(chuàng)建5個(gè)學(xué)生對(duì)象,并賦值。28 Student s1 = new Student("小明", 27);29 Student s2 = new Student("小紅", 30);30 Student s3 = new Student("小強(qiáng)", 30);31 Student s4 = new Student("旺財(cái)", 12);32 Student s5 = new Student("張三", 35);33 34 // 將對(duì)象放到數(shù)組中。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 對(duì)象數(shù)組的內(nèi)存圖解

15.03 集合的由來(lái)及與數(shù)組的區(qū)別
集合類(lèi)的由來(lái):面向?qū)ο笳Z(yǔ)言對(duì)事物的體現(xiàn)都是以對(duì)象的形式,所以為了方便對(duì)多個(gè)對(duì)象的操作,Java就提供了集合類(lèi)。
數(shù)組和集合類(lèi)同的區(qū)別:
數(shù)組可以存儲(chǔ)同一種類(lèi)型的基本數(shù)據(jù)也可以存儲(chǔ)同一種類(lèi)型的對(duì)象,但長(zhǎng)度是固定的
集合只可以存儲(chǔ)不同類(lèi)型的對(duì)象,長(zhǎng)度是可變的
集合類(lèi)的特點(diǎn):集合只用于存儲(chǔ)對(duì)象,集合長(zhǎng)度是可變的,集合可以存儲(chǔ)不同類(lèi)型的對(duì)象。
15.04 集合的繼承體系圖解
集合容器因?yàn)閮?nèi)部的數(shù)據(jù)結(jié)構(gòu)不同,有多種具體容器,根據(jù)共性?xún)?nèi)容不斷的向上抽取,就形成了集合框架。
框架的頂層Collection接口

15.05 Collection集合的功能概述
Collection 層次結(jié)構(gòu)中的根接口。Collection 表示一組對(duì)象,這些對(duì)象也稱(chēng)為 collection 的元素。一些 collection 允許有重復(fù)的元素,而另一些則不允許。一些 collection 是有序的,而另一些則是無(wú)序的。JDK 不提供此接口的任何直接實(shí)現(xiàn):它提供更具體的子接口(如 Set 和 List)實(shí)現(xiàn)。此接口通常用來(lái)傳遞 collection,并在需要最大普遍性的地方操作這些 collection。
15.06 Collection集合的基本功能測(cè)試
成員方法:
1. boolean add(E e):確保此 collection 包含指定的元素(可選操作)。
2. boolean remove(Object o):從此 collection 中移除指定元素的單個(gè)實(shí)例,如果存在的話(huà)(可選操作)。
3. void clear():移除此 collection 中的所有元素(可選操作)。
4. boolean contains(Object o):如果此 collection 包含指定的元素,則返回 true。
5. boolean isEmpty():如果此 collection 不包含元素,則返回 true。
6. int size():返回此 collection 中的元素?cái)?shù)。
例:
1 // 創(chuàng)建集合對(duì)象 2 // Collection c = new Collection(); //錯(cuò)誤,因?yàn)榻涌诓荒軐?shí)例化 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"));//移除一個(gè)元素 9 // System.out.println("remove:" + c.remove("javaee"));10 // 判斷集合中是否包含指定的元素11 System.out.println("contains:"+c.contains("hello"));//contains:true12 System.out.println("contains:"+c.contains("android"));//contains:false13 //判斷集合是否為空14 System.out.println("isEmpty:"+c.isEmpty());//isEmpty:false15 //元素的個(gè)數(shù)16 System.out.println("size:"+c.size());//size:317 System.out.println("c:" + c);//c:[hello, world, java]15.07 Collection集合的高級(jí)功能測(cè)試
成員方法:
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 的元素(可選操作)。換句話(huà)說(shuō),移除此 collection 中未包含在指定 collection 中的所有元素。
例:
c1.addAll(c2);//將c2集合中的所有元素添加到c1集合中,c1變c2不變c1.removeAll(c2);//將c1集合中與c2集合相同的所有元素刪除,只要有一個(gè)相同的就返回truec1.containsAll(c2);//判斷c1集合中的元素是否包含c2中的全部元素,全部包含則返回truec1.retainAll(c2);//將c1集合中與c2集合相同的元素保留,刪除其他元素,返回值表示c1集合是否發(fā)生變化,發(fā)生變化返回true,沒(méi)有變化返回false
15.08 集合的遍歷之集合轉(zhuǎn)數(shù)組遍歷
Object[] toArray():返回包含此 collection 中所有元素的數(shù)組。
例:
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 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 //向下轉(zhuǎn)為String類(lèi)型15 String s = (String)objs[i];16 System.out.println(s+":"+s.length());17 }18 }19 }運(yùn)行結(jié)果:
hello:5world:5java:4
15.09 Collection存儲(chǔ)自定義對(duì)象并遍歷案例(使用數(shù)組)
例:
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 6 Collection c = new ArrayList(); 7 //創(chuàng)建學(xué)生對(duì)象并添加到集合 8 c.add(new Student("小明",23)); 9 c.add(new Student("小紅",32));10 c.add(new Student("小強(qiáng)",14));11 c.add(new Student("旺財(cái)",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 }運(yùn)行結(jié)果:
小明:23小紅:32小強(qiáng):14旺財(cái):8張三:16
15.10 集合的遍歷之迭代器遍歷
Iterator<E> iterator():返回在此 collection 的元素上進(jìn)行迭代的迭代器。
例:
1 // 創(chuàng)建集合 2 Collection c = new ArrayList(); 3 //創(chuàng)建元素并添加到集合 4 c.add("hello"); 5 c.add("world"); 6 c.add("java"); 7 //獲取迭代器,實(shí)際返回的是子類(lèi)對(duì)象,多態(tài) 8 Iterator it = c.iterator(); 9 while(it.hasNext())10 {11 System.out.println(it.next());12 }15.11 Collection存儲(chǔ)自定義對(duì)象并遍歷案例(使用迭代器)
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 6 Collection c = new ArrayList(); 7 //創(chuàng)建學(xué)生對(duì)象并添加到集合 8 c.add(new Student("小明",23)); 9 c.add(new Student("小紅",32));10 c.add(new Student("小強(qiáng)",14));11 c.add(new Student("旺財(cái)",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 迭代器使用的問(wèn)題探討
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循環(huán)結(jié)束后就變成垃圾,效率較高
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個(gè)學(xué)生的姓名,第2個(gè)學(xué)生的年齡,以此類(lèi)推,如果集合中的元素是奇數(shù)個(gè),則會(huì)報(bào)NoSuchElementException錯(cuò)誤
15.13 集合的使用步驟圖解

集合的使用步驟:
1. 創(chuàng)建集合對(duì)象
2. 創(chuàng)建元素對(duì)象
3. 將元素添加到集合
4. 遍歷集合
4.1 通過(guò)集合對(duì)象獲取迭代器對(duì)象
4.2 通過(guò)迭代器對(duì)象的hasNext()方法判斷是否有元素
4.3 通過(guò)迭代器對(duì)象的Next()方法獲取元素并移動(dòng)到下一個(gè)位置
15.14 迭代器的原理及源碼解析
原理:
假設(shè)迭代器定義的是一個(gè)類(lèi),那么就可以創(chuàng)建該類(lèi)的對(duì)象,調(diào)用該類(lèi)的方法來(lái)實(shí)現(xiàn)集合的遍歷,但是Java中提供了很多的集合類(lèi),而這些集合類(lèi)的數(shù)據(jù)結(jié)構(gòu)是不同的,所以存儲(chǔ)的方式和遍歷的方式也應(yīng)該是不同的,進(jìn)而它們的遍歷方式也應(yīng)該是不一樣的。最終就沒(méi)有定義迭代器類(lèi)
而無(wú)論使用哪種集合都應(yīng)該具備獲取元素的操作,并且最好再輔助與判斷功能,也就是說(shuō)判斷功能和獲取功能應(yīng)該是一個(gè)集合遍歷所具備的,而每種集合的方式又不太一樣,所以將這兩個(gè)功能給提取出來(lái),并不提供具體實(shí)現(xiàn),這種方式就是接口,而真正具體的實(shí)現(xiàn)類(lèi)在具體的子類(lèi)中以?xún)?nèi)部類(lèi)的方式體現(xiàn)的
源碼:
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(); } //內(nèi)部類(lèi) 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存儲(chǔ)字符串并遍歷
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 // 創(chuàng)建集合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("旺財(cái)");20 //通過(guò)集合對(duì)象獲取迭代器對(duì)象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存儲(chǔ)學(xué)生對(duì)象并遍歷
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 // 創(chuàng)建集合10 Collection c = new ArrayList();11 //創(chuàng)建學(xué)生對(duì)象并添加到集合12 c.add(new Student("小明",23));13 c.add(new Student("小紅",32));14 c.add(new Student("小強(qiáng)",14));15 c.add(new Student("旺財(cái)",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存儲(chǔ)字符串并遍歷
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 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集合的特點(diǎn)
List接口概述:有序的(存取順序一致)collection(也稱(chēng)為序列)。此接口的用戶(hù)可以對(duì)列表中每個(gè)元素的插入位置進(jìn)行精確地控制。用戶(hù)可以根據(jù)元素的整數(shù)索引(在列表中的位置)訪(fǎng)問(wèn)元素,并搜索列表中的元素。
特點(diǎn):與 set 不同,列表通常允許重復(fù)的元素。
15.19 List存儲(chǔ)學(xué)生對(duì)象并遍歷
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 6 List list = new ArrayList(); 7 //創(chuàng)建學(xué)生對(duì)象并添加到集合 8 list.add(new Student("小明",23)); 9 list.add(new Student("小紅",32));10 list.add(new Student("小強(qiáng)",14));11 list.add(new Student("旺財(cái)",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集合的特有功能概述和測(cè)試
1. void add(int index,E element):
在列表的指定位置插入指定元素(可選操作)。
2. E remove(int index):
移除列表中指定位置的元素(可選操作)。
3. E get(int index):
返回列表中指定位置的元素。
4. E set(int index, E element):
用指定元素替換列表中指定位置的元素(可選操作)。
例:
list.add(2,"javaee");//在2的位置插入javaee,改變集合長(zhǎng)度
list.get(2)//返回集合中2位置上的元素,不改變集合長(zhǎng)度
list.remove(1)//刪除集合中1位置上的元素,返回被刪除的元素,改變集合長(zhǎng)度
list.set(2, "javaee")//將集合中2位置上的元素替換為javaee,返回被替換的元素,不改變集合長(zhǎng)度
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存儲(chǔ)自定義對(duì)象并遍歷(使用List特有功能遍歷)
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 6 List list = new ArrayList(); 7 //創(chuàng)建學(xué)生對(duì)象并添加到集合 8 list.add(new Student("小明",23)); 9 list.add(new Student("小紅",32));10 list.add(new Student("小強(qiáng)",14));11 list.add(new Student("旺財(cái)",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():
返回此列表元素的列表迭代器(按適當(dāng)順序)。
注意:ListIterator可以實(shí)現(xiàn)逆向遍歷,但是必須先正向遍歷,才能逆向遍歷
例:
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 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 //獲取上一個(gè)元素24 String s = (String)lit.previous();25 System.out.println(s);26 }27 28 }29 }運(yùn)行結(jié)果:
hello
world
java
-----
java
world
hello
15.24 并發(fā)修改異常的產(chǎn)生原因及解決方案
例:
1 public class Practice 2 { 3 public static void main(String[] args) 4 { 5 // 創(chuàng)建集合 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 }上面的代碼會(huì)運(yùn)行錯(cuò)誤,發(fā)生ConcurrentModificationException異常
錯(cuò)誤產(chǎn)生原因:迭代器是依賴(lài)于集合存在的,在迭代的過(guò)程中使用集合的方法添加元素迭代器是不知道的,所以報(bào)錯(cuò),并發(fā)修改異常
解決方案:1.用迭代器迭代元素時(shí)使用迭代器修改元素(ListIterator列表迭代器),添加的元素在迭代的元素后面
2.用集合遍歷元素,用集合修改元素(for循環(huán)),添加的元素在最后
15.25 數(shù)據(jù)結(jié)構(gòu)之棧和隊(duì)列
數(shù)據(jù)結(jié)構(gòu):數(shù)據(jù)的組織方式

15.26 數(shù)據(jù)結(jié)構(gòu)之?dāng)?shù)組和鏈表
數(shù)組:存儲(chǔ)同一種類(lèi)型的多個(gè)元素的容器

鏈表:由一個(gè)鏈子把多個(gè)節(jié)點(diǎn)(數(shù)據(jù)和節(jié)點(diǎn))連起來(lái)組成的數(shù)據(jù)

15.27 List的三個(gè)子類(lèi)的特點(diǎn)
ArrayList:底層數(shù)據(jù)結(jié)構(gòu)是數(shù)組,查詢(xún)快,增刪慢,是不同步的,線(xiàn)程不安全,效率高
Vector:底層數(shù)據(jù)結(jié)構(gòu)是數(shù)組,查詢(xún)快,增刪慢,是同步的,線(xiàn)程安全,效率低
LinkedList:底層數(shù)據(jù)結(jié)構(gòu)是鏈表,查詢(xún)慢,增刪快,是不同步的,線(xiàn)程不安全,效率高
|
新聞熱點(diǎn)
疑難解答
圖片精選
網(wǎng)友關(guān)注