1.用思惟導圖對本週的學習內容進行總結。
java
2.當程序中出現異常時,JVM會依據方法調用順序依次查找有關的錯誤處理程序。可以使用printStackTrace 和getMessage方法瞭解異常發生的狀況。閱讀下面的程序,說明printStackTrace方法和getMessage 方法的輸出結果分別是什麼?並分析異常的傳播過程。git
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" ); } }
printStackTrace方法的輸出結果是
正則表達式
getMessage方法的輸出結果是
設計模式
析異常的傳播過程:先是throw new Exception( "Exception thrown in method3" ),而後是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); } }
運行結果
this
產生錯誤的緣由:集合自己的內容被破壞掉,因此迭代器出現錯誤,會中止輸出。.net
運行結果
設計
解釋:運行時先取出集合中元素進行輸出,最後取出的是Three book,因此前兩個元素也能輸出。
修改以下code
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 ("One book".equals(book)) { it.remove(); } } System.out.println("移除元素以後:"+books); } }
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); }
}
緣由:沒進行比較元素的地址是否相等。
修改以下
import java.util.*; class Student { String id; String name; public Student(String id, String name) { this.id = id; this.name = name; } public boolean equals(Object obj){ if(this==obj){ return true; } if(!(obj instanceof Student)){ return false; } Student p=(Student) obj; if(this.id.equals(p.id)&&this.name.equals(p.name)){ return true; } else{ return false; } } public int hashCode(){ return this.id.hashCode()*this.name.hashCode(); } 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); } }
1.點歌系統
代碼行數(新增/累積) | 學習時間(新增/累積) | 本週學習內容 | |
---|---|---|---|
目標 | 5000行 | 300小時 | |
第2-4周 | 100/100 | 20/20 | 學習了數組和方法 |
第5周 | 200/300 | 30/50 | 學習了String類和StringBuffer類 |
第6周 | 800/1100 | 40/90 | 學習了this、static關鍵字,Singleton模式 |
第八週 | 1200/1700 | 60/110 | 繼承和多態,抽象方法 |
第九周 | 1500/2000 | 70/120 | 接口、工廠設計模式、包裝類、匿名內部類、日期類、正則表達式 |
第十週 | 1900/2400 | 80/130 | 異常處理、泛型、集合 |