Java技术第六次作业
《Java技术》第六次作业
(一)学习总结
1.用思维导图对本周的学习内容进行总结。
参考资料: XMind
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" );
}
}`
-
getMessage输出结果
java.lang.Exception: Exception thrown in method3
-
printStackTrace输出结果
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)
getMessage()只会获得具体的异常名称.
printStackTrace()会打出详细异常,异常名称,出错位置,便于调试用。
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);
}
}
`
Iterator直接使用remove()方法直接删除,上面的删除方法在使用Iterator输出时集合对象调用了自身的删除方法,运行时就会出现错误
- 如果删除最后一个对象,运行结果
原始元素之后:[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.Test.main(Test.java:19)
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);
}
}
`
-
运行结果
[Student id=2, name=Rose, Student id=1, name=Jack, Student id=2, name=Rose]
-
重复元素的原因:
因为添加的是Student的一个匿名对象,匿名对象内容虽然相同,但是引用不同,所以有重复元素出现 -
解决方法:
重写hashcode()和equals()方法。
5.其他需要总结的内容。Collection接口:
Collection接口是构造集合框架的基础。它声明所有集合类都将拥有的核心方法。
List接口:
List接口是包含有序元素的一种Collection子接口,其中的元素必须按序存放。元素之间的顺序关系可以由插入的时间先后决定,也可以由元素值的大小决定。List接口使用类似于数组下标的索引的概念表示元素在List 中的位置。用户能够使用索引来访问List 中的元素。索引从0开始。
List接口定义:
public interface Listextends Collection
(二)实验总结
实验内容:
1.模拟KTV点歌系统
分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现以下功能:
(1)显示歌曲列表
(2)添加歌曲到列表
(3)删除歌曲
(4)将歌曲置顶
(5)将歌曲前移一位
(6)退出
-
程序设计思路:
创建一个Song类包括曲名、演唱者,Test测试类定义菜单所具有的方法在main中调用方法进行测试。 -
问题1:歌曲的前移实现不了
原因:元素的删除放入错误
解决方案:
`
public static List<Song> qianyi(List<Song> song,String str){
int w1 = song.lastIndexOf(new Song(str));// 得到输入元素的位置
int w2 = song.lastIndexOf(new Song(str)) - 1;// 得到输入元素前一个的位置
song.remove(w1);// 删除输入的元素
song.add(w2, new Song(str));// 输入的放入前一个的位置
System.out.println("歌曲列表");
return song;
`题目扩展:歌曲包括曲名、演唱者。增加排序显示歌曲列表功能。** 2.模拟微博用户注册**
用HashSet实现一个模拟微博用户注册的程序。用户输入用户名、密码、确认密码、生日(格式yyyy-mm-dd)、手机号码(11位,13、15、17、18开头)、邮箱信息进行微博的注册。要求对用户输入的信息进行验证,输入信息正确后,验证是否重复注册,如果不是则注册成功,否则注册失败。
提示:
(1)设计一个用户类存储用户注册信息
(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。
(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。
- 程序设计思路
设计一个users类,存储用户的用户名、密码、生日、手机号、邮箱等属性;
设计一个check 检校信息类,判断用户输入信息是否合法;
设计一个用户注册类,执行用户注册过程:用户输入各项信息后,判断信息是否合法、是否重复,如果信息合格,则注册成功。
问题1:验证手机号,邮箱号的正确
原因:不知该怎样用正则表达式来写邮箱,手机号的验证
解决方案:上午查例子。
public int checkemail(Set<User> user, String str) {
int i = 0;
Iterator<User> iterator = user.iterator();
if (!str.matches("^\\w+@[a-zA-Z0-9]+([.][a-zA-Z0-9]+)*$")) {
i = 1;
System.out.println("邮箱格式不合法");
} else {
while (iterator.hasNext()) {
if (iterator.next().getEmail().equals(str)) {
i = 1;
}
(三)代码托管
- 码云commit历史截图