20145303 《Java程序设计》第5周学习总结

教材学习内容总结

1、java中所有错误都会被打包为对象,如果愿意,可以尝试(try)捕捉(catch)代表错误的对象后做一些处理。

import java.util.*;
class Average2 {
public static void main(String[] args) {
try{
Scanner console = new Scanner(System.in);
double sum = 0;
int count = 0;
while (true){
int number = console.nextInt();
if(number ==0){
break;
}
sum += number;
count++;
}
System.out.printf("平均 %.2f%n", sum/count);
}catch (InputMismatchException ex){
System.out.println("必须输入整数");
}
}
}

正确执行结果:

错误输入结果:

import java.util.*;
public class Average3 {
public static void main(String[] args) {
Scanner console = new Scanner(System.in);
double sum = 0;
int count = 0;
while (true) {
try {
int number = console.nextInt();
if (number == 0) {
break;
}
sum += number;
count++;
} catch (InputMismatchException ex) {
System.out.printf("略过非整数输入:%s%n", console.next());
}
}
System.out.printf("平均 %.2f%n", sum/count);
}
}

结果:

2、在异常发生时,可使用try 、catch处理当时可进行的异常处理,当时环境下无法决定如何处理的部分,可以由调用方法的客户端处理,想先处理对象在抛出

import java.io.*;
import java.util.Scanner;
public class FileUtil {
public static String readFile(String name) throws FileNotFoundException{
StringBuilder text = new StringBuilder();
try{
Scanner console = new Scanner (new FileInputStream(name));
while(console.hasNext()){
text.append(console.nextLine())
.append('\n');
}
}catch(FileNotFoundException ex){
ex.printStackTrace();
throw ex;
}
return text.toString();
}
}

3、查看堆栈追踪最简单的方法,就是直接调用异常对象的printStackTrace()

public class StackTraceDemo {
public static void main(String[] args) {
try{
c();
}catch(NullPointerException ex){
ex.printStackTrace();
}
}
static void c(){
b();
}
static void b(){
a();
}
static String a(){
String text = null;
return text.toUpperCase();
}
}

4、在使用throw重抛异常时,异常的追踪堆栈起点时,仍是异常的发生根源,而不是重抛异常的地方

public class StackTraceDemo2 {
public static void main(String[] args) {
try{
c();
}catch(NullPointerException ex){
ex.printStackTrace();
}
}
static void c(){
try{
b();
}catch(NullPointerException ex) {
ex.printStackTrace();
throw ex;
}
}
static void b(){
a();
}
static String a(){
String text = null;
return text.toUpperCase();
}
}

结果:

5、fillaInStackTrace()方法会重新装填异常堆栈,将起点设为重抛异常的地方,并返回Throwable对象

public class StackTraceDemo3 {
public static void main(String[] args) {
try{
c();
}catch(NullPointerException ex){
ex.printStackTrace();
}
}
static void c(){
try{
b();
}catch(NullPointerException ex) {
ex.printStackTrace();
Throwable t = ex.fillInStackTrace();
throw(NullPointerException) t;
}
}
static void b(){
a();
}
static String a(){
String text = null;
return text.toUpperCase();
}
}

结果:

6、无论try区块中有无异常,若撰写finally区块,则finally区块一定会被执行

public class FinallyDemo {
public static void main(String[] args) {
System.out.println(test(true));
}
static int test(boolean flag){
try{
if(flag){
return 1;
}
}finally{
System.out.println("finally...");
}
}return 0;
}

7、

收集对象的行为,像是新增对象的add()方法、移除对象的remove()方法等,都是定义在java.util.Collection中,既然可以收集对象,也要能逐一取得对象,这就是java.lang.Iterable定义的行为,它定义了iterator()方法返回java.util.Iterator操作对象,可以让你逐一取得收集的对象。

import java.util.*;
import static java.lang.System.out;
public class Guest {
public static void main(String[] args) {
List names = new ArrayList();
collectNameTo(names);
out.println("访客名单:");
printUpperCase(names);
}
static void collectNameTo(List names){
Scanner console = new Scanner(System.in);
while(true){
out.print("访客名称:");
String name = console.nextLine();
if(name.equals("quit")){
break;
}
names.add(name); }
}
static void printUpperCase(List names){
for(int i = 0;i<names.size();i++){
String name=(String) names.get(i);
out.println(name.toUpperCase());
}
}
}

8、

如果对象有操作Queue,并打算以队列方式使用,且队列长度受限,通常建议使用offer()、poll()、与peek()等方法。想对队列的前端与尾端进行操作,在前端加入对象与取出对象,在尾端加入对象与取出对象,Queue的子接口Deque就定义了这类行为。

import java.util.*;
interface Request{
void execute();
}
public class RequestQueue {
public static void main(String[] args) {
Queue requests = new LinkedList();
offerRequestTo(requests);
process(requests);
}
static void offerRequestTo(Queue requests){
for(int i=1;i<6;i++){
Request request = new Request(){
public void execute(){
System.out.printf("处理数据%f%n",Math.random());
}
};
requests.offer(request);
}
}
static void process(Queue requests){
while(requests.peek()!=null){
Request rquest = (Request) request.poll();
request.execute();
}
}
}

本周代码托管截图

其他(感悟、思考等,可选)

随着学习进度的跟进,内容越来越多,有些知识的容易相互混淆。现在光敲代码意义没那么大,还是要自己多总结,多思考,才能有所收货,有所进步。

学习进度条

代码行数(新增/累积) 博客量(新增/累积) 学习时间(新增/累积) 重要成长
目标 5000行 30篇 400小时
第五周 250/1000 1/9 27/125

最新文章

  1. opencv算法学习
  2. Java编程里类的继承
  3. cs11_c++_lab4b
  4. EF初接触01
  5. [Prodinner项目]学习分享_第一部分_Model层
  6. 【规范】javascript 变量命名规则
  7. package.json 字段全解析 share
  8. Java运行系统命令并获取值(Process java.lang.Runtime.exec(String[] cmdarray, String[] envp, File dir)
  9. Oracle 闪回特性(FLASHBACK DATABASE)
  10. SVN版本管理提示信息
  11. VScript 函数调用的两种分类:Sub过程和Function过程
  12. Ruby on Rails Session 2: How to install Aptana Studio 3 on Ubuntu 12.04 LTS
  13. SmartBusinessDevFramework架构设计-1:结构简介
  14. 改造百度UMeditor(UEditor-min)富文本编辑器的图片上传功能
  15. 关于cisco ccp 或sdm管理gns3中思科路由器的成功分享
  16. html css笔记zht
  17. [转] 图解Seq2Seq模型、RNN结构、Encoder-Decoder模型 到 Attention
  18. mysql原理~undo管理
  19. IntelliJ IDEA 2017版 编译器使用学习笔记(七) (图文详尽版);IDE快捷键使用;IDE代码重构(编写高质量代码)
  20. springboot + mybatis +easyUI整合案例

热门文章

  1. Android FragmentActivity 给Fragment传值
  2. ORACLE WITH AS 用法,创建临时表
  3. mysql如何用sql添加字段如何设置字符集和排序规则
  4. fieldmeta 基于springboot的字段元数据管理,通用代码生成,快速开发引擎
  5. 170419、Centos7下完美安装并配置mysql5.6
  6. session------&gt;防表单重复提交
  7. JavaScript中的原型与原型链
  8. 剑指Offer——机器人的运动范围
  9. 在setting中实现可拔插的插件功能实现
  10. tornado调用ioloop TracebackFuture实现非堵塞的模块