getMessage输出结果:
Exception thrown in method3printStackTrace方法的输出结果:
java.lang.Exception: Exception thrown in method3 at test.PrintExceptionStack.method3(PrintExceptionStack.java:22) at test.PrintExceptionStack.method2(PrintExceptionStack.java:18) at test.PrintExceptionStack.method1(PrintExceptionStack.java:14) at test.PrintExceptionStack.main(PrintExceptionStack.java:6)在java程序中产生异常的时候会产生一个异常类的实例化对象,在try语句中对此异常对象进行捕捉,然后与catch语句中的各个异常类型进行匹配,如果匹配成功则执行catch语句中的代码。 使用getMessage()会获得异常名称,printStackTrace()会输出详细的异常名称,出错位置。
原因:在迭代时,如果调用集合对象的remove()方法删除对象,会出现运行错误。 解决:使用迭代器本身的删除方法 将
if (book.equals("One book")) { it.remove(); }改为
if (book.equals("Three book")){ it.remove(); }在set.add方法中使用匿名对象,虽然内容相同,但是由于是匿名对象,内容虽然相同但引用的地址不相同,故在HashSet接口中仍能实现。
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; } }分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现以下功能: (1)显示歌曲列表 (2)添加歌曲到列表 (3)删除歌曲 (4)将歌曲置顶 (5)将歌曲前移一位 (6)退出 题目扩展:歌曲包括曲名、演唱者。增加排序显示歌曲列表功能。
内容:用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,13、15、17、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,如果不是则注册成功,否则注册失败。
https://git.oschina.net/hebau_cs15/java-cs01fs.git
转载于:https://www.cnblogs.com/fsaixuexi/p/6809052.html
相关资源:声明结构类型的控制台应用程序