1.用思惟导图对本周的学习内容进行总结。
java
2.当程序中出现异常时,JVM会依据方法调用顺序依次查找有关的错误处理程序。可以使用printStackTrace 和getMessage方法了解异常发生的状况。阅读下面的程序,说明printStackTrace方法和getMessage 方法的输出结果分别是什么?并分析异常的传播过程。git
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方法的输出结果是:正则表达式
Exception thrown in method3学习
将try语句中写可能出现的异常代码catch来捕获异常,若try语句中产生了异常,则程序会自动跳到catch找到匹配的类型进行相应的处理this
3.阅读下面程序,分析程序的运行结果,解释产生错误的缘由,若是删除的是books集合的最后一个对象,运行的结果又是什么?你能对此做出解释吗?若是在遍历时非要删除集合中的元素,应如何实现?.net
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); } }
运行结果为:设计
原始元素以后:[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)
缘由:不能在ArrayList遍历的时候删掉其中的元素,使大小发生改变,iterator发生异常code
删除最后一个对象的运行结果为:对象
原始元素以后:[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)
缘由:在遍历输出时,大小没有发生变化,而在删除的时候大小发生了变化,产生异常。blog
修改后的代码为:
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("Three 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); }
缘由:在实例化中,他们都是新建的内存空间,地址不一样,因此判断为 不相同的,须要重写equals()方法和hashCode()方法
修改后的代码为:
import java.util.*; class Student { String id; String name; public Student(String id, String name) { this.id = id; this.name = name; } 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; } 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); } }
1.模拟KTV点歌系统
程序设计思路:设计一个Music类,定义属性歌名及歌手,分别定义方法显示歌曲列表,添加歌曲。删除歌曲,指定歌曲,将歌曲前移一位。分别建一个test类用LinkedList和ArrayList集合实现点歌系统,在test类中用while语句来循环,用switch()case来使用户选择要进行的操做
实验问题分析:
问题1:
public static List
for(int j=0;j<list.size();j++){
if(j==i){
String song=list.get(i).getName();
String songer=list.get(i).getSonger();
list.remove(i);
list.add(0,new Music(song,songer));
}
}
return list;
}
缘由:没有定义get及set方法
解决方案:在获得歌名时,不知应如何查找,问同窗同窗说不用setget方法,最后本身试了下定义那两个方法,而后能够出来。
问题2:
case 2:
System.out.println("请输入要添加的歌曲");
String name=in.next();
System.out.println("请输入要添加的歌曲的歌手");
String songer=in.next();
Music.add(list,name,songer);
break;
缘由:不知道应该怎样调用方法
解决方案:经过问同窗得知应该用类名来进行调用,而后试了一下调用成功
2.模拟微博用户注册
问题1:
public static int checkname(Set<User> user,String cname){ int i=0; Iterator<User> cuser=user.iterator(); while(cuser.hasNext()) { if(cuser.next().getName().equals(cname)){ System.out.println("用户名已存在"); i=1; } else{ System.out.println("用户名注册成功"); } } return i; }
缘由:刚开始定义方法为void型的
解决方案:初始为void ,没有返回,没法使从新输入,将类型改成int型是返回值来判断是否从新输入
问题2:
String pat = "^[0-9]{5,10}+@qq.+(com|cn|edu|net|gov|org)$";
缘由:不知该怎样用正则表达式来写邮箱的验证
解决方案:经过看书以及从网上查阅资料用[0-9]{5,10}来表示能够为5到10个数字。