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方法的運行結果是異常信息:
java
printStackTrace方法的輸出結果:git
緣由:printStackTrace:打印方法調用堆棧,String getMessage():返回此throwable詳細信息字符串。
異常傳播過程:
一個Throwable類的對象都有一個getMessage方法,method3()產生異常並拋出,method2() 調用method3()產生異常並拋出它返回一個字串,這個字串是在Exception構造函數中傳入的,一般讓這一字串包含特定異常的相關信息。正則表達式
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); } }
運行結果和錯誤緣由:再進行刪除時,會使迭代器的大小發生改變,而輸出沒有發生變化,形成異常。編程
解決方案: 使用迭代器自己的刪除方法jvm
正確代碼以下:函數
import java.util.*; public class Test13 { 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("Three book")){ it.remove(); } } System.out.println("移除元素以後:" + books); } }
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); } }
程序運行結果:
學習
結果有相同數據緣由:
每個實例化的對象,對應一個哈希曼編碼,有三個,因此會有相同的值出現。測試
修改方法:
須要重寫equals()方法、hashCode()方法。以下:this
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.正則表達式能夠方便的對數據進行匹配,能夠執行更加複雜的字符串驗證、拆分、替換功能。
2.若是要想在程序中應用正則表達式則必須依靠Pattern類與Matcher類,這兩個類都在java.util.regex包中定義。Pattern類的主要做用是進行正則規範的編寫,而Matcher類主要是執行規範,驗證一個字符串是否符合其規範。
3. 幾個經常使用規範格式:編碼
日期(yyyy-MM-dd):「\\d{4}-\\d{2}-\\d{2}」 Email: 「\\w+@\\w+.(com|com.cn|cn|net|gov|edu|org)」
分別用LinkedList和ArrayList集合,實現一個模擬KTV點歌系統的程序。實現如下功能:
(1)顯示歌曲列表
(2)添加歌曲到列表
(3)刪除歌曲
(4)將歌曲置頂
(5)將歌曲前移一位
(6)退出
題目擴展:歌曲包括曲名、演唱者。增長排序顯示歌曲列表功能。
(1)程序設計思路:
(1)設計一個用戶類存儲用戶註冊信息
(2)設計一個校驗信息類,定義校驗方法完成對輸入信息的校驗。學習使用正則表達式完成對生日、手機號碼和郵箱的驗證。
(3)設計一個用戶註冊類模擬註冊過程。用HashSet存儲用戶數據列表,定義一個initData()方法添加初始用戶信息。在main方法中完成用戶註冊功能。
(4)設計一個users類,存儲用戶的用戶名、密碼、生日、手機號、郵箱等屬性; 使用正則表達式方法寫 check 檢驗函數,看用戶輸入的信息是否相符。最後設計一個測試註冊類,進行判斷。
註冊成功,用戶信息存入HashSet類集,要定義一個intData()方法,以下:
在寫檢查輸入信息手機號,生日,郵箱,格式是否有誤時,應用正則表達式判斷,翻之前課件,最後才寫好,關鍵代碼:
String birthCheck="\\d{4}-\\d{2}-\\d{2}"; String phoneCheck="^(18|15|17|13)\\d{9}$"; String emailCheck="\\w+@\\w+.(com|cn|edu|net|gov|org)"; Pattern p2 = Pattern.compile(emailCheck); Matcher m2 =p2.matcher(email);
代碼行數(新增/累積) | 學習時間(新增/累積) | 本週學習內容 | |
---|---|---|---|
目標 | 5000行 | 300小時 | |
第2-4周 | 200/200 | 20/20 | java基本語法 |
第5周 | 150/350 | 20/40 | 學習了構造函數,各類基本調用方法。 |
第6周 | 150/350 | 30/70 | 運行 |
第7周 | 500/850 | 15/85 | 面向對象編程的知識點(封裝、繼承和多態) |