Java第六次作业--异常处理和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" );
}
}
- 运行结果
- printStackTrace方法输出结果
java.lang.Exception: Exception thrown in method3
- getMessage 方法的输出结果
at test.Test.method3(Test.java:22)
at test.Test.method2(Test.java:18)
at test.Test.method1(Test.java:14)
at test.Test.main(Test.java:6)
- 异常的传播
在可能发生异常的地方使用try{}将代码放入其中, catch(){}语块捕获到所发生的异常,并进行相应的处理。
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);
}
}
- 运行结果
- 删除的是books集合的最后一个对象
- 原因
在迭代时,如果调用集合对象的remove()方法删除对象,会出现运行错误 如果要删除,必须调用迭代器本身的remove
用remove()方法将迭代器所返回的元素删除
if (book.equals("One 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);
}
}
- 运行结果
- 原因
添加学生信息时分别进行了实例化,分别实例化后两个对象的HashCode值不同,所以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;
}
(二)实验总结
1.模拟KTV点歌系统
- 程序设计思路:利用ArrayList类和Linked类 分别进行了添加、删除、置顶等操作;
- 问题:前移操作不太会
- 解决方案:
2.模拟微博用户注册
- 程序设计思路:
(1)设计一个用户类存储用户注册信息
(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。
(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。- 问题:
新学的内容如set类 HashSet类不太理解,和以前学的内容也联系不起来,正太表达式也不太会运用,以前巩固不深,写起来比较困难 没能写出完整的程序来- 解决方案:以后多多练习.........







浙公网安备 33010602011771号