(一)学习总结
1.思维导图:
2.当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。可使用printStackTrace 和getMessage方法了解异常发生的情况。阅读下面的程序,说明printStackTrace方法和getMessage 方法的输出结果分别是什么?并分析异常的传播过程。
答:getMessage方法输出的是:“Exception thrown in method3”
printStackTrace方法输出的是:
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)
调用method1的时候抛出了异常,再调用method2是在抛出异常,再调用method3抛出“Exception thrown in method3”,在main函数中捕捉异常,用printStackTrace方法输出
3.阅读下面程序,分析程序的运行结果,解释产生错误的原因,如果删除的是books集合的最后一个对象,运行的结果又是什么?你能对此作出解释吗?如果在遍历时非要删除集合中的元素,应如何实现?
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
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);
}
}
结果:
原始元素之后:[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)
如果删除最后一个:
原始元素之后:[One book, Two book, Three book]
One book
Two book
Three 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)
错误原因:删除元素后迭代器的大小发生了变化,所以输出时有问题,要想遍历时删除元素一定要用迭代器的remove进行删除。
while(it.hasNext())
{
String book = (String)it.next();
System.out.println(book);
if (book.equals("One book"))
{
it.remove();
}
}
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);
}
}
原因:因为用new分配的内存,所以hashcode中认为是不同的两个元素,要想不能有重复的元素,就要重写hashcode和equals方法
public boolean equals(Object obj){
if(this==obj){
return true;
}
if(!(obj instanceof Student)){
return false;
}
Student s=(Student) obj;
if(this.name.equals(s.name)&&this.id.equals(s.id)){
return true;
}else{
return false;
}
}
public int hashCode(){
final int num=2;
int finall;
if(!id.equals(null)){
return finall=num+id.hashCode()+name.hashCode();
}
else
{return 0;
}
}
}
(二)实验总结
实验内容:
1.模拟KTV点歌系统
分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现以下功能:
(1)显示歌曲列表
(2)添加歌曲到列表
(3)删除歌曲
(4)将歌曲置顶
(5)将歌曲前移一位
(6)退出
程序设计思路:创建一个KTV类,然后分别建立ArrayList和LinkedList列表进行操作。
实验无问题。
2.模拟微博用户注册
用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,13、15、17、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,如果不是则注册成功,否则注册失败。
提示:
(1)设计一个用户类存储用户注册信息
(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。
(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。
程序设计思路:创建3个类,其中测试类继承注册类,在测试类里面测试注册形式是否符合要求。
问题1:用正则表达式判断邮箱格式,没有实例化
解决方案:放在应用的方法里即可
(三)代码托管
git@git.oschina.net:wangyikai1996/shiyan6.git