JAVA19
Collection
是单例集合的顶层接口,它表示一组对象,这些对象也称为Collection的元素
JDK 不提供此接口的任何直接实现,它提供更具体的子接口(如Set和List)实现。
创建Collection集合的对象
多态的方式
具体的实现类ArrayList`
package demo01;
import java.util.ArrayList;
import java.util.Collection;
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("王五");
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]);
}
}
Iterator:迭代器
,集合的专用遍历方式
Iterator iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到
迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的
Iterator中的常用方法
E next():返回迭代中的下一个元素
boolean hasNext():如果迭代具有更多元素,则返回 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<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);
}
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();it.hasNext();) {
String e=it2.next();
System.out.println(e);
}
System.out.println("=======第三次==========");
for(String s:coll) {
System.out.println(s);
}
System.out.println("=======第四次==========");
int[] arr1= {45,6,7,8,9,10};
for(int i=0;i<arr1.length;i++)
{
System.out.println(i);
}
for(int i:arr1) {
System.out.println(i);
}
}
}
运行
[]
true
[张三]
[张三, 李四, 李四, 王五, 赵六]
true
[张三, 李四, 李四, 赵六]
true
false
false
4
张三
姚明
詹姆斯
===========
易建联
麦迪
库里
科比
===========
=======第三次==========
姚明
詹姆斯
易建联
麦迪
库里
科比
=======第四次==========
0
1
2
3
4
5
45
6
7
8
9
10
package demo02;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class demo01Exception {
public static void main(String[] args) {
//编译异常
SimpleDateFormat sdf=new SimpleDateFormat("yyy-MM-dd");
Date date=null;
try {
date=sdf.parse("2021-05-11");
}catch(ParseException e) {
e.printStackTrace();
}
System.out.println(date);
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*10241024*1024];
System.out.println("后续代码");
}
}
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[] args) throws Exception {
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("文件正确");
}
}