千锋逆战班day26

day26
在千锋逆战学习第26天
越努力越幸运
15、Map集合的遍历
KeySet(); 获得Map集合中所有的键。返回的是Set集合
Values(); 获得Map集合中的所有值。返回的是Collection集合
entrySet(); 返回的是Map.Entry(Node) 包含了getValue()和getValue()。直接输出调用的是toString打印键值对

package com.qf.day26.maps;

import java.util.*;

public class HashMapApply {

	public static void main(String[] args) {
		Map<String,String> maps=new HashMap<String,String>();
		maps.put("CN", "中国");
		maps.put("US", "美国");
		maps.put("JP", "日本");
		maps.put("IT", "意大利");
		
		//entrySet();  键值对   getKey()  getValue()
		//Set集合里存放的都是node对象
		Set<Map.Entry<String,String>> entry=maps.entrySet();
		for (Map.Entry<String, String> e : entry) {
			System.out.println(e.getKey()+":"+e.getValue());
			System.out.println(e);
		}
		
		
		//遍历方式1  KeySet();  键-无序、无下标、不可重复
		//遍历的是所以的键
		Set<String> keys=maps.keySet();
		for (String k : keys) {
			System.out.println(maps.get(k));	
		}
		
		//遍历方式2  values 值 可以重复 
		Collection<String> cs=maps.values();
		for (String value : cs) {
			System.out.println(value);
		}
		
	}

}

16、Iterator 迭代器
专注于迭代Collectioin体系的集合

package com.qf.day26.maps;

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class TestIterator {

	public static void main(String[] args) {
		// 对于Collection体系集合进行遍历
		List<String> list=new ArrayList<String>();
		list.add("A");
		list.add("B");
		list.add("C");
		list.add("D");
		
		//1、获得一个迭代器
		//hasNext();  是否有下一个元素可以遍历  有true  没有false
		//next();   返回下一个元素
		Iterator<String> iter=list.iterator();
		while(iter.hasNext()){
			System.out.println(iter.next());
		}
		
		Set<String> sets=new HashSet<String>();
		sets.add("E");
		sets.add("F");
		sets.add("G");
		sets.add("H");
		
		Iterator<String> si=sets.iterator();
		while(si.hasNext()){
			System.out.println(si.next());
		}
	}

}



第十三章异常
1、什么是异常
程序在运行过程中出现的特殊情况
异常处理的必要性:任何程序都可能存在大量的未知问题。错误:如果不对这些问题进行正确处理,则可能导致程序的中断,造成不必要的损失
2、异常的分类
Throwable:可抛出、一切错误或异类的父类。位于java.long包中
Error:JVM、硬件、执行逻辑错误,不能手动处理
Exception:程序在运行和配置过程中产生的问题,可处理
RuntimeException:运行时异常,可处理,可不处理
CheckedException:受查异常,必须处理

package com.qf.day26.errors;

import java.util.Scanner;

public class TestException {

	public static void main(String[] args) {
		m6();//不处理---->java虚拟机处理,打印堆栈跟踪信息
		//程序运行过程当中产生的

	}
	//java.lang.ClassCastException 类型转换异常
	public static void m1(){
		Object o=new Integer(2);
		Scanner sc=(Scanner) o;
	}
	
	//java.lang.ArithmeticException 算数异常
	public static void m2(){
		System.out.println(10/0);
	}
	
	//java.lang.ArrayIndexOutOfBoundsException数组下标越界
	public static void m3(){
		int[] nums=new int[1];
		System.out.println(nums[1]);
	}
	
	//java.lang.StringIndexOutOfBoundsException字符串下标越界
	public static void m4(){
		String str="anc";
		System.out.println(str.charAt(5));
	}
	
	//java.util.InputMismatchException 输入匹配异常
	public static void m5(){
		Scanner sc=new Scanner(System.in);
		System.out.println("请输入一个数:");
		int num=sc.nextInt();
	}
	
	//java.lang.NullPointerException 空指针异常
	public static void m6(){
		Object o=null;
		o.hashCode();
	}

}



3、异常的产生:
自动抛出异常:当程序在运行时遇到不符合规范的代码或结果时,会产生异常
手动抛出异常:throw new 异常类型(“实际参数”)
一旦产生异常结果:相当于执行return语句,导致程序因异常而终止

public class TestThrowException {

	public static void main(String[] args) {
		Student stu=new Student();
		
		stu.setAge(20);//在调用者角度看,不清楚违规问题
		System.out.println(stu.getAge());
	}

}
class Student{
	private int age;
	public int getAge(){
		return this.age;
	}
	public void setAge(int age){
		if(age>0&&age<125){
			this.age=age;
		}else{
			RuntimeException re=new RuntimeException();//1、创建异常对象,单独存在就意味着仅仅是个对象
			throw re;//2、结合throw关键字,抛出异常
			
			//throw new RuntimeException();
		}
	}
}

4、异常的传递:
按照方法的调用链反向传递,如果最终都没有处理异常,最终交由我们的JVM进行默认异常处理(打印堆栈跟踪信息)
受查异常:throws声明异常,声明位置:修饰在方法参数列表的后端
运行时异常:因其可处理,可不处理,无需声明

package com.qf.day26.transfer;

import javax.management.RuntimeErrorException;

public class TestTeabsferException {

	public static void main(String[] args)throws Exception {
		System.out.println("main--start");
		m1(10);
		System.out.println("main--end");
	}
	public static void m1(int n)throws Exception{
		System.out.println("m1--start");
		m2(n);
		System.out.println("m1--end");
	}
	
	public static void m2(int n)throws Exception{
		System.out.println("m2--start");
		m3(n);
		System.out.println("m2--end");
	}
	
	//throws 消极的处理方式
	public static void m3(int n)throws Exception{//向上声明--声明该方法存在受查异常
		System.out.println("m3--start");
		if(n%2==0){
			throw new Exception();
//			throw new RuntimeException();
		}	
		System.out.println("m3--end");
	}
	
	public static void method(){
		
	}

}

5、异常的处理

package com.qf.day26.dispose;

import java.util.Scanner;

public class TestTryCatch {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		
		System.out.println("请输入一个被除数:");
		int num1=sc.nextInt();
		
		System.out.println("请输入一个除数:");
		int num2=sc.nextInt();
		
		try{
			int result=num1/num2;
			System.out.println(result);
		}catch(Exception e){
			System.out.println("除数不可为0");//处理方案1:自定义处理
			e.printStackTrace();//处理方案2:打印堆栈跟踪信息
			System.out.println(e.getMessage());//处理方案3:获取异常的原因message
			
		}
		System.out.println("程序结束");

	}

}

package com.qf.day26.dispose;
import java.util.InputMismatchException;
import java.util.Scanner;

public class TestMoreTryCatch {

	public static void main(String[] args) {
		Scanner sc=new Scanner(System.in);
		 int num1=0;
		 int num2=0;
		try{
			System.out.println("请输入一个被除数:");
			num1=sc.nextInt();
			
			System.out.println("请输入一个除数:");
			num2=sc.nextInt();
			
			int result=num1/num2;
			System.out.println(result);
		

		}catch(ArithmeticException e){
			System.out.println("除数不可为0");
		}catch(InputMismatchException e){
			System.out.println("请输入正确的整数!");
		}catch(Exception e){
			System.out.println("出现未知问题");
		}
	}
}
package com.qf.day26.dispose;

public class TestFinally {

	public static void main(String[] args) {
		System.out.println(method(2));

	}
	public static int method(int n){
		try{
			if(n%2==0){
				throw new RuntimeException();
			}
			return 20;
		}catch (Exception e) {
			System.out.println("解决异常。。。");
			return 30;
		}finally{
			System.out.println("程序结束!");
		}
	}

}

Chapter 10 异常
Key Point:
●异常的概念和分类
●异常的产生和传递
●异常的处理
●自定义异常
习题:
1、填空
java中所有的错误都继承自 Throwable 类;在该类的子类中, Error 类表示严重的底层错误,对于这类错误一般的处理方式是 不应该试图捕捉他 ; Exception 类表示例外、异常

2、查询API,填空
异常类java.rmi.AlreadyBoundException类;从分类上说,该类属于 已检察 异常,从处理方式上说,对这种异常 必须 处理
异常类java.util.regex.PattemSynataxException,从分类上说,该类属于 未检查 异常,从处理方式上说,对这种异常 可处理可不处理 处理

3、(异常的产生)把下面代码补全
public class TestThrow {
public static void main(String[] args) {
throwException(10);
}
public static void throwException(int n){
if(n==0){
//抛出一个NullPointerException
throw new NullPointerException();
}else{
//抛出一个ClassCastException
throw new ClassCastException();
//并设定详细信息为“类型转换出错”
//char a=(char) n;
}
}
}

4、(try-catch-finally)有以下代码:
package t4;

import java.io.;
import java.sql.
;
public class TestException {
public static void main(String[] args) {
System.out.println(“main 1”);
int n;
//读入n
ma(n);
System.out.println(“main 2”);
}
public static void ma(int n){
try{
System.out.println(“ma1”);
mb(n);
System.out.println(“ma2”);
}catch(EOFException e){
System.out.println(“Catch EOFException”);
}catch(IOException e){
System.out.println(“Catch IOException”);
}catch(SQLException e){
System.out.println(“Catch SQLException”);
}catch (Exception e) {
System.out.println(“Catch Exception”);
}finally{
System.out.println(“In finally”);
}
}
public static void mb(int n)throws Exception{
System.out.println(“mb1”);
if(n1)throw new EOFException();
if(n
2)throw new FileNotFoundException();
if(n3)throw new SQLException();
if(n
4)throw new NullPointerException();
System.out.println(“mb2”);
}
}
问:当读入的n分别为1,2,3,4,5时,输出的结果分别是什么?
答:n=1时:
main 1
ma1
mb1
Catch EOFException
In finally
main 2

n=2时:
main 1
ma1
mb1
Catch IOException
In finally
main 2

n=3时:
main 1
ma1
mb1
Catch SQLException
In finally
main 2

n=4时:
main 1
ma1
mb1
Catch Exception
In finally
main 2

n=5时:
main 1
ma1
mb1
mb2
ma2
In finally
main 2
7、(try-catch)代码改错:
class MyException{}
class TestException {
public static void main(String[] args) {
ma();
}
public static int ma(){
try{
m();
return 100;
}catch (Exception e) {
System.out.println(“Exception”);
}catch(ArithmeticException e){
System.out.println(“ArithmeticException”);
}
}
public static void m(){
throw new MyException();
}
}
改完为:
class MyException{}
class TestException {
public static void main(String[] args) {
ma();
}
public static int ma(){
try{
m();
return 100;

	}catch(ArithmeticException e){
		System.out.println("ArithmeticException");
	}catch (Exception e) {
		System.out.println("Exception");
	}
	return 200;
}
public static void m() throws Exception{
	throw new Exception();
}

}
9、(try-catch 局部变量)有以下代码:
public class TestTryCatch {
public static void main(String[] args) {
System.out.println(ma());
}
public static int ma(){
int n;
try{
n=10/0;
}catch (Exception e) {

	}
	return n;
}

}
选择正确答案: A
A.编译不通过
B.编译通过,输出-1
C.编译通过,输出0
11、(try-finally)写出下面代码的运行结果
public class TestTryFinally {
public static void main(String[] args) {
try {
ma();
} catch (Exception e) {
}
}
public static void ma() throws Exception{
int n=10;
int b;
//读入一个整数b
try{
System.out.println(“ma1”);
int result=n/b;
System.out.println(“ma2”+result);
}finally{
System.out.println(“In Finally”);
}
}
}
在ma中,读入整数b。如果读入的值为10,则输出:
ma1
ma21
In Finally
如果读入的值为0,则输出:
ma1
In Finally
13、(异常的捕捉和抛出)有以下代码:
public class TestException {
public static void main(String[] args) {
try{
System.out.println(“main1”);
ma();
System.out.println(“main2”);
}catch (Exception e) {
System.out.println(“In Catch”);
}
}
public static void ma(){
System.out.println(“ma1”);
throw new NullPointerException();
System.out.println(“ma2”);
}
}
选择正确答案: A
A.编译出错
B.编译正常,输出main1 ma1 In Catch
C.编译正常,运行时出错
14、(异常的捕捉和抛出)有以下代码:
import java.io.;
import java.sql.
;
class TestException {
public static void main(String[] args) {
try{
ma();
}
//1
catch (Exception e) {
}
}
public static void ma() throws IOException{}
}
下列哪些代码放在//1处可以编译通过 AB
A.catch(NullPointerException npe){}
B.catch(IOException ioe){}
C.catch(SQLException sqle){}

上一篇:使用MaxCompute Java SDK运行安全相关命令


下一篇:每日总结-Day26