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()的输出结果:java
Exception thrown in method3 java.lang.Exception: Exception thrown in method3 at PrintExceptionStack.method3(PrintExceptionStack.java:21) at PrintExceptionStack.method2(PrintExceptionStack.java:17) at PrintExceptionStack.method1(PrintExceptionStack.java:13) at PrintExceptionStack.main(PrintExceptionStack.java:5)
getMessage()的输出结果:git
Exception thrown in method3
异常传播过程:程序检测到一个错误时会抛出一个异常对象,异常处理代码会捕获并处理这个错误。catch语句块中的代码是用来处理错误,当异常发生时,程序控制流程由try语句块跳转到catch语句块,进行处理。正则表达式
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
原始元素以后:[One book, Two book, Three book] One book Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at Test.main(Test.java:14)
删除的是books集合的最后一个对象,运行的结果:学习
Exception in thread "main" java.util.ConcurrentModificationException at java.util.ArrayList$Itr.checkForComodification(Unknown Source) at java.util.ArrayList$Itr.next(Unknown Source) at test.main(test.java:14) 原始元素以后:[One book, Two book, Three book] One book Two book Three book
缘由:
在删除时调用了remove()方法,删除元素后集合的大小发生了变化,因此输出时有问题。测试
在遍历时非要删除集合中的元素:this
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")) { 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); } }
运行结果为:.net
[Student id=2, name=Rose, Student id=2, name=Rose, Student id=1, name=Jack]
分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现如下功能:
(1)显示歌曲列表
(2)添加歌曲到列表
(3)删除歌曲
(4)将歌曲置顶
(5)将歌曲前移一位
(6)退出
题目扩展:歌曲包括曲名、演唱者。增长排序显示歌曲列表功能。
程序设计思路:分别用linkedList和ArrayList集合来完成添加删除置顶移动的测试类设计
用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,1三、1五、1七、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,若是不是则注册成功,不然注册失败。
提示:
(1)设计一个用户类存储用户注册信息
(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。
(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。3d
连接:
http://git.oschina.net/hebau_cs15/Java-CS02lc/tree/master