java18

集合
Iterator 迭代器
异常的处理
1、集合
Collection: 单列集合类的根接口,用于存储-系列符合某种规则的元素,它有两个重要的子接口,分别是java.util.List和java.util.Set。其中,List的特点是元素有序、元素可重复。Set 的特点是元素无序,而且不可重复。List接口的主要实现类有java. util.HashSet和java.util.TreeSet。
集合本身是一个工具,它存放在java.util包中。 在collection 接口定义着单列集合框架中最最共性的内容。
1.1、 Collection常用功能
Collection是所有单列集合的父接口,因此在Collection中定义了单列集合( List和Set)通用的一些方法,这些方法可用于操作所有的单列集合。方法如下:

public boolean add(E e)把给定的对象添加到当前集合中。
public void clear ():清空集合中所有的元素。
pub1ic boolean remove (E e):把给定的对象在当前集合中删除。
public boolean contains (E e):判断当前集合中是否包含给定的对象。
public boolean isEmpty ( ) :判断当前集合是否为空。
public int size ( ) :返回集合中元素的个数。
public object[ ] toArray ( ): 把集台中的元素,存储到数组中。
例一:
运行代码如下:
package demo01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;

public class Demo01Collection {
public static void main(String[] args) {
// ArrayList coll = new ArrayList<>();
// Collection coll = new ArrayList<>(); //体现了多态

	Collection<String> coll = new HashSet<>();
	System.out.println(coll);
	
	boolean b1 = coll.add("张");
	System.out.println(b1);
	System.out.println(coll);
	
	coll.add("一");
	coll.add("二");
	coll.add("三");
	coll.add("四");
	coll.add("五");
	coll.add("六");
	System.out.println(coll);
	
	boolean b2 = coll.remove("五");
	System.out.println(b2);
	System.out.println(coll);
	
	boolean b3 = coll.contains("六");
	System.out.println(b3);
	System.out.println(coll);
	boolean b4 = coll.remove("五");
	System.out.println(b3);
	System.out.println(coll);
	
	boolean b5 = coll.isEmpty();
	System.out.println(b5);
	System.out.println(coll);
	
	int b6 = coll.size();  //返回集合中元素个数
	System.out.println(b6);
	System.out.println(coll);
	
	Object[] arr = coll.toArray();
	System.out.println(arr[0]);
	System.out.println("=====================");
	
	coll.clear();
	System.out.println(coll);
	b5 = coll.isEmpty();
	System.out.println(b5);
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
运行结果:

2、Iterator迭代器
2.1、 Iterator接口
在程序开发中,经常需要遍历集合中的所有元素。针对这种需求,JDK专门提供了一个接口java.util.Iterator。Iterator接口也是Java集合中的一员,但它与collection、Map接口有所不同,collection接口与Map接口主要用于存储元素,而Iterator主要用于迭代访问(即遍历) Collection中的元素,因此Iterator对象也被称为迭代器。
想要遍历Collection集合,那么就要获取该集合迭代器完成迭代操作,下面介绍下犹取达代器的万法:

public Iteratoriterator:获取集合对应的迭代器,用来遍历集合中的元素的。
下面介绍一下迭代的概念:
迭代:即Collection集合元素的通用狱取方式。在取元素之前先要判断集合中有没有元素,如果有,就把这个元素取出来,继续在判断,如果还有就再取出出来。一直把集合中的所有元素全部取出。这种取出方式专业术语称为迭代。
Iterator接口的常用方法如下:
public E next O :返回迭代的下一个元素。
public boolean hasNextO:如果仍有元素可以迭代,则返回true。
例二:
运行代码如下:
package demo01;

import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo02Iterator {
public static void main(String[] args) {
Collection coll = new ArrayList<>();

	coll.add("one");
	coll.add("two");
	coll.add("three");
	coll.add("four");
	coll.add("five");
	coll.add("six");
	
	Iterator<String> it = coll.iterator();
	if(it.hasNext()) {
		String e = it.next();
		System.out.println(e);
	}
	System.out.println("====================");
	
	while(it.hasNext()) {
		String e = it.next();
		System.out.println(e);
	}
	System.out.println("====================");
	
	for(Iterator<String> it2 = coll.iterator();it2.hasNext();) {
		String e = it2.next();
		System.out.println(e);
	}
	
	System.out.println("=========第三次===========");
	//增强for循环
	for(String s:coll) {
		System.out.println(s);
	}
	
	System.out.println("=========第四次===========");
	int[] arr1 = {12,23,45,67,90};
	for(int i=0;i<arr1.length;i++)
	{
		System.out.println(i);
	}
	
	for(int i:arr1)
	{
		System.out.println(i);
	}
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
运行结果:

3、异常的处理
Java异常处理的五个关键字: try、catch、 finally、throw、throws
概述: 就是程序出现了不正常的情况。
3.1、异常处理——捕获异常try…catch…

格式
try{
可能出现异常的代码;
}catCh(异常类名变量名) {
异常的处理代码;

执行流程
程序从try里面的代码开始执行。
出现异常,会自动生成一个异常类对象,该 异常对象将被提交给Java运行时系统。
当Java运行时系统接收到异常对象时,会到catCh中去找匹配的异常类,找到后进行异常的处理执行完毕之后,程序还可以继续往下执行。

例三:
运行代码如下:

package demo02;

import java.util.Date;
import java.text.ParseException;
import java.text.SimpleDateFormat;

public class Demo01Exception {
public static void main(String[] args) {
//编译期异常
SimpleDateFormat sdf = new SimpleDateFormat(“yyyy-MM-dd”);

	Date date = null;
	try {
		date = sdf.parse("2021-05-11");
	} catch (ParseException e) {
		
		e.printStackTrace();
	}
	
	System.out.println(date);
	System.out.println("第二句测试");
	
	//runtimeException  运行期异常
	int[] arr = {1,2,3};
	System.out.println(arr[0]);
	// 有问题。但是不会提示,在执行的时候才会有异常
	try {
		System.out.println(arr[2]);
	}catch(Exception e) {
		System.out.println("第一句测试");
		System.out.println(e);
	}
	
	//错误
	int[] arr2 = new int[1024*1024*1024*1024*1024];
	System.out.println("后续代码");
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
运行结果:

3.2、异常处理——抛出异常throw
在编写程序时,我们必须要考虑程序出现问题的情况。比如,在定义方法时,方法需要接受参数。那么,当调用方法使用接受到的参数时,首先需要先对参数数据进行合法的判断,数据若不合法,就应该告诉调用者,传递合法的数据进来。这时需要使用抛出异常的方式来告诉调用者。
在java中,提供了一个throw关键字,它用来抛出一个指定的异常对象。那么,抛出一个异常具体如何操作呢?
1.创建一个异常对象。封装一些提示信息(信息可以自己编写)。
2.需要将这个异常对象告知给调用者。怎么告知呢?怎么将这个异常对象传递到调用者处呢?通过关键字throw就可以完成。throw 异常对象。
throw用在方法内,用来抛出一个异常对象,将这个异常对象传递到调用者处,并结束当前方法的执行。

使用格式:
throw new异常类名(参数);
例如:
throw new Nul1PointerExc ept i on(“要访问的arr数组不存在”)
thrownewArrayIndexoutofBoundsException(“该索引在数组中不存在,已超出范围”);
例四:
运行代码如下:
package demo02;
//抛出异常throw
public class Demo02Throw {
public static void main(String[] args) {
int[] arr = null;
//int[] arr = new int[3];

	int e = getElement(arr,3);
	System.out.println(e);
}

public static int getElement(int[] arr,int index) {
	if(arr == null) {
		throw new NullPointerException("传递的数组值是null");		
	}
	if(index<0||index>arr.length-1) {
		throw new ArrayIndexOutOfBoundsException("传递的索引超出数组的正常使用范围");
	}
	int ele = arr[index];
	return ele;
}

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
运行结果:(显示错误)
3.3、Throwable体系
Error: 严重错误Error 无法通过处理的错误,只能事先避免,好比绝症。
Exception: 表示异常,异常产生后程序员可以通过代码的方式纠正,使程序继续运行,是必须要处理的。好比感冒、阑尾炎。
3.4、Throwable中的常用方法

public void printstackTrace ()打印异常的详细信息。
包含了异常的类型,异常的原因还包括异常出现的位置在开发和调试阶段都得使用printStackTrace。
public String getMessage ()获取发生异常的原因。
提示给用户的时候就提示错误原因。
public String toString ()获取异常的类型和异常描述信息(不用)。
出现异常,不要紧张把异常的简单类名拷贝到API中去查。
例五:
运行代码如下:
package demo02;

import java.io.FileNotFoundException;
import java.io.IOException;

public class Demo03Throws {
// public static void main(String[] args) throws FileNotFoundException, IOException
public static void main(String[] args) throws Exception{
//readFile(“c:\a.txt”);
readFile(“c:\a”);
System.out.println(“后续代码”);
}

 public static void readFile(String fileName) throws FileNotFoundException,IOException {
	if(!fileName.equals("c:\\a.txt")) {
		throw new FileNotFoundException("传递的文件不是c:\\a.txt");
	}
	if(!fileName.endsWith(".txt")) {
		throw new IOException("文件后缀名有无");
	}
	System.out.println("文件正确");
}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值