学习目标
1:collection集合
2:Iterator迭代器
3:异常
4:Throw和Throws抛出异常
学习内容
一:collection集合
概念:
Collection是最基本的集合接口,一个Collection代表一组Object,即Collection的元素(Elements)。一些Collection允许相同的元素而另一些不行。一些能排序而另一些不行,于是衍生出两个子类接口List和Set
功能:
C:添加功能
boolean add(Object obj):向集合中添加一个元素
boolean addAll(Collection c):向集合中添加一个集合的元素。
B:删除功能
void clear():删除集合中的所有元素。
boolean remove(Object obj):从集合中删除指定的元素
boolean removeAll(Collection c):从集合中删除一个指定的集合元素。
C:判断功能
boolean isEmpty():判断集合是否为空。
boolean contains(Object obj):判断集合中是否存在指定的元素。
boolean containsAll(Collection c):判断集合中是否存在指定的一个集合中的元素。
D:遍历功能
Iterator iterator():就是用来获取集合中每一个元素。
E:长度功能
int size():获取集合中的元素个数
F:交集功能
boolean retainAll(Collection c):判断两个集合中是否有相同的元素。???
G:把集合转换成数组
Object[] toArray():把集合变成数组。
例如:
package com.hg.demo01;
import java.util.ArrayList;
import java.util.Collection;
public class Demo01Cllecion {
public static void main (String[] args) {
Collection<String> coll=new ArrayList<>();
System.out.println(coll);
boolean b1=coll.add("颜敏");
System.out.println(b1);
System.out.println(coll);
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);
boolean b4=coll.remove("王五");
System.out.println(b4);
boolean b5=coll.isEmpty();
System.out.println(b5);
int b6=coll.size();
System.out.println(b6);
Object[] arr=coll.toArray();
System.out.println(arr[0]);
System.out.println("=======================");
coll.clear();
System.out.println(coll);
}
}
结果为:
二:Iterator迭代器
package com.hg.demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Demo01Iterator {
public static void main (String[] args) {
Collection<String> coll=new ArrayList<>();
coll.add("Hello");
coll.add("Java");
coll.add("姚明");
coll.add("詹姆斯");
coll.add("科比");
coll.add("World");
//ArrayList<String> list=new ArrayList<>();
//获取迭代器对象
Iterator<String> it=coll.iterator();
//hasNext()判断是否有下一个元素,如果有就用next()获取
while(it.hasNext()){//集合的便利
//获取下一个元素
String s=it.next();
System.out.println(s);
}
System.out.println("========================");
//迭代器的使用
for(Iterator<String> it2=coll.iterator(); it2.hasNext();) {
String s=it2.next();
System.out.println(s);
}
System.out.println("==============第三次==========");
//增强型for循环
for(String s:coll){
System.out.println(s);
}
System.out.println("==========第四次==============");
int[] arr={1,2,3,4,5,6};
//高级for循环遍历数组
for(int a:arr){
System.out.println(a);
}
}
}
结果为;
三:异常
概念:
Exception(异常):是程序本身可以处理的异常。Exception 类有一个重要的子类 RuntimeException。RuntimeException 类及其子类表示“JVM 常用操作”引发的错误。例如,若试图使用空值对象引用、除数为零或数组越界,则分别引发运行时异常(NullPointerException、ArithmeticException)和 ArrayIndexOutOfBoundException。
分类:
Exception(异常)分两大类:运行时异常和非运行时异常(编译异常)。程序中应当尽可能去处理这些异常。
package com.hg.demo02;
import java.sql.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");
java.util.Date date=null;
System.out.println("第一句测试");
try {
date= sdf.parse("2021-05-11");
}catch (ParseException e) {
System.out.println(date);
System.out.println("第二句测试");
//运行期异常
int[] arr= {1,2,3};
System.out.println(arr[0]);
try {
System.out.println(arr[3]);//有问题,但是不会提示,因为能够编译通过,在执行的时候才会异常
}catch(Exception s) {
System.out.println("第一句测试");
System.out.println(e);
}
//error错误
int[] arr2= new int[1027*4456*48921*889219];
System.out.println("后续代码");
}
}
}
结果为:
四:Throw和Throws抛出异常
异常抛出:任何Java代码都可以抛出异常,如:自己编写的代码、来自Java开发环境包中代码,或者Java运行时系统。无论是谁,都可以通过Java的throw语句抛出异常。从方法中抛出的任何异常都必须使用throws子句。
throw语句用在方法体内,表示抛出异常,由方法体内的语句处理。
throws语句用在方法声明后面,表示抛出异常,由该方法的调用者来处理。
package com.hg.demo02;
public class demo01Throw {
public static void main (String[] args) {
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) {
}
if(index<0||index>arr.length-1) {
throw new ArrayIndexOutOfBoundsException("传递的索引超出了数组的正常使用");
}
int ele=arr[index];
return ele;
}}
结果为:
throws主要是声明这个方法会抛出这种类型的异常,使它的调用者知道要捕获这个异常。
throw是当程序出现某种逻辑错误时由程序员主动抛出某种特定类型的异常是,具体向外抛异常的动作,所以它是抛出一个异常实例。
package com.hg.demo02;
import java.io.FileNotFoundException;
import java.io.IOException;
public class Demo03throwS {
public static void main(String[] args) throws Exception{
readFile("c:\\a.txt");
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("文件正确");
}
}
结果为:
2020080605041