一、 填空題
- Java集合框架提供了一套性能優良、使用方便的接口和類,包括Collection和Map兩大類,它們都位於 java.util 包中
- 隊列和堆棧有些相似,不同之處在於棧是先進后出,隊列是先進先出 。
- 鏈表 結構是一種由多個節點組成的線性數據結構,並且每個節點包含有數據以及指向下一個節點的引用。
- ___LinkedList__是一種集合類,它 采用鏈表作為的存儲結構,便於刪除和添加元素,但是按照索引查詢元素效率低下。
- TreeSet 是一種Collection類型的集合類,其中元素唯一,並采用二叉樹作為存儲結構,元素按照自然順序排列。
- 如果希望將自定義類Student的多個對象放入集合TreeSet,實現所有元素按照某個屬性的自然順序排列,則需要Student類實現__Comparable__接口。
- 在Java中 HashMap 集合的訪問時間接近穩定,它是一種鍵值對映射的數據結構。這個數據結構是通過數組來實現的。
- 迭代器Iterator為集合而生,專門實現集合遍歷,該接口有三個方法,分別是hasNext() 、__next()_、remove()。
二、 選擇題
1. |
以下選項中關於Java集合的說法錯誤的是( AC )。(選擇二項) |
|
|
|
|
|
A. |
List接口和Set接口是Collections接口有兩個子接口 |
|
B. |
List接口中存放的元素具有有序,不唯一的特點 |
|
C. |
Set接口中存放的元素具有無序,不唯一的特點 |
|
D. |
Map接口存放的是映射信息,每個元素都是一個鍵值對 |
2. |
如下Java代碼,輸出的運行結果是( A )。(選擇一項) |
|
|
public class Test { public static void main(String[ ] args) { List<String> list=new ArrayList<String>(); list.add("str1"); list.add(2, "str2"); String s=list.get(1); System.out.println(s); } } |
|
|
|
|
|
A |
運行時出現異常 |
|
B. |
正確運行,輸出str1 |
|
C. |
正確運行,輸出str2 |
|
D. |
編譯時出現異常 |
3. |
以下Java代碼的作用是首先將一個數組的內容存入集合,然后判斷集合中是否有指定的元素存在,其中共有( D )處錯誤。(選擇一項) |
|
|
import java.util.List; public class Test { public int getIndexofArray(float[] f){ int rtn=-1; float objf=3.4; List list=null; for(int i=0;i<f.size( );i++){ list.add(f[i]); } for(int i=0;i<list.size( );i++){ float tmp=(float)list.get(i); if(objf==tmp){ rtn=i; } } return rtn; } } |
|
|
|
|
|
A |
0 |
|
B. |
1 |
|
C. |
2 |
|
D. |
3 |
4. |
分析如下Java 代碼,編譯運行后將輸出( B )。(選擇一項) |
|
|
public class Test { public Test() { } static void print(List<Integer> al) { al.add(2); al = new ArrayList<Integer>(); al.add(3); al.add(4); } public static void main(String[] args) { List<Integer> al = new ArrayList<Integer>(); al.add(1); print(al); System.out.println(al.get(1)); } } |
|
|
|
|
|
A |
1 |
|
B. |
2 |
|
C. |
3 |
|
D. |
4 |
5. |
在Java中,下列集合類型可以存儲無序、不重復的數據的是( D )。(選擇一項) |
|
|
|
|
|
A |
ArrayList |
|
B. |
LinkedList |
|
C. |
TreeSet |
|
D. |
HashSet |
6. |
以下代碼的執行結果是( C )。(選擇一項) |
|
|
Set<String> s=new HashSet<String>(); s.add("abc"); s.add("abc"); s.add("abcd"); s.add("ABC"); System.out.println(s.size()); |
|
|
|
|
|
A. |
1 |
|
B. |
2 |
|
C. |
3 |
|
D. |
4 |
7. |
給定如下Java代碼,編譯運行的結果是( C )。(選擇一項) |
|
|
public class Test { public static void main(String[] args) { Map<String, String> map = new HashMap<String, String>(); String s = "code"; map.put(s, "1"); map.put(s, "2"); System.out.println(map.size()); } } |
|
|
|
|
|
A |
編譯時發生錯誤 |
|
B. |
運行時引發異常 |
|
C. |
正確運行,輸出:1 |
|
D. |
正確運行,輸出:2 |
8. |
下面集合類中屬於非線程安全,且結構采用了哈希表的是( C )。(選擇一項) |
||
|
|
|
|
|
A. |
Vector |
|
|
B. |
ArrayList |
|
|
C. |
HashMap |
|
|
D. |
Hashtable |
|
9. |
在Java中,LinkedList類與ArrayList類同屬於集合框架類,下列( CD )選項中是LinkedList類有而ArrayList類沒有的方法。(選擇兩項) |
||
|
|
|
|
|
A |
add(Object o) |
|
|
B. |
add(int index,Object o) |
|
|
C. |
getFirst() |
|
|
D. |
removeLast() |
|
三、 判斷題
- 數組和集合中的元素可以是任何數據類型,包括基本類型和引用類型。( F )
- Java集合中的Set接口和List接口都是從Collection接口派生出來的。( T )
- Collection 接口存儲一組不唯一,有序的對象,它有兩個子接口:List和Set。( F )
- Collection是Java集合頂級接口,其中的元素無序,唯一。Java平台不提供這個接口任何直接的實現。( F )
- List是有序的Collection,使用此接口能夠精確的控制每個元素插入的位置。用戶能夠使用索引來訪問List中的無素,這類似於Java的數組。( T )
- HashSet采用哈希表存儲結構,特點是查詢速度快,但是其中元素無序排列。( T )
- LinkedHashMap是一種有序的HashMap,查詢速度快,便於添加刪除操作。( T )
- 基本數據類型的值可以被直接存儲在Vector對象中。( F )
- Dictionary建立了關鍵字和值的映射,只要提供一個關鍵字,Dictionary就會返回一個相應的值。( T )
- 泛型是JavaSE1.7的新特性,泛型的本質是參數化類型,也就是說所操作的數據類型被指定為一個參數。Java語言引入泛型的好處是安全簡單。( F )
- Collection是專門操作集合的工具類,提供一系列靜態方法實現對各種集合操作。( F )
- Iterator接口可以遍歷任何Collection接口的實現類,可以從一個Collection中使用iterator( )方法來獲取迭代器實例。迭代器取代了Java集合框架中的Enumeration。( T )
四、 簡答題
- 集合和數組的比較
- 簡述List、Set、Collection、Map的區別和聯系。
- ArrayList和LinkedList的區別和聯系。
- HashSet采用了哈希表作為存儲結構,請說明哈希表的特點
- Vector和ArrayList的區別和聯系。
- 請你簡述HashMap和Hashtable的區別?
五、 編碼題
1.使用List和Map存放多個圖書信息,遍歷並輸出。其中商品屬性:編號,名稱,單價,出版社;使用商品編號作為Map中的key。
1 public class Book 2 { 3 public int id; 4 public String name; 5 public double price; 6 public String press; 7 public Book() 8 { 9 super(); 10 } 11 public Book(int id, String name, double price, String press) 12 { 13 super(); 14 this.id = id; 15 this.name = name; 16 this.price = price; 17 this.press = press; 18 } 19 public int getId() 20 { 21 return id; 22 } 23 public void setId(int id) 24 { 25 this.id = id; 26 } 27 public String getName() 28 { 29 return name; 30 } 31 public void setName(String name) 32 { 33 this.name = name; 34 } 35 public double getPrice() 36 { 37 return price; 38 } 39 public void setPrice(double price) 40 { 41 this.price = price; 42 } 43 public String getPress() 44 { 45 return press; 46 } 47 public void setPress(String press) 48 { 49 this.press = press; 50 } 51 @Override 52 public String toString() 53 { 54 return "Book [id=" + id + ", name=" + name + ", press=" + press 55 + ", price=" + price + "]"; 56 } 57 } 58 public class TestListMap 59 { 60 public static void main(String[] args) 61 { 62 Book b1 = new Book(1000, "b1", 30.5, "bjsxt"); 63 Book b1_1 = new Book(1000, "b1", 30, "bjsxt"); 64 Book b2 = new Book(1000, "b2", 50, "bjsxt"); 65 Book b3 = new Book(1001, "b3", 30.5, "bjsxt"); 66 Book b4 = new Book(1002, "b4", 30.5, "bjsxt"); 67 Book b5 = new Book(1003, "b5", 50, "bjsxt1"); 68 //使用HashSet存儲圖書並遍歷
69 List<Book> bookList = new ArrayList<Book>(); 70 bookList.add(b1); 71 bookList.add(b1); 72 bookList.add(b2); 73 bookList.add(b3); 74 bookList.add(b4); 75 bookList.add(b5); 76 bookList.add(b1_1); 77 System.out.println("遍歷輸出hashset"); 78 System.out.println(bookList.size()); 79 for (Book book : bookList) 80 { 81 System.out.println(book.toString()); 82 } 83 //使用TreeSet存儲圖書並遍歷
84 Map<Integer, Book> bookMap = new HashMap<Integer, Book>(); 85 bookMap.put(b1.getId(), b1); 86 bookMap.put(b1.getId(), b1); 87 bookMap.put(b2.getId(), b2); 88 bookMap.put(b3.getId(), b3); 89 bookMap.put(b4.getId(), b4); 90 bookMap.put(b5.getId(), b5); 91 bookMap.put(b1_1.getId(), b1_1); 92 System.out.println("遍歷輸出treeset"); 93 for (Entry<Integer, Book> entry : bookMap.entrySet()) 94 { 95 System.out.println(entry.getKey() + "----------->" + entry.getValue()); 96 } 97 } 98 }
2.使用HashSet和TreeSet存儲多個商品信息,遍歷並輸出;其中商品屬性:編號,名稱,單價,出版社;要求向其中添加多個相同的商品,驗證集合中元素的唯一性。
提示:向HashSet中添加自定義類的對象信息,需要重寫hashCode和equals( )
向TreeSet中添加自定義類的對象信息,需要實現Comparable接口,指定比較規則
1 public class Book implements Comparable<Book>
2 { 3 public int id; 4 public String name; 5 public double price; 6 public String press; 7 public Book() 8 { 9 super(); 10 } 11 public Book(int id, String name, double price, String press) 12 { 13 super(); 14 this.id = id; 15 this.name = name; 16 this.price = price; 17 this.press = press; 18 } 19 public int compareTo(Book o) 20 { 21 return this.id - o.id; 22 } 23 @Override 24 public int hashCode() 25 { 26 final int prime = 31; 27 int result = 1; 28 result = prime * result + id; 29 result = prime * result + ((name == null) ? 0 : name.hashCode()); 30 result = prime * result + ((press == null) ? 0 : press.hashCode()); 31 long temp; 32 temp = Double.doubleToLongBits(price); 33 result = prime * result + (int) (temp ^ (temp >>> 32)); 34 return result; 35 } 36 @Override 37 public boolean equals(Object obj) 38 { 39 if (this == obj) 40 { 41 return true; 42 } 43 if (obj == null) 44 { 45 return false; 46 } 47 if (getClass() != obj.getClass()) 48 { 49 return false; 50 } 51 Book other = (Book) obj; 52 if (id != other.id) 53 { 54 return false; 55 } 56 if (name == null) 57 { 58 if (other.name != null) 59 { 60 return false; 61 } 62 } else if (!name.equals(other.name)) 63 { 64 return false; 65 } 66 if (press == null) 67 { 68 if (other.press != null) 69 { 70 return false; 71 } 72 } else if (!press.equals(other.press)) 73 { 74 return false; 75 } 76 if (Double.doubleToLongBits(price) != Double 77 .doubleToLongBits(other.price)) 78 { 79 return false; 80 } 81 return true; 82 } 83 @Override 84 public String toString() 85 { 86 return "Book [id=" + id + ", name=" + name + ", press=" + press 87 + ", price=" + price + "]"; 88 } 89 } 90 public class TestSet 91 { 92 public static void main(String[] args) 93 { 94 Book b1 = new Book(1000, "b1", 30.5, "bjsxt"); 95 Book b1_1 = new Book(1000, "b1", 30, "bjsxt"); 96 Book b2 = new Book(1000, "b2", 50, "bjsxt"); 97 Book b3 = new Book(1001, "b3", 30.5, "bjsxt"); 98 Book b4 = new Book(1002, "b4", 30.5, "bjsxt"); 99 Book b5 = new Book(1003, "b5", 50, "bjsxt1"); 100 //使用HashSet存儲圖書並遍歷
101 Set<Book> hashSet = new HashSet<Book>(); 102 hashSet.add(b1); 103 hashSet.add(b1); 104 hashSet.add(b2); 105 hashSet.add(b3); 106 hashSet.add(b4); 107 hashSet.add(b5); 108 hashSet.add(b1_1); 109 System.out.println("遍歷輸出hashset"); 110 System.out.println(hashSet.size()); 111 for (Book book : hashSet) 112 { 113 System.out.println(book.toString()); 114 } 115 //使用TreeSet存儲圖書並遍歷
116 Set<Book> treeSet = new TreeSet<Book>(); 117 treeSet.add(b1); 118 treeSet.add(b1); 119 treeSet.add(b2); 120 treeSet.add(b3); 121 treeSet.add(b4); 122 treeSet.add(b5); 123 treeSet.add(b1_1); 124 System.out.println("遍歷輸出treeset"); 125 for (Book book : treeSet) 126 { 127 System.out.println(book.toString()); 128 } 129 } 130 }
3.實現List和Map數據的轉換。具體要求如下:
功能1:定義方法public void listToMap( ){ }將List中Student元素封裝到Map中
1) 使用構造方法Student(int id,String name,int age,String sex )創建多個學生信息並加入List
2) 遍歷List,輸出每個Student信息
3) 將List中數據放入Map,使用Student的id屬性作為key,使用Student對象信息作為value
4) 遍歷Map,輸出每個Entry的key和value
功能2:定義方法public void mapToList( ){ }將Map中Student映射信息封裝到List
1) 創建實體類StudentEntry,可以存儲Map中每個Entry的信息
2) 使用構造方法Student(int id,String name,int age,String sex )創建多個學生信息,並使用Student的id屬性作為key,存入Map
3) 創建List對象,每個元素類型是StudentEntry
4) 將Map中每個Entry信息放入List對象
1 public class TestListToMap 2 { 3 public void listToMap() 4 { 5 //1.創建多個學生信息
6 Student stu1 = new Student(110, "小明", 23, 98.0); 7 Student stu2 = new Student(111, "大剛", 21, 80.5); 8 Student stu3 = new Student(112, "小白", 12, 93.0); 9 //2.加入List
10 List<Student> list = new ArrayList<Student>(); 11 list.add(stu1); 12 list.add(stu2); 13 list.add(stu3); 14 //3.遍歷List,輸出每個Student信息
15 for (Student stu : list) 16 { 17 System.out.println(stu); 18 } 19 //4.將List中數據放入Map,使用Student的id屬性作為key Map<Integer, Student> map = new HashMap<Integer, Student>();
20 Iterator<Student> it = list.iterator(); 21 while (it.hasNext()) 22 { 23 Student stu = it.next(); 24 map.put(stu.getId(), stu); 25 } 26 //5.遍歷Map,輸出每個Entry的key和value
27 Set<Entry<Integer, Student>> entrySet = map.entrySet(); 28 for (Entry<Integer, Student> entry : entrySet) 29 { 30 System.out.println(entry.getKey() + "---->" + entry.getValue()); 31 } 32 } 33 } 34 public class StudentEntry 35 { 36 private int key;//關鍵字
37 private Student stu;//學生
38 public int getKey() 39 { 40 return key; 41 } 42 public void setKey(int key) 43 { 44 this.key = key; 45 } 46 public Student getStu() 47 { 48 return stu; 49 } 50 public void setStu(Student stu) 51 { 52 this.stu = stu; 53 } 54 } 55 public class TestMapToList 56 { 57 public void mapToList() 58 { 59 //1.創建多個學生信息
60 Student stu1 = new Student(110, "小明", 23, 98.0); 61 Student stu2 = new Student(111, "大剛", 21, 80.5); 62 Student stu3 = new Student(112, "小白", 12, 93.0); 63 //2.使用Student的id屬性作為key,存入Map
64 Map<Integer, Student> map = new HashMap<Integer, Student>(); 65 map.put(stu1.getId(), stu1); 66 map.put(stu2.getId(), stu2); 67 map.put(stu2.getId(), stu3); 68 //3.創建List對象,每個元素類型是StudentEntry
69 List<StudentEntry> list = new ArrayList<StudentEntry>(); 70 //4.將Map對象轉化為List集合
71 for (Entry<Integer, Student> entry : map.entrySet()) 72 { 73 StudentEntry studentEntry = new StudentEntry(); 74 // 將map中的一個映射關系,封裝為一個studentEntry對象
75 studentEntry.setKey(entry.getKey()); 76 studentEntry.setStu(entry.getValue()); 77 // 將studentEntry對象List集合
78 list.add(studentEntry); 79 } 80 //5.遍歷Map
81 for (StudentEntry se : list) 82 { 83 System.out.println(se.getKey() + "\t" + se.getStu()); 84 } 85 } 86 }
六、 可選題
1.假如有以下email數據“aa@sohu.com,bb@163.com,cc@sina.com,..”現需要把email中的用戶部分和郵件地址部分分離,分離后以鍵值對應的方式放入HashMap?
1 public class EmailSplit 2 { 3 public static void main(String[] args) 4 { 5 String str = "aa@sohu.com,bb@163.com,cc@sina.com"; 6 //得到每一個email
7 String strs[] = str.split(","); 8 //存放分離后email的信息
9 Map<String, String> emailMap = new HashMap<String, String>(); 10 for (String email : strs) 11 { 12 String temp[] = email.split("@"); 13 //分割email存入map
14 emailMap.put(temp[0], temp[1]); 15 } 16 System.out.println(emailMap.toString()); 17 } 18 }
2.由控制台按照固定格式輸入學生信息,包括學號,姓名,年齡信息,當輸入的內容為exit退出;將輸入的學生信息分別封裝到一個Student對象中,再將每個Student對象加入到一個集合中,要求集合中的元素按照年齡大小正序排序;最后遍歷集合,將集合中學生信息寫入到記事本,每個學生數據占單獨一行。
推薦步驟:
a) 創建Student類,並指定按照年齡正序排列
b) 通過控制台輸入多個不同Student信息。格式規定為:編號#姓名#年齡
c) 取出字符串中相應信息放入Student對象,並將Student加入到集合中
d) 遍歷集合的過程中將學生的信息輸入到記事本
難點:
e) 如何指定學生按照年齡正序排列
f) 如果從字符串“編號#姓名#年齡”中提取學生信息
g) 放入哪種集合后可以保證學生按照年齡大小正序排列
h) 如何將集合中學生信息寫入記事本,每個學生數據占單獨一行
1 public class Student implements Comparable<Student>
2 { 3 private Integer num; 4 private String name; 5 private Integer age; 6 //省略getter和setter方法 7 //省略構造方法
8 public int compareTo(Student stu) 9 { 10 return this.age - stu.age; 11 } 12 public String toString() 13 { 14 return "Student [age=" + age + ", name=" + name 15 + ", num=" + num + "]"; 16 } 17 } 18 public class Test 19 { 20 public static void main(String[] args) 21 { 22 //保存輸入信息到set中
23 Set<Student> stuSet = saveStudentInfo(); 24 //遍歷set
25 Iterator<Student> it = stuSet.iterator(); 26 while (it.hasNext()) 27 { 28 String info = it.next().toString(); 29 System.out.println(info); 30 } 31 } 32 private static Set<Student> saveStudentInfo() 33 { 34 Scanner input = new Scanner(System.in); 35 // 保存學生信息的TreeSet集合對象
36 Set<Student> stuSet = new TreeSet<Student>(); 37 while (true) 38 { 39 // 輸入提示
40 System.out.println("請輸入學生信息:(學號#姓名#年齡)"); 41 String inputData = input.nextLine(); 42 // 判斷是否退出 inputData.equals("exit")
43 if ("exit".equals(inputData)) 44 { 45 break; 46 } 47 // 將用戶輸入的學生信息分割為String[]
48 String[] info = inputData.split("#"); 49 // 將輸入信息封裝到Student對象中
50 Student stu 51 = new Student(Integer.parseInt(info[0]), info[1], 52 Integer.parseInt(info[2])); 53 // 將學生對象加入集合
54 stuSet.add(stu); 55 } 56 return stuSet; 57 } 58 }