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
printStackTrace方法的输出结果:git
缘由:printStackTrace:打印方法调用堆栈,String getMessage():返回此throwable详细信息字符串。
异常传播过程:
一个Throwable类的对象都有一个getMessage方法,method3()产生异常并抛出,method2() 调用method3()产生异常并抛出它返回一个字串,这个字串是在Exception构造函数中传入的,一般让这一字串包含特定异常的相关信息。正则表达式
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); } }
运行结果和错误缘由:再进行删除时,会使迭代器的大小发生改变,而输出没有发生变化,形成异常。编程
解决方案: 使用迭代器自己的删除方法jvm
正确代码以下:函数
import java.util.*; public class Test13 { 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); } }
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()方法。以下:this
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.正则表达式能够方便的对数据进行匹配,能够执行更加复杂的字符串验证、拆分、替换功能。
2.若是要想在程序中应用正则表达式则必须依靠Pattern类与Matcher类,这两个类都在java.util.regex包中定义。Pattern类的主要做用是进行正则规范的编写,而Matcher类主要是执行规范,验证一个字符串是否符合其规范。
3. 几个经常使用规范格式:编码
日期(yyyy-MM-dd):“\\d{4}-\\d{2}-\\d{2}” Email: “\\w+@\\w+.(com|com.cn|cn|net|gov|edu|org)”
分别用LinkedList和ArrayList集合,实现一个模拟KTV点歌系统的程序。实现如下功能:
(1)显示歌曲列表
(2)添加歌曲到列表
(3)删除歌曲
(4)将歌曲置顶
(5)将歌曲前移一位
(6)退出
题目扩展:歌曲包括曲名、演唱者。增长排序显示歌曲列表功能。
(1)程序设计思路:
(1)设计一个用户类存储用户注册信息
(2)设计一个校验信息类,定义校验方法完成对输入信息的校验。学习使用正则表达式完成对生日、手机号码和邮箱的验证。
(3)设计一个用户注册类模拟注册过程。用HashSet存储用户数据列表,定义一个initData()方法添加初始用户信息。在main方法中完成用户注册功能。
(4)设计一个users类,存储用户的用户名、密码、生日、手机号、邮箱等属性; 使用正则表达式方法写 check 检验函数,看用户输入的信息是否相符。最后设计一个测试注册类,进行判断。
注册成功,用户信息存入HashSet类集,要定义一个intData()方法,以下:
在写检查输入信息手机号,生日,邮箱,格式是否有误时,应用正则表达式判断,翻之前课件,最后才写好,关键代码:
String birthCheck="\\d{4}-\\d{2}-\\d{2}"; String phoneCheck="^(18|15|17|13)\\d{9}$"; String emailCheck="\\w+@\\w+.(com|cn|edu|net|gov|org)"; Pattern p2 = Pattern.compile(emailCheck); Matcher m2 =p2.matcher(email);
代码行数(新增/累积) | 学习时间(新增/累积) | 本周学习内容 | |
---|---|---|---|
目标 | 5000行 | 300小时 | |
第2-4周 | 200/200 | 20/20 | java基本语法 |
第5周 | 150/350 | 20/40 | 学习了构造函数,各类基本调用方法。 |
第6周 | 150/350 | 30/70 | 运行 |
第7周 | 500/850 | 15/85 | 面向对象编程的知识点(封装、继承和多态) |