List接口
1.有序的集合(存储和取出元素顺序相同)
2.允许存储重复的元素
3.有索引,可以使用普通的for循环遍历
Collection接口
1.定义的是所有单列集合中共性的方法,所有的单列集合都可以使用共性的方法,没有带索引的方法。
2.集合框架的学习方式:
(1)学习顶层:学习顶层接口/抽象类中共性的方法,所有的子类都可以使用。
(2)使用底层:底层不是接口就是抽象类,无法创建对象使用,需要使用底层的子类创建对象使用
3.继承:子类共性抽取,形成父类(接口)
public boolean add(E e):把给定的对象添加到当前集合中
public void clear():清空集合中所有的元素
public boolean remove(E e):把给定的对象在当前集合中删除
public boolean contains(E e):判断当前集合中是否包含给定的对象
public boolean isEmpty():判断当前集合是否为空
public int size():返回集合中元素的个数
public Object[] toArray():把集合中的元素,存储到数组中
Set接口
1.不允许存储重复元素
2.没有索引(不能使用普通的for循环遍历)
3.无序的集合(存储和取出元素的顺序有可能不一致)
抛出异常throw
在编写程序时,我们必须要考虑程序出现问题的情况,比如在定义方法时,方法需要接受参数,那么当调用方法使用接受到的参数时,首先需要先对参数数据进行合理的判断,数据若不合法,就应该告诉调用者传递合法的数据进来,这时需要使用抛出异常的方法来告诉调用者。
在java中提供了一个throw关键字,它用来抛出一个指定的异常对象。
操作方法:
1.创建一个异常对象,封装一些提示信息(信息可以自己编写)
2.throw用在方法内,用来抛出一个异常对象,将这个异常对象传递给调用者处,并结束当前方法的执行
代码与成果展示
demo01
package demo01;
import java.util.ArrayList;
import java.util.Collection;
import java.util.HashSet;
public class Demo01Collection {
public static void main(String[] args) {
//ArrayList<String> coll=new ArrayList<>();
//Collection<String> 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("龙");
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();
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<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();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= {45,2,4,6,8,};
for(int i=0;i<arr1.length;i++)
{
System.out.println(i);
}
for(int i:arr1) {
System.out.println(i);
}
}
}
demo02
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("yyyy-MM-dd");
Date date=null;
try {
date=sdf.parse("2021-05-11");
}
catch(ParseException e)
{
e.printStackTrace();
}
System.out.println(date);
//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);
}
//error 错误
int[] arr2=new int[1024*1024*1024];
System.out.println("后续代码");
}
}
package demo02;
public class Demo02Throw {
public static void main(String[] args) {
int[] arr=new int[3];
//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 Demo03Throws {
//public static void main(String[] args) throws FileNotFoundException, IOException{
//public static void main(String[] args) throws IOException{
public static void main(String[] args) throws Exception{
readFile("c:\\a");
System.out.println("后现代码");
}
public static void readFile(String string) throws FileNotFoundException, IOException {
if(!string.equals("c:\\a.txt")) {
throw new FileNotFoundException("传递的文件不是c:\\a.txt");
}
if(!string.endsWith(".txt")) {
throw new IOException("文件后缀名有无");
}
System.out.println("文件正确");
}
}