Java的异常处理和集合
一.异常处理
1. 处理方式一:
try{
//可能出现异常的代码
}catch(Exception e1){
//处理方式1
}catch(Exception e2){
// 处理方式2
}finally{
// 一定要执行的代码
}
注:
①.try内声明的变量,类似于局部变量,出了try{}语句,就不能被调用
②.catch语句内部时对异常对象的处理:
getMessage();printStackTrace;
③.如果异常处理了,那么后面的代码继续执行
④.若catch中多个异常类型时“包含”关系,须将子类放在父类的上面,进行处理。
⑤.finally中存放的时一定会被执行的代码,不管try和catch中是否有异常未被处理
2.处理方式二:
在方法的声明出,显式的使用throws+异常类型
举例:
public void method1() throws Exception1 e1 , Exception2 e2{
可能出现异常(尤其是编译时的异常,一定要处理)
}
public void method2()throws Exception1 e1 , Exception2 e2{
method1();
}
public void method3(){
try{
method2();
}catch(Exception1 e1){
System.out.print(e1.getMessage());
}catch(Exception2 e2){
System.out.print(e2.getMessage());
}
}
public static void main(String[] args){
对象1.method3();//不会出现上述Exception1和Exception2的异常
}
3.如何自定义一个异常类?
①.自定的异常类继承现有的异常类
②.提供一个序列号,提供几个重载的构造器
public class MyException extends Exception{
static final long serialVersionUID = -703489.....;
public MyException(){
//异常的处理方法
}
public MyExcepion(String msg){
super(msg);
}
}
二.Java的集合
1.一些概念:
①.存储对象可以考虑:①数组②集合
②.数组存储对象的特点:Student[] stu = new Student[20];stu[0]=new Student();…
弊端:一旦创建,其长度不可变
真实的数组存放的对象的个数是不可加的
③. 集合
Collection接口
-----List接口:
-----Set接口
2.Java集合可以分为collection和map两种体系
①.Collection接口:
-
set:元素无序,不可重复的集合 类似于高中的“集合”
|----HashSet,LinkedHashSet,TreeSet -
List:元素有序,可重复的集合 类似于“动态”数组
|----ArrayList(主要的实现类),LinkedList(对于频繁的插入,删除操作),Vertor
②Map接口:具有映射关系“key-value对”的集合
|-----HashMap,LinkedHashMap,TreeMap,Hashtable(子类:Properties)
3.Collection的学习
方法:
- add(Object obj):向集合中添加元素
- size():返回集合中的元素的个数
- isEmpty():判断集合是否为空,空为true
- clear():清空集合元素
- contains(object obj):判断当前集合中是否包含指定obj元素,如果包含,返回true,否则返回false
- containsAll(Collection coll):判断当前集合中是否包含coll中的元素
- retainAll(Collection coll):求当前集合与coll的共有的元素,返回给当前集合
- remove(Object obj):删除集合中的obj元素,若删除成功,返回true,否则返回false
- removeAll(Collection coll):从当前集合中删除包含coll中的元素
- equals(Object obj):判断集合中的所有元素是否完全相同
- hashcode():算出该集合的哈希值
- toArray():将集合转换为数组
- iterator():实现集合的遍历,返回一个Iterator接口实现类的对象
- addAll(collection coll):将形参coll中包含的所有元素添加到当前集合中
例子:
导入的包:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;//ctrl+shift+o可以导入缺少的包
import java.util.Date;
import java.util.Iterator;
import org.junit.Test;
/*--------------------------------------------------------------*/
public class javajihe {
@Test
public void testCollection1(){
Collection coll = new ArrayList();
//size();返回集合中的元素的个数
System.out.println(coll.size()); //Alt+/快速敲出输出语句
//add(object obj)
coll.add(123);
coll.add("AA");
coll.add(new Date());
coll.add("B");
System.out.println(coll.size());
//addAll(collection coll)将形参coll中包含的所有元素添加到当前集合中
Collection coll1 = Arrays.asList(1,2,3);
coll.addAll(coll1);
System.out.println(coll.size());
//查看集合元素
System.out.println(coll);
//isEmpty():判断集合是否为空,空为true
System.out.println(coll.isEmpty());
//clear():清空集合元素
coll.clear();
System.out.println(coll.isEmpty());
}
}
/*---------------------------------------------------------------------*/
@Test
public void testCollection2() {
//创建一个coll的对象
Collection coll = new ArrayList();
coll.add(123);
coll.add("AA");
coll.add(new Date());
coll.add("B");
coll.add(new Person("WW",24));
//创建一个coll1的对象
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add("AA");
// removeAll(Collection coll):从当前集合中删除包含coll中的元素
coll.removeAll(coll1);
System.out.println(coll);
//equals(Object obj):判断集合中的所有元素是否完全相同
//hashcode():算出该集合的哈希值
System.out.println(coll.hashCode());
//toArray():将集合转换为数组
Object[] obj = coll.toArray();
for(int i= 0;i<obj.length;i++)
{
System.out.println(obj[i]);
}
// iterator():实现集合的遍历,返回一个Iterator接口实现类的对象
Iterator iterator = coll.iterator();
while(iterator.hasNext()){
System.out.println(iterator.next());
}
}
/*---------------------------------------------------------------------*/
@Test
public void testcollection3() {
Collection coll = new ArrayList();
coll.add(123);
coll.add("AA");
coll.add(new Date());
coll.add("B");
//将一个对象存入集合中
coll.add(new Person("WW",24));
System.out.println(coll);
//contains(object obj):判断当前集合中是否包含指定obj元素,如果包含,返回true,否则返回false
// 判断依据:根据元素所在类的equals()方法进行判断
//如果存入集合中的元素是自定义类的,要求自定义类要重写equals()方法
boolean b =coll.contains(123);
b = coll.contains("AA");
System.out.println(b);
boolean b1 = coll.contains(new Person("WW",24));
System.out.println(b1);
//containsAll(Collection coll):判断当前集合中是否包含coll中的元素
Collection coll1 = new ArrayList();
coll1.add(123);
coll1.add("AA");
boolean b2 = coll.containsAll(coll1);
System.out.println("#" + b2);
//retainAll(Collection coll):求当前集合与coll的共有的元素,返回给当前集合
coll.retainAll(coll1);
System.out.println(coll);
//remove(Object obj):删除集合中的obj元素,若删除成功,返回true,否则返回false
boolean b3 = coll.remove("BB");
System.out.println(b3);
}
Person类的定义:
public class Person {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Person(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Person [name=" + name + ", age=" + age + "]";
}
补充:增强for循环:
例子:
for(String e : str){
// e = "mm"这个e是新定义的局部变量,其值的修改不会对str本身造成影响
//}
四.ArrayList的深入
1.ArrayList:List的主要实现类
2.List常用的方法:
增(add(Object obj)),
删(remove)
改(set(int index,Object obj))
查(get(int index))
插(add(int index,Object ele))
长度(size())
例子:
import java.util.ArrayList;
import java.util.List;
import org.junit.Test;
public class TestList {
@Test
public void testList1(){
List list = new ArrayList();
list.add(123);
list.add(45);
list.add(new String("AA"));
System.out.println(list);
//在指定的索引位置index添加元素ele
list.add(0, 555);//参数一为想要插入的位置,参数二想要插入的数据
System.out.println(list);
//取出指定位置的元素
Object obj = list.get(1);
System.out.println(obj);
//删除指定位置的元素
list.remove(0);
System.out.println(list);
//设置指定位置的元素为ele
list.set(0, "ads");
System.out.println(list);
}
@Test
public void testList2() {
List list = new ArrayList();
list.add(123);
list.add(45);
list.add(new String("AA"));
list.add(45);
//indexof()返回obj在集合中首次出现的位置,没有的话,返回-1
//lastIndexof()返回obj在集合中最后一次出现的位置,没有的话,返回-1
System.out.println(list.indexOf(45));
System.out.println(list.lastIndexOf(45));
System.out.println(list.indexOf(123)==list.lastIndexOf(123));
System.out.println(list.indexOf(789));
//返回从fromIndex到toIndex结束的左闭右开一个子list
List list1 = list.subList(0, 3);
System.out.println(list1);
}
}