百戰程序員8-容器
1、容器指的是“可以容納其他對象的對象”,這種說法對嗎?
對的
2、Collection/Set/List的聯系跟區別?
collection接口(list、set)和map接口的區別
3、Set和List的特點跟區別?
Set集合是無序的,元素不允許重復。HashSet, LinkedHashSet,TreeSet 可以實現Set接口。
List可以通過index知道元素的位置,它允許元素的重復。ArrayList, LinkedList, Vector可以實現List接口。
4、【上機】練習Collection接口中常用的方法
5、【上機】下面的代碼,效果一致嗎? 分析說明之。
Collection c = new HashSet(); Collection c2 = new HashSet(); Apple a = new Apple(); c.add(a); c2.addAll(c); //增加另一個容器中所有的元素! Collection c = new HashSet(); Collection c2 = new HashSet(); Apple a = new Apple(); c.add(a); c2.add(c); |
6、想取兩個容器中元素的交集,使用哪個方法?
7、說明isEmpty的作用,並說明下面代碼有問題嗎?
Collection c = null; System.out.println(c.isEmpty()); |
集合不為空才可以判斷
8、我想定義一個數組。該數組既可以放:Dog對象、也可以放Cat對象、還可以放
Integer對象,怎麽定義?
數組類型為object
Object[] objArr=new Object[5]; objArr[0]=56; objArr[1]=123; objArr[2]="Hello world"; objArr[3]=newDog(); objArr[4]=newCat();
9、List接口中增加了一些與順序相關的操作方法,下面兩個方法的作用是什麽?
add(int index, E element) , get(int index)
add(int index, E element): 在指定索引添加元素
get(int index) : 獲取指定索引的元素
10、ArrayList底層使用什麽來實現的? LinkedList是用什麽實現的?
11、說出ArrayLIst、LinkedList、Vector的區別。
List接口一共有三個實現類,分別是ArrayList、Vector和LinkedList。
ArrayList內部是由數組實現的,適合隨機查找和遍歷,不適合插入和刪除。
Vector與ArrayList一樣,也是通過數組實現的,不同的是它支持線程的同步,即某一時刻只有一個線程能夠寫Vector,避免多線程同時寫而引起的不一致性,但實現同步需要很高的花費,因此,訪問它比訪問ArrayList慢。
LinkedList是用鏈表結構存儲數據的,很適合數據的動態插入和刪除,隨機訪問和遍歷速度比較慢。另外,他還提供了List接口中沒有定義的方法,專門用於操作表頭和表尾元素,可以當作堆棧、隊列和雙向隊列使用。
12、我有一些數據,需要頻繁的查詢,插入和刪除操作非常少,並且沒有線程之間的共
享,使用List下面的哪個實現類好一些?
Arraylist
13、【上機】針對List中新增的有關順序的方法,每個都進行測試。並且使用debug
來幫助我們理解程序運行。
14、定義Computer類,使用價格排序。(使用Comparable接口)
定義computer類
package collection; public class Computer implements Comparable{ private double price; @Override public String toString() { return "Computer{" + "price=" + price + ‘}‘; } public Computer(double price) { super(); this.price = price; } @Override public int compareTo(Object o) { Computer c = (Computer) o; //方法調用對象與實參對象比較,即this與c對象比較 if (this.price>c.price){ return 1; }else if(this.price<c.price){ return -1; }else{ return 0; } } }View Code
測試類
package collection; import java.util.Iterator; import java.util.TreeSet; public class ComputerTest { public static void main(String[] args) { // // 創建treeset TreeSet<Computer> treeSet = new TreeSet<Computer>(); //創建computer對象 Computer c1 = new Computer(12); Computer c2 = new Computer(10); Computer c3 = new Computer(50); Computer c4 = new Computer(5); Computer c5 = new Computer(889); //把computer對象放入treeset treeSet.add(c1); treeSet.add(c2); treeSet.add(c3); treeSet.add(c4); treeSet.add(c5); //創建叠代器 Iterator<Computer> it = treeSet.iterator(); //遍歷treeset while (it.hasNext()){ System.out.println(it.next()); } } }View Code
15、equals返回true,hashcode一定相等嗎?
是的
16、HashSet和TreeSet的區別
HashSet:
-
-
- 存儲結構:采用hashtable哈希表存儲結構
- 優點: 添加,查詢,刪除快
- 缺點: 無序
-
TreeSet:
-
-
- 存儲結構:二叉樹
- 優點:有序,查詢速度快於list,慢於HashSet
-
17、使用HashSet存儲自定義對象,為什麽需要重寫hashCode()和equals()?
18、使用TreeSet存儲多個學生數據,實現按照不同屬性值進行排序?
19、【上機】說明Comparable接口作用。並定義一個學生類,使用分數來比較大小。
20、Map中,key能否重復?如果重復,會有什麽現象?
key 無序且唯一
添加重復的key不報錯,會直接覆蓋
21、Set和Map的集合類名稱相似,有沒有內在的聯系?
Hashmap和HashSet 都采用哈希表結構,需要用到hashCode哈希碼和equals方法
22、【上機】綜合使用List、Map容器存放如下數據, 並從map中取出“李四”。
姓名:張三 年齡:18 體重:90 地址:北京
姓名:李四 年齡:28 體重:50 地址:上海
註:不能使用Javabean封裝!
23、【上機】使用JavaBean封裝,完成上個題目的練習
24、【上機】寫出List、Set、Map中使用泛型的例子。
25、使用泛型有什麽好處?
26、【上機】用代碼寫出遍歷List的四種方式
27、【上機】用代碼寫出遍歷Set的兩種方式
28、【上機】用代碼寫出遍歷map的方式
29、采用增強for循環遍歷List或者Set,如果List或者Set沒有加泛型,能遍歷嗎?
能;
30、如果我想在遍歷時刪除元素,采用哪種遍歷方式最好?
Iterator接口,接口有remove方法
31、Iterator是一個接口還是類?
接口
32、Collection和Collections有什麽區別?
33、資源文件有什麽作用?
34、【上機】在src下建立一個資源文件(不包含中文),嘗試使用Property類讀取裏
面的屬性。
35、上機】使用entrySet方法遍歷Map。
36、Vector和ArrayList的區別聯系
線程安全、
非線程安全
37、Hashtable和HashMap的區別聯系
38、Java主要容器的選擇依據和應用場合
百戰程序員8-容器