《Java技術》第六次作業
(一)學習總結
1.用思維導圖對本周的學習內容進行總結。
2.當程序中出現異常時,JVM會依據方法調用順序依次查找有關的錯誤處理程序。可使用printStackTrace 和getMessage方法了解異常發生的情況。閱讀下面的程序,說明printStackTrace方法和getMessage 方法的輸出結果分別是什么?并分析異常的傳播過程。
public class PrintExceptionStack {public static void main( String args[] ){try {method1();} catch ( Exception e ) {System.err.println( e.getMessage() + "\n" );e.printStackTrace();}}public static void method1() throws Exception{method2();}public static void method2() throws Exception{method3();}public static void method3() throws Exception{throw new Exception( "Exception thrown in method3" );}}
- getMessage 方法的輸出結果是:
Exception thrown in method3
- 而printStackTrace方法輸出的是
java.lang.Exception: Exception thrown in method3at PrintExceptionStack.method3(PrintExceptionStack.java:22)at PrintExceptionStack.method2(PrintExceptionStack.java:18)at PrintExceptionStack.method1(PrintExceptionStack.java:14)at PrintExceptionStack.main(PrintExceptionStack.java:6)
在java程序中,一旦產生異常,則首先會產生一個異常類的實例化對象,在try語句中對此異常對象進行捕捉,然后與catch語句中的各個異常類型進行匹配,如果匹配成功則執行catch語句中的代碼。
在 method3()中拋出了一個異常,那么在method2(),method1()中相繼調用該方法時也要拋出這個異常,最后在主方法中進行捕獲處理,并輸出異常信息。
3.閱讀下面程序,分析程序的運行結果,解釋產生錯誤的原因,如果刪除的是books集合的最后一個對象,運行的結果又是什么?你能對此作出解釋嗎?如果在遍歷時非要刪除集合中的元素,應如何實現?
import java.util.*;public class Test{public static void main(String[] args) {Collection<String> books = new ArrayList<String>();books.add("One book");books.add("Two book");books.add("Three book");System.out.println("原始元素之后:"+books);Iterator<String> it = books.iterator();while(it.hasNext()){String book = (String)it.next();System.out.println(book);if (book.equals("One book")){books.remove(book);}}System.out.println("移除元素之后:"+books);}}
- 運行結果:
原始元素之后:[One book, Two book, Three book]
One book
Exception in thread "main" java.util.ConcurrentModificationExceptionat java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)at java.util.ArrayList$Itr.next(ArrayList.java:791)at Test.main(Test.java:14)
- 刪除最后一個的運行結果:
原始元素之后:[One book, Two book, Three book]
One book
Exception in thread "main" Two book
Three book
java.util.ConcurrentModificationExceptionat java.util.ArrayList$Itr.checkForComodification(ArrayList.java:819)at java.util.ArrayList$Itr.next(ArrayList.java:791)at Test.main(Test.java:14)
Iterator 被創建之后會建立一個指向原來對象的單鏈索引表,當我刪除一個元素后,原來的對象數量發生變化,這個索引表的內容不會同步改變,所以當索引指針往后移動的時候就找不到要迭代的對象,Iterator 會馬上拋出 java.util.ConcurrentModificationException 異常。所以 Iterator 在工作的時候是不允許被迭代的對象被改變的。
- 如果一定要刪除,可以使用 Iterator 本身的方法 remove() 來刪除對象, Iterator.remove() 方法會在刪除當前迭代對象的同時維護索引的一致性。
- 修改為即可成功輸出:
while(it.hasNext()){String book = (String)it.next();System.out.println(book);if (book.equals("One book")){it.remove();}
}
4.HashSet存儲的元素是不可重復的。運行下面的程序,分析為什么存入了相同的學生信息?如果要去掉重復元素,應該如何修改程序。
import java.util.*;class Student {String id; String name;public Student(String id, String name) {this.id = id;this.name = name;}public String toString() {return "Student id=" + id + ", name=" + name ;}}public class Test{public static void main(String[] args) {HashSet<Student> set = new HashSet<Student>();set.add(new Student("1","Jack"));set.add(new Student("2","Rose"));set.add(new Student("2","Rose"));System.out.println(set); }}
- 要想實現Hashset的不可重復元素的方法,就必須在類中重寫hashcode和equals方法。在student中添加如下方法:
public int hashCode() {final int prime = 31;int result = 1;result = prime * result + ((id == null) ? 0 : id.hashCode());result = prime * result + ((name == null) ? 0 : name.hashCode());return result;}public boolean equals(Object obj) {if (this == obj)return true;if (obj == null)return false;if (getClass() != obj.getClass())return false;Student other = (Student) obj;if (id == null) {if (other.id != null)return false;} else if (!id.equals(other.id))return false;if (name == null) {if (other.name != null)return false;} else if (!name.equals(other.name))return false;return true;}
(二)實驗總結
實驗內容:
1.模擬KTV點歌系統
分別用LinkedList和ArrayList集合,實現一個模擬KTV點歌系統的程序。實現以下功能:
(1)顯示歌曲列表
(2)添加歌曲到列表
(3)刪除歌曲
(4)將歌曲置頂
(5)將歌曲前移一位
(6)退出
- 程序設計思路:創建一個String類的LinkedList或ArrayList,先添加數據,對歌曲的置頂和前移實際上是可以通過對元素的按位置添加和刪除來解決。
- 問題:置頂歌曲錯誤
原因:用戶給的位置實際上與元素在list中的位置相差1,從0開始的
解決方案:
String firstsong = Song.get(n-1);
Song.remove(n-1);
Song.addFirst(firstsong);
2.模擬微博用戶注冊
用HashSet實現一個模擬微博用戶注冊的程序。用戶輸入用戶名、密碼、確認密碼、生日(格式yyyy-mm-dd)、手機號碼(11位,13、15、17、18開頭)、郵箱信息進行微博的注冊。要求對用戶輸入的信息進行驗證,輸入信息正確后,驗證是否重復注冊,如果不是則注冊成功,否則注冊失敗。
提示:
(1)設計一個用戶類存儲用戶注冊信息
(2)設計一個校驗信息類,定義校驗方法完成對輸入信息的校驗。學習使用正則表達式完成對生日、手機號碼和郵箱的驗證。
(3)設計一個用戶注冊類模擬注冊過程。用HashSet存儲用戶數據列表,定義一個initData()方法添加初始用戶信息。在main方法中完成用戶注冊功能。
- 程序設計思路:設計一個用戶類中存儲相關信息,設計一個校驗類,對輸入的信息進行正則表達式的檢查和對已有的信息的重復性檢查,設計一個test類供用戶輸入數據,并在用戶類中重寫hashcode和equals方法,相同用戶名無法插入,在最后用戶輸入完信息后,輸出成功或失敗信息。
(三)代碼托管
- 碼云commit歷史截圖