java第十八次学习笔记

java第十八次学习笔记


前言

我觉de


一、Collection是什么?

1.Collection集合概述

●单例集合的顶层接口,它表示-组对象,这些对象也称为Collection的元素
●JDK 不提供此接口的任何直接实现, 它提供更具体的子接口(如Set和List)实现
创建Collection集合的对象
多态的方式
●具体的实现类ArrayList
在这里插入图片描述

2.集合类体系结构

在这里插入图片描述

3.常用方法

在这里插入图片描述

代码如下(示例):

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

public class Demo01Collection {
    
    
	public static void main(String []args) {
    
    
		// ArrayList<String> coll = new ArrayList<>()
		
		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("李五");
		coll.add("李六");
		coll.add("李七");
		
		System.out.println(coll);
		
		boolean b2=coll.remove("李五");
		System.out.println(b2);
		System.out.println(coll);
		
		boolean b3=coll.remove("李六");
		System.out.println(b3);
		System.out.println(coll);
		
		boolean b4= coll.contains("李五");
		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);
		b5=coll.isEmpty();
		System.out.println(b5);
	}

}

在这里插入图片描述

4.Collection集合的遍历

Iterator:迭代器,集合的专用遍历方式
Iterator iterator(): 返回此集合中元素的迭代器,通过集合的iterator0方法得到

迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
Iterator中的常用方法

E next0: 返回迭代中的下一个元素
●boolean hasNext():如果迭代具有更多元素,则返回true

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("姚明");
		coll.add("麦迪");
		coll.add("库里");
		coll.add("詹姆斯");
		coll.add("欧文");
		coll.add("艾弗森");
		
		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= {
    
    4,5,6,7,8,9,10};
		for(int i: arr1) {
    
    
			System.out.println(i);
		}
				
	}
	

}

在这里插入图片描述

二、List

1.List集合概述

●有集合(也称为序列), 胪可以精确控制列表中海个元素的插入位置。驴可以通过整数索引访问元素,
并搜索列表中的元素
●与Set集合不同, 列表通常允许重复的元素
List集合特点
●有序:存储和取出的元素顺序一 致
可重复:存储的元素可以重复

2.List集合特有方法

在这里插入图片描述

3. Listlterator

Listlterator:列表迭代器
●通过List集合的listlterator0方法得到, 所以说它是List集合特有的迭代器
●于允许程序员沿任一方向遍历列表的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置
Listlterator中的常用方法

E next():返回迭代中的下一个元素

boolean hasNext():如果迭代具有更多元素,则返回true

E previous():返回列表中的上一个元素

boolean hasPrevious(:如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回true
●void add(E e):将指定的元素插入列表

三、异常

1.异常概述

在这里插入图片描述

2. try…catch…

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

package Demo02;

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

public class Demo02Exception {
    
    
	public static void main(String[] args) {
    
    
		//编译期间异常
		SimpleDateFormat sdf= new SimpleDateFormat("yyyy-MM-dd");
		
		Date date =null;
		//运行期异常
		System.out.println("第一句测试");
		try {
    
    
			date = sdf.parse ("2021-05-11");
		} catch (ParseException e) {
    
    
			
			e.printStackTrace();
		}
		
		System.out.println(date);
		System.out.println("第二句测试");
		
		//运行期异常
		int [] arr= {
    
    1,122,3};
		System.out.println(arr[0]);
		//有问题,但是不会提示,因为编译能够通过,在执行的时候才会有异常
		try {
    
    
			System.out.println(arr[2]);

		}catch(Exception e) {
    
    
			System.out.println("第一句测试");
			System.out.println(e);

		}
		//error  错误
		int [] arr2= new int[1024*10000000];
		System.out.println("后续代码");
	}

}

在这里插入图片描述

3.throw 和 throws

在这里插入图片描述
注意:这个格式是跟在方法的括号后面的
编译时异常必须要进行处理,两种处理方案: try…catch … .或者throws,如果采用throws这种方案,将来谁调用谁处理
运行时异常可以不处理,出现问题后,需要我们回来修改代码

Java中的异常被分为两大类:编译时异常和运行时异常,也被称为受检异常和非受检异常
所有的RuntimeException类及其子类被称为运行时异常,其他的异常都是编译时异常

编译时异常:必须显示处理,否则程序就会发生错误,无法通过编译
运行时异常:无需显示处理,也可以和编译时异常-样处理

package Demo02;
//java异常处理的五个关键字try catch finally throw throws
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;
		
	}

}

在这里插入图片描述

package Demo02;

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

public class Demo02Throws {
    
    
	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("文件正常");
		}

}

在这里插入图片描述


猜你喜欢

转载自blog.csdn.net/weixin_54405545/article/details/116661238