[TOC]
复习
异常处理
用于承载方法运行过程中出现的异常问题,异常类中有异常信息的简要描述,异常出现的方法执行流程,代码执行流程,异常使用可以用于后期的日子处理,操作日志,错误日志,也会对异常的等级进行分类 info 信息 Warning 警告 Danger 危险
对于目前而言,也就是 Ctrl + 1 显示异常,目前没有日志操作。
Throwable
---| Exception extends Throwable
异常 可以处置,可以处理
---| Error extends Throwable
错误 只能避免
String toString();
String getMessage();
void printStactTrace();
异常处理方式
1. 捕获
try {
} catch (异常类型 异常变量) {
异常的处理方式
}
2. 抛出
throw
方法内部条件判断之后抛出指定类型的异常
throws
方法的声明位置告知调用者当前方法有异常抛出
运行时异常和编译时异常
项目中的排序算法:
数据保护原则
字符串和集合
1. 字符串类型
字符串是对于程序员而言非常重要的信息载体,稳定,兼容,数据量大,可以自定义内容信息方式......
1.1 字符串存储问题
package com.qfedu.a_string;
/*
* 字符串存储和比较
*/
public class Demo1 {
public static void main(String[] args) {
String str1 = "郑州大雾";
String str2 = "郑州大雾";
String str3 = new String("郑州大雾");
String str4 = new String(str1);
System.out.println("str1 == str2: " + (str1 == str2));
System.out.println("str2 == str3: " + (str2 == str3));
System.out.println("str3 == str4: " + (str3 == str4));
System.out.println("str4 == str1: " + (str4 == str1));
/*
* Java 中规定,英文双引号""包含的内容在整个程序中独一份,是一个常量
* 代码中看到的是是哪个字符串"郑州今天大雾"实际上对于程序是一个字符串
*
* == 判断对于引用数据类型而言比较的内容不是指向空间内容,是引用数据类型存储的地址数据
*
* 字符串如果想要比较内容判断是否一致,需要使用其他方法
*/
}
}
/*
* 【注意】
* 【强烈要求】字符串比较是否一致,必须使用 equals 方法
*/
System.out.println("str1.equals(str2) : "+ str1.equals(str2));
System.out.println("str2.equals(str3) : "+ str2.equals(str3));
System.out.println("str3.equals(str4) : "+ str3.equals(str4));
System.out.println("str4.equals(str1) : "+ str4.equals(str1));
API 学会学习 API 文档,在工作中给你一份 API 文档,对于你的伙伴,你的上司而言,他们就认为当前这个技能你已经掌握
1.2 字符串操作相关方法
1.21 字符串获取方法
int length();
字符串有效字符个数
char charAt(int index);
获取当前字符串指定下标对应的字符数据。
int indexOf(int ch);
查询指定字符在当前字符串中第一次出现的下标位置,如果没有返回 -1
int lastIndexOf(int ch);
查询指定字符在当前字符串中最后一次出现的下标位置,如果没有返回 -1
int indexOf(String str);
查询指定字符串在当前字符串中第一次出现的下标位置,如果没有返回 -1
int lastIndexOf(String str);
查询指定字符串在当前字符串中最后一次出现的下标位置,如果没有返回 -1
补充知识点
int ch ==> indexOf 方法对应的参数格式,之所以用 int 类型是考虑到字符有编码值情况,同时 ch 参数变量名称告知/提示程序员当前的实际类型,参与代码运行的类型是字符类型
package com.qfedu.a_string;
/*
int length();
字符串有效字符个数
char charAt(int index);
获取当前字符串指定下标对应的字符数据。
int indexOf(int ch);
查询指定字符在当前字符串中第一次出现的下标位置,如果没有返回 -1
int lastIndexOf(int ch);
查询指定字符在当前字符串中最后一次出现的下标位置,如果没有返回 -1
int indexOf(String str);
查询指定字符串在当前字符串中第一次出现的下标位置,如果没有返回 -1
int lastIndexOf(String str);
查询指定字符串在当前字符串中最后一次出现的下标位置,如果没有返回 -1
*/
public class Demo2 {
public static void main(String[] args) {
String str = "ABCDEFGABCDEFG";
System.out.println("length: " + str.length());
System.out.println("charAt: " + str.charAt(5));
System.out.println("charAT: " + str.charAt(8));
System.out.println();
/*
* java.lang.StringIndexOutOfBoundsException: String index out of range: 28
* 字符串下标越界
*
* System.out.println("charAT: " + str.charAt(28));
*/
System.out.println("indexOf: " + str.indexOf('A'));
System.out.println("indexOf: " + str.indexOf(66));
System.out.println("indexOf: " + str.indexOf("B"));
System.out.println("indexOf: " + str.indexOf(67));
System.out.println();
System.out.println("indexOf: " + str.lastIndexOf('A'));
System.out.println("indexOf: " + str.lastIndexOf(65));
System.out.println("indexOf: " + str.lastIndexOf('B'));
System.out.println("indexOf: " + str.lastIndexOf(66));
System.out.println();
System.out.println("indexOf: " + str.indexOf("BCD"));
System.out.println("indexOf: " + str.indexOf("bcd"));
System.out.println("indexOf: " + str.lastIndexOf("BCD"));
System.out.println("indexOf: " + str.lastIndexOf("bcd"));
}
}
1.22 字符串判断方法
boolean endsWith(String str);
判断当前调用方法字符串是否已参数字符串结尾
boolean startsWith(String str);
判断当前调用方法字符串是否已参数字符串开头
boolean isEmpty();
判断字符串是否为空,JDK1.6之后版本空字符串为""
boolean contains(String str);
判断当前调用方法字符串是否包含参数目标字符串
boolean equal(Object obj) // Override equals method in Object 重写 Object 方法
判断当前调用方法字符串和参数字符串是否内容一致
boolean equalsIgnoreCase(String str);
判断当前调用方法字符串和参数字符串,在不区分大小写比较字符串内容
package com.qfedu.a_string;
/*
* boolean endsWith(String str);
判断当前调用方法字符串是否已参数字符串结尾
boolean startsWith(String str);
判断当前调用方法字符串是否已参数字符串开头
boolean isEmpty();
判断字符串是否为空,JDK1.6之后版本空字符串为""
boolean contains(String str);
判断当前调用方法字符串是否包含参数目标字符串
boolean equal(Object obj) // Override equals method in Object 重写 Object 方法
判断当前调用方法字符串和参数字符串是否内容一致
boolean equalsIgnoreCase(String str);
判断当前调用方法字符串和参数字符串,在不区分大小写比较字符串内容
*/
public class Demo3 {
public static void main(String[] args) {
String str = "ABCDEFG";
System.out.println("endsWith: " + str.endsWith("FG") );
System.out.println("endsWith: " + str.endsWith("FF") );
System.out.println();
System.out.println("startsWith: " + str.startsWith("AB") );
System.out.println("startsWith: " + str.startsWith("ab") );
System.out.println();
System.out.println("isEmpty: " + str.isEmpty());
System.out.println("isEmpty: " + "".isEmpty());
System.out.println();
System.out.println("contains: " + str.contains("AA") );
System.out.println("contains: " + str.contains("BC") );
System.out.println();
System.out.println("equals: " + str.equals("ABCDEFG"));
System.out.println("equals: " + "ABCDEFG".equals("str")); // 推荐容量在前调用方法,避免错误
System.out.println();
System.out.println();
System.out.println("equalsIgnoreCase: " + str.equalsIgnoreCase("abcdefg"));
System.out.println("equalsIgnoreCase: " + "ABCDEFG".equalsIgnoreCase(str) );
}
}
空字符串"" 有什么用:
Java 中任意数据类型想要变成字符串类型都可以 + ""
String str = 3 + ""; str ==> "3"
'A' + "" ==> "A"
true +"" ==> "true"
false +"" ==> "false"
1.23 字符串转换方法
String(char[] arr);
【构造方法】 将一个字符数组转换为字符串
String(char[] arr, int offset, int length);
【构造方法】 将一个字符数组从指定下标 offset 开始,计数字符个数 length,转换为一个字符串
static String valueOf(char[] arr);
【静态方法】 将一个字符数组转换为字符串
static String valueOf(char[] arr, int offset, int length);
【静态方法】 将一个字符数组从指定下标 offset 开始,计数字符个数 length,转换为一个字符串
char[] toCharArray();
字符串数据内容转字符数组
package com.qfedu.a_string;
import java.util.Arrays;
/*
String(char[] arr);
【构造方法】 将一个字符数组转换为字符串
String(char[] arr, int offset, int length);
【构造方法】 将一个字符数组从指定下标 offset 开始,计数字符个数 length,转换为一个字符串
static String valueOf(char[] arr);
【静态方法】 将一个字符数组转换为字符串
static String valueOf(char[] arr, int offset, int length);
【静态方法】 将一个字符数组从指定下标 offset 开始,计数字符个数 length,转换为一个字符串
char[] toCharArray();
字符串数据内容转字符数组
*/
public class Demo4 {
public static void main(String[] args) {
String str = "我是谁,你是谁";
char[] array = str.toCharArray();
System.out.println(Arrays.toString(array));
System.out.println();
String str2 = new String(array);
String str3 = new String(array, 1, 3);
System.out.println(str2);
System.out.println(str3);
System.out.println();
String str4 = String.valueOf(array);
String str5 = String.valueOf(array, 1, 3);
System.out.println(str4);
System.out.println(str5);
}
}
1.24 字符串其他方法
【注意】字符串是一个常量,字符串执行方法原串不变
以下所有方法调用方法的字符串都不变,所有的结果都是通过方法返回值新字符串得到
String replace(char oldchar, char newChar);
调用方法的字符串,指定的 oldChar 字符串替换为 newChar 字符串内容,最终替换结果通过方法返回值返回一个新的字符串
String[] split(String str);
调用方法字符串,根据指定的参数字符串进行分割操作,得到的结果是一个分割之后的字符串数组。
例如:
String str = "name=张三&age=16&gender=f";
// 当前字符串是一串数据信息,可以转换成 JavaBean 对象,当前字符串是有规律和规则
// 信息采用表达式方式为 Key = Value 模式/键值对模式 同时对不同的信息直接使用 & 分割
String[] arr = str.split("&");
// arr = {"name = 张三", "age = 16", "gender = f"};
String substring(int begin);
调用方法字符,从指定下标 begin 位置开始到字符串末尾,截取子字符串。
String substring(int begin, int end);
调用方法字符,从指定下标 begin 位置开始到指定下标 end 位置结束,截取子字符串。【要头不要尾】
String toUpperCase();
调用方法字符串所有的英文字母全部转大写,结果通过返回新字符串得到
String toLowerCase();
调用方法字符串所有的英文字母全部转小写,结果通过返回新字符串得到
String trim();
删除调用方法字符串两端的无效空格,结果通过返回值新字符串得到
package com.qfedu.a_string;
import java.util.Arrays;
/*
String replace(char oldchar, char newChar);
调用方法的字符串,指定的 oldChar 字符串替换为 newChar 字符串内容,最终替换结果通过方法返回值返回一个新的字符串
String[] split(String str);
调用方法字符串,根据指定的参数字符串进行分割操作,得到的结果是一个分割之后的字符串数组。
例如:
String str = "name=张三&age=16&gender=f";
// 当前字符串是一串数据信息,可以转换成 JavaBean 对象,当前字符串是有规律和规则
// 信息采用表达式方式为 Key = Value 模式/键值对模式 同时对不同的信息直接使用 & 分割
String[] arr = str.split("&");
// arr = {"name = 张三", "age = 16", "gender = f"};
String substring(int begin);
调用方法字符,从指定下标 begin 位置开始到字符串末尾,截取子字符串。
String substring(int begin, int end);
调用方法字符,从指定下标 begin 位置开始到指定下标 end 位置结束,截取子字符串。【要头不要尾】
String toUpperCase();
调用方法字符串所有的英文字母全部转大写,结果通过返回新字符串得到
String toLowerCase();
调用方法字符串所有的英文字母全部转小写,结果通过返回新字符串得到
String trim();
删除调用方法字符串两端的无效空格,结果通过返回值新字符串得到
*/
public class Demo5 {
public static void main(String[] args) {
String str = "abcdefg";
String str2 = str.replace('a', 'b');
System.out.println("方法调用之后得到的新字符串:" + str2);
System.out.println("原字符串: " + str);
System.out.println();
String str3 = "name=张三&age=18&gender=f";
String[] arr = str3.split("&");
System.out.println(Arrays.toString(arr));
System.out.println();
String str4 = str.substring(3);
String str5 = str.substring(3, 6);
System.out.println(str4);
System.out.println(str5);
System.out.println();
System.out.println("AaskfjhYTVj".toUpperCase());
System.out.println("AaskfjhYTVj".toLowerCase());
String str6 = " 测试 测试 测试 ";
System.out.println(str6.trim());
System.out.println(str6);
}
}
2. 集合【重点】
2.1 为什么要使用集合
目前而言,对于数据量多个/大量,并且数据类型一致的元素,目前的解决方案是数组
1. 容量固定,扩容麻烦
2. 只能处理/存储一种数据类型
3. 数组配套方法少,使用不方便
4. 因为支持类型有且只能是一种类型,配套容量,配套方法都需要独立完成,无法复用
集合可以解决以上问题
1. 容量不需要调用者/程序员考虑,只管用!!【有扩容】【有缩容】
2. 支持数据类型多样化,但是严格遵从数据类型一致化要求。【有泛型】
3. 配套方法多,并且工具类多,且操作手段多
4. 支持多元类型,方法复用度高,使用方便
2.2 集合类型的整体结构和类型特征分析
interface collection<E>
Java 中所有集合的基类接口,规定了集合操作的基础方法
--| interface List<E>extends Collection<E>
Java 中 List 特征集合接口,继承Collection 接口,同时增加了 List 接口特征方法
特征: 有序,可重复
--| class ArrayList<E> implement List<E>
底层结构为 Object 数据类型,为可变长数组 List 集合, 特征:增删慢,查询块。
--| class LinkedList<E> implement List<E>
底层结构为带有链表头的双向链表结构
--| class Vector<E> implement List<E>
底层结构为 Object 数据类型,为可变长数组 List 集合,线程安全,性能对比 ArrayList 较差 (是ArrayList的祖师爷)
--| interface Set<E>extends Collection<E>
Java 中 List 特征集合接口,遵从Collection 接口,同时增加了 List 接口特征方法
特征: 无序,不可重复
--| class HashSet<E>implement Set<E>
底层结构为 哈希表结构,可以认为是 Excel 表格
--| class TreeSet<E> implement Set<E>
底层结构为 二叉树结构
2.3 collection 集合常用方法
增
add(E e);
添加实例化集合对象过程中,泛型约束对应具体数据类型元素。
addAll(Collection< ? extend E > c);
添加参数集合到当前调用方法集合中,要求参数集合存储的数据类型:
1. 和调用方法集合存储类型一致
2. 调用方法集合存储类型的子类
删
remove(Object obj);
调用方法集合,删除指定元素
removeAll(Collection<?> c);
调用方法集合中,删除和参数集合的交集
retainAll(Collection<?> c);
调用方法集合中,仅保留和参数集合的交集
clear();
清空调用方法集合所有存储元素
查
int size();
集合中有效元素个数
boolean isEmpty();
判断当前集合是否为空
boolean contains(Object obj);
判断参数对象元素是否在当前集合中存在
boolean containsAll(Collection<?> C);
判断参数集合对象是否为当前集合的子集
Object[] toArray();
返回集合中所有存储元素的 Object 类型数组
【补充知识点】
class Animal
class Monkey extends Animal
class Tiger extends Animal
class Lion extends Animal
<? extends E>
有泛型,有继承,? 通配符
如果当前 E ==> 约束为 Animal 类型
<? extends Animal>
要求当前数据类型可以是 Animal 类型或者 Animal 的子类类型
【泛型的上限】
package com.qfedu.b.collection;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
/*
* Collection 集合方法演示
*/
public class Demo1 {
public static void main(String[] args) {
/*
Collection 是一个接口,无法实例化对象,无法调用目标方法,这里使用
Collection 接口的实现类,ArrayList 演示方法
接口的引用指向遵从接口的实现类对象【多态】
*/
Collection<String> c1 = new ArrayList<String>();
c1.add("不知火舞");
c1.add("干将莫邪");
c1.add("金蝉");
c1.add("甄姬");
c1.add("安其拉");
System.out.println(c1);
Collection<String> c2 = new ArrayList<String>();
c2.add("奕星");
c2.add("妲己");
c2.add("姜子牙");
c2.add("貂蝉");
c1.addAll(c2);
System.out.println(c1);
System.out.println(c2);
c1.remove("妲己");
c1.remove("金");
System.out.println(c1);
Collection<String> c3 = new ArrayList<String>();
c3.add("金蝉");
c3.add("火舞");
c3.add("貂蝉");
c3.add("奕星");
//c1.removeAll(c3);
//System.out.println(c1);
//System.out.println(c3);
//c1.retainAll(c3);
//System.out.println(c1);
c3.clear();
System.out.println(c1.size());
System.out.println(c3.size());
System.out.println(c1.isEmpty());
System.out.println(c3.isEmpty());
System.out.println("contains:" + c1.contains("不知火舞"));
System.out.println("contains:" + c1.contains("妲己"));
System.out.println("contains:" + c1.contains("c2"));
System.out.println();
Object[] array = c1.toArray();
Arrays.stream(array).forEach(System.out::println);
}
}
2,4 List 集合特征
List 集合是基于 Collection 集合延伸符合特定需求的一种集合形式,主要引入的模式是【下标模式】,并且延续 Collection 集合规定的方法,增加了符合 List集合特征的方法。
特征:
1. 操作数据有下标形式
2. 数据存储有序,可以重复
有序:数据添加顺序和数据存储顺序一致
可重复:存储元素可以相同
2.5 List 集合常用方法
增
add(E e);
添加实例化集合对象过程中,泛型约束对应具体数据类型元素。
addAll(Collection<? extends E> c);
添加参数结合到当前调用方法集合中,要求参数结合存储的数据类型:
1. 和调用方法集合存储类型一致
2. 调用方法集合存储类型的子类
add(int index, E e);
在当前集合指定下标位置,添加实例化集合到当前调用方法集合中,要求参数集合存储的数据类型:
1. 和调用方法集合存储类型一致
2. 调用方法集合存储类型的子类
删
E remove(int index);
删除当前集合指定下标元素,返回值是被删除的元素对象。
remove(Object obj);
调用方法集合,删除指定元素
removeAll(Collection<?> c);
调用方法集合中,删除和参数集合的交集
retainAll(Collection<?> c);
调用方法集合中,仅保留和参数集合的交集
clear();
清空调用方法集合所有存储元素
改
E set(int index, E e);
在当前集合中指定下标位置,使用实例化集合对象约束的数据类型元素,替换对应元素,返回值是被替换的元素
查
int size();
集合中有效元素个数
boolean isEmpty();
判断当前集合是否为空
boolean contains(Object obj);
判断参数对象元素是否在当前集合中的存在
boolean containAll(Collection<?> c);
判断参数结合对象是否为当前集合的子集
Object[] toArray();]
返回集合中所有存储元素的 Object 类型数组
E get(int index);
在当前集合中,获取指定下标元素
List<E> subList(int fromIndex, int toIndex);
从当前集合中指定 fromIndex 下标开始到 toIndex 下标结束,获取子集合对象,要头不要尾
int indexOf(Object obj);
在当前集合中找出指定元素第一次出现的下标位置
int lastIndexOf(Object obj);
在当前集合中找出指定元素最后一次出现的下标位置
package com.qfedu.b_collection;
import java.util.ArrayList;
import java.util.List;
/*
List 集合特征方法
*/
public class Demo3 {
public static void main(String[] args) {
List<String> list = new ArrayList<String>();
list.add("巫术法杖");
list.add("法帽");
list.add("回响之杖");
list.add("圣杯");
list.add("痛苦面具");
System.out.println(list);
list.add(2, "噬神之书");
System.out.println(list);
List<String> list2 = new ArrayList<String>();
list2.add("闪电匕首");
list2.add("吸血刀");
list2.add("破魔刀");
list2.add("影刃");
list2.add("破军");
list.addAll(4, list2);
System.out.println(list);
System.out.println();
System.out.println(list.get(4));
System.out.println(list.indexof("影刃"));
System.out.println(list.lastIndexOf("手抓饭"));
List<String> subList = list.subList(3, 7);
System.out.println(subList);
}
}
3. 作业
1. 项目进度跟上
2. 今天所有的方法,每一个敲三遍
3. 开始进入方法抄写模式