2021-05-11

第十八天学习java
学习内容:

:Collection

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


Iterator:迭代器
,集合的专用遍历方式
Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的

Iterator中的常用方法
E next():返回迭代中的下一个元素
boolean hasNext():如果迭代具有更多元素,则返回 true

三、
并发修改异常
ConcurrentModificationException

产生原因
迭代器遍历的过程中,通过集合对象修改了集合中的元素,造成了迭代器获取元素中判断预期修改值和实际修改值不一致

解决方案
用for循环遍历,然后用集合对象做对应的操作即可。

四、增强for循环
简化数组和Collection集合的遍历
实现Iterable接口的类允许其对象成为增强型 for语句的目标
它是JDK5之后出现的,其内部原理是一个Iterator迭代器

增强for的格式
格式:
for(元素数据类型 变量名 : 数组或者Collection集合) {
//在此处使用变量即可,该变量就是元素
}

五、代码练习:
第一个包:

package Demo01;

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

public class Demo01Collection {
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.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();
	
	for(int i=0;i<arr.length;i++) {
		System.out.println(arr[0]);
	}
	System.out.println("===============");
	coll.clear();
	System.out.println(coll);
	b5=coll.isEmpty();
	System.out.println(b5);
}

}

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("奥利尔");
	coll.add("风云");
	coll.add("大运摩托");
	coll.add("迪迦奥特曼");
	coll.add("代码练习");
	
	Iterator<String> it=coll.iterator();
	
	if(it.hasNext()) {
		String e=it.next();
		System.out.println(e);
	}
	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);
	}
	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= {1,2,3,4,5,};
    for(int i=0;i<arr1.length;i++)
    for(int i1:arr1) {
    	System.out.println(i1);
    }

}
}

第二个包:

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=(Date) sdf.parse("2021-05-11");
 }  catch (ParseException e) {
	e.printStackTrace(); 
 }
 System.out.println("第一句测试");
 System.out.println(date);
 
 
 //runtimeException  运行期异常
 int[] arr= {1,2,3};
 System.out.println(arr[0]);
 //有问题但是不会提示,编译通过,执行会异常
 try {
	 System.out.println(arr[3]);
 }catch(Exception e) {
	 System.out.println("第二句测试");
	 System.out.println(e);
 }
 //error 错误
 int[] arr2=new int[1024*1024];

}
}

package Demo02;
//
public class Demo02Throw {
public static void main(String[] args) {
int[] arr=null;
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 Demo03Throws {
public static void main(String[] arfs) throws IOException {
readFile(“c:\a.txt”);
System.out.println(“后续代码”);
}
public static void readFile(String fileName) throws IOException {
if(!fileName.equals(“c:\a.txt”)) {
throw new FileNotFoundException(“传递的文件不是c:\\a.txt”);

	}
	if(!fileName.endsWith(".txt")) {
		throw new IOException("文件后缀名有无");
	}
	System.out.println("文件正确");
}

}

学号:2020080605015

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值