目录
一、异常
概述:程序出现了不正常的情况
Throwable是Java语言中所有的错误和异常的超类。
Error:严重问题,不需要处理。
Exception:称为异常类,它表示程序本身可以处理的问题
• RuntimeException:在编译期是不检查的,出现问题后,需要我们回来修改代码
• 非RuntimeException:编译期就必须处理的,否则程序不能通过编译,就更不能正常运行了
1、JVM的默认处理方案
1-把异常的名称,异常的原因以及异常的出现位置等信息输出在了控制台
2-程序停止运行
2、异常处理
自己解决的两个方法:
1使用try...catch方法
try...catch格式:
try{
可能出现异常的代码;
} catch(异常类名 变量名){
异常的处理代码;
}
package myException;
public class ExceptionDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() {
try {
int arr[]= {1,2,3};
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e) {
System.out.println("访问的数组不存在");
e.printStackTrace();
}
}
}
输出:
2、使用Throwable的成员方法
package myException001;
public class ExceptionDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println("开始");
method();
System.out.println("结束");
}
public static void method() {
try {
int arr[]= {1,2,3};
System.out.println(arr[3]);
}catch (ArrayIndexOutOfBoundsException e) {
//System.out.println("访问的数组不存在");
//e.printStackTrace();
System.out.println(e.getMessage());
//Index 3 out of bounds for length 3
//输出错误原因
System.out.println(e.toString());
//java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
//输出异常类名以及异常原因
e.printStackTrace();
/*
java.lang.ArrayIndexOutOfBoundsException: Index 3 out of bounds for length 3
at myException001.ExceptionDemo.method(ExceptionDemo.java:14)
at myException001.ExceptionDemo.main(ExceptionDemo.java:8)
*/
//输出异常类名,异常原因,异常代码的位置
}
}
}
3、编译时异常与运行时异常区别
也成为受检异常和非受检异常
编译时异常----必须显式处理,否则程序就会发生错误,无法通过编译
运行时异常----无需显示处理,也可以和编译时异常一样处理。
package myException002;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
public class ExceptionDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
method1();
method2();
}
//运行时异常
public static void method1() {
try {
int [] arr= {1,2,3};
System.out.println(arr[3]);
}catch(ArrayIndexOutOfBoundsException e) {
e.printStackTrace();
}
}
//编译异常--必须处理,否则无法通过编译
public static void method2() {
try {
String s="2004-06-08";
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd");
Date d=sdf.parse(s);
System.out.println(d);
}catch(ParseException e) {
e.printStackTrace();
}
}
}
4、throws异常处理
格式:throws 异常类名;
*该格式跟在方法的括号后。
编译时异常必须、要进行处理,如果采用throws这种方案,谁调用谁处理。(可以用tr...catch处理)。
5、自定义异常
格式:
public class 异常类名 extends Exception{
无参构造
带参构造
}
如下:
package myException003;
public class ExceptionDemo extends Exception{
public ExceptionDemo() {}
public ExceptionDemo(String message) {
super(message);
}
}
6、throw和throws的区别
throws
•用在方法声明后面,跟的是异常类名
•表示抛出异常,由该方法的调用者来处理
•表示出现异常的一种可能性,并不一定会发生这些异常
throw
•用在方法体内,跟的是异常对象名
• 表示抛出异常,由方法体内的语句处理
• 执行 throw 一定抛出了某种异常
二、集合
1、集合类体系结构
collection(单列):
可重复----List;--->ArrayList、LinkList....
不可重复----Set;---->HashSet、TreeSet....
Map(双列):
---->HashMap....
2、collection集合
概述:
是单例集合的顶层接口,它表示一组对象,这些对象也成为Collecttion的元素。
JDK不提供此接口的任何实现,它提供更具体的子接口(如Set和List)实现。
创建Collection集合的对象:
多态的方式。
具体的实现类ArrayList。
Collection集合常用方法
package myCollection001;
import java.util.ArrayList;
import java.util.Collection;
public class CollectionDemo02 {
public static void main(String[] args) {
Collection<String> c=new ArrayList<String>();
System.out.println(c.add("hello"));
System.out.println(c.add("world"));
//boolean类型,永远返回true
System.out.println(c.remove("world"));
//boolean类型,有该元素将会删除,返回true;无该元素,返回false,不删除任何元素
c.clear();
//void返回类型
System.out.println(c.contains("world"));
//boolean类型,查询集合中是否有该元素
System.out.println(c.isEmpty());
System.out.println(c.size());
}
}
Collection集合的遍历
Iterator:迭代器,集合的专用遍历方式。
Iterator <E> iterator():返回此集合中元素的迭代器,通过集合的iterator()方法得到。
迭代器是通过集合的iterator()方法得到的,所以我们说它是依赖于集合而存在的。
E next():返回迭代中的下一个元素。
boolean hasNext():如果迭代具有更多的元素,返回true。
3、List集合
概述:
*有序集合,用户可以准确控制列表中每个元素的插入位置。用户可以通过整数索引访问元素。并搜索列表中的元素。
*与Set集合不同,列表通常允许重复的元素。
特点:
*有序----存储和取出顺序一致。
*可重复----存储的元素可以重复。
1、List集合特有方法
package myCollection002;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class ListDemo {
public static void main(String[] args) {
// TODO Auto-generated method stub
List<String> list=new ArrayList<String>();
list.add("hello");
list.add("world");
list.add("java");
list.add("world");
System.out.println(list);
//可重复
Iterator<String> it= list.iterator();
/* while (it.hasNext()) {
String s=it.next();
System.out.println(s);
}
*/
list.add(1,"java1");
System.out.println(list);
//指定位置添加元素
System.out.println(list.remove(2));
System.out.println(list);
//删除指定位置的元素,返回删除的元素
System.out.println(list.set(1,"java2"));
//输出被修改的元素 修改指定位置的元素
System.out.println(list);
System.out.println(list.get(1));
//获取指定位置元素
}
}
2、List并发修改异常
产生原因:迭代器遍历的过程中,通过集合对象修改了集合中元素的长度,造成了迭代器获取元素中判断预期修改值和实际修改值不一致。
解决方法:用for循环遍历,然后用集合对象做对应操作即可。
3、ListIterator
列表迭代器----通过ListIterator()方法得到,是List集合特有的迭代器。用于程序员沿任意方向遍历列表的列表迭代器,在迭代期间修改列表,并获取列表中迭代器的当前位置。
常用方法:
E next();返回迭代中的下一个元素
boolean hasNext();如果迭代具有更多元素,则返回true
E previous();返回列表中的上一个元素
boolean hasPrevious();如果此列表迭代器在相反方向遍历列表时具有更多元素,则返回true
void add(E e);将指定的元素插入列表
4、加强for循环
简化数组和Collection集合的遍历。
*增强Iterable接口的类允许其对象成为增强型for语句的目标。
*内部原理是一个Iterator迭代器。
增强for的格式
for (元素数据类型 变量名:数组或Collection集合){
在此处使用变量即可,该变量就是元素。
}
int []arr={1,2,3,4,5};
for(int i:arr){
System.out.println(i);
}
选择:需要索引时使用普通for;单纯遍历的时候使用增强for。
5、List集合子类特点
常用子类:ArrayList、LinkedList
ArrayList:底层数据结构是数组,查询快,增删慢。
LinkedList:底层数据结构是链表,查询慢,增删快。
6、LinkedList集合特有功能
package myCollection002;
import java.util.LinkedList;
public class CollectionDemo002 {
public static void main(String[] args) {
// TODO Auto-generated method stub
LinkedList<String> ll=new LinkedList<String>();
ll.add("hello");
ll.add("world");
ll.add("java");
ll.addFirst("my");
System.out.println(ll);
//将元素添加到列表开头中
ll.addLast("java01");
System.out.println(ll);
//将元素添加到列表结尾中
System.out.println(ll.getFirst());
//得到列表中第一个元素
System.out.println(ll.getLast());
//得到列表中最后一个元素
System.out.println(ll.removeFirst());
System.out.println(ll);
//删除列表中第一个元素,并返回第一个元素
System.out.println(ll.removeLast());
System.out.println(ll);
//删除列表中最后一个元素,并返回最后一个元素
}
}
4、set集合
特点:
不包含重复元素的集合。
没有带索引的方法,所以不能使用普通的for循环遍历。
1、HashSet
对集合的迭代顺序不做任何保证。
1、哈希值
JDK根据对象的地址或字符串或者数字算出来的int类型的数值。
Object类中有方法获取对象的哈希值----public int hashCode();
package myCollection003;
import java.net.MulticastSocket;
public class HashSetDemo {
public static void main(String[] args) {
Person p1=new Person("小明",18);
System.out.println(p1.hashCode());
System.out.println(p1.hashCode());
//同一个对象多次调用hashCode()返回值是相同的
System.out.println("------------");
Person p2=new Person("小明",18);
System.out.println(p2.hashCode());
//默认情况下,不同对象的哈希值是不相同的
//通过方法重写可以实现不同对象的哈希值相同
}
}
特点:
默认情况下,不同对象的哈希值是不相同的
通过方法重写可以实现不同对象的哈希值相同
2、HashSet集合特点
*底层数据结构是哈希表。
*对集合的迭代顺序不做任何保证,也就是说不保证存储和取出的元素顺序一致。
*没有带索引的方法,所以不能使用普通for循环遍历。
*由于是Set集合,所以是不包含重复元素的集合。--->要保证元素唯一性,需要重写hashCode()和equals()。
2、LinkedHashSet
特点
*哈希表和链表实现的set接口,具有可预测的迭代次序。
*由链表保证元素有序,也就是说元素的存储和取出顺序是一致的。
*由哈希表保证元素唯一。
3、TreeSet
特点:
*元素有序,这里的顺序不是指存储和取出的顺序,而是按照一定的顺序进行排序,具体排序方式取决于构造方法-------TreeSet();根据其元素的自然排序进行排序。TreeSet(Comparator comparator);根据指定的比较器进行排序。
*没有带索引的方法,所以不能使用普通for循环遍历。
*由于是Set集合,所以不包含重复元素。
4、自然排序Comparable
用TreeSet集合存储自定义对象,无参构造方法使用的是自然排序对元素进行排序的。
自然排序,就是让元素所属的类实现Comparable接口,重写compareTo(T o)方法。
重写方法时,一定要注意排序规则必须按照要求的主要条件和次要条件来写。
package myComparable001;
public class Person implements Comparable<Person>{//实现该接口
String name;
int age;
Person(String name,int age){
this.age=age;
this.name=name;
}
@Override
public int compareTo(Person p) {
//compareTo(T o)重写
// TODO Auto-generated method stub
int num1=this.age-p.age;
//主条件
int num2=num1==0?this.name.compareTo(p.name):num1;
//次条件
return num2;
}
}