当你清楚的知道自己想要什么,并且意愿非常强烈的时候,你总会有办法得到的。
- 异常
- 数组
- 常用类
- String
一、异常
1、异常概念
程序没有按照自己预想的结果运行出来,出现了非正常情况
2、异常的分类
- 异常
- 异常
- 运行时异常
- 逻辑不够严谨出现的异常,可以通过增强代码的健壮性解决
- 检查时异常
- 必须要处理的异常
- 运行时异常
- 错误
- 系统出现的问题,自己无法解决
- 异常
3、异常处理
1、异常产生
内部抛出了异常,这个异常为系统产生,或手动抛出的异常
2、捕获
public class Test1 {
public static void main(String[] args) {
try {
int a = 1/0;
System.out.println("上面发生了异常,我将不在执行");
}catch (ArithmeticException e){
System.out.println(e.getMessage());
}
System.out.println("啊哈哈哈");
}
}
运行结果 |
---|
/ by zero 啊哈哈哈 |
这是一个除数为0,然后的到的异常
如果出现异常,则后面的语句将不在执行
3、抛出
public class Test2 {
public static void main(String[] args) {
try {
a1();
System.out.println("上面出现异常,我将不会再执行");
}catch (NullPointerException e){
System.out.println("异常--->"+e.getMessage());
}
System.out.println("啊哈哈哈");
}
static void a1() throws NullPointerException{
// 抛出异常
Object o = null;
o.toString();
}
}
运行结果 |
---|
异常—>null 啊哈哈哈 |
这是一个空指针异常
throws:抛出异常,抛给调用者,谁调用谁处理
4、自定义异常
public class MyException extends Exception{
public MyException() {
}
public MyException(String message) {
super(message);
}
}
public class Test3 {
public static void main(String[] args) {
try {
a1(3);
}catch (MyException e){
System.out.println(e.getMessage());
}
}
static void a1(int i) throws MyException{
if (i%2!=0){
throw new MyException(i+"不是一个偶数");
}
System.out.println(i+"是偶数");
}
}
运行结果 |
---|
3不是一个偶数 |
自定义异常,如果不是偶数,则抛出一个自定义异常
自定义异常一般继承Exception
类
4、finally
public class Test1 {
public static void main(String[] args) {
try {
int a = 1/0;
System.out.println("上面发生了异常,我将不在执行");
}catch (ArithmeticException e){
System.out.println(e.getMessage());
}finally {
System.out.println("有没有异常,我都会执行");
}
System.out.println("啊哈哈哈");
}
}
运行结果 |
---|
/ by zero 有没有异常,我都会执行 啊哈哈哈 |
在最后添加了finally
则,有没有异常,语句块中的语句都会被执行
二、数组
1、概念
就是相同数据类型的元素按一定顺序排列的集合,就是把有限个类型相同的变量用一个名字命名,然后用编号区分他们的变量的集合,这个名字称为数组名,编号称为下标。组成数组的各个变量称为数组的分量,也称为数组的元素,有时也称为下标变量。数组是在程序设计中,为了处理方便,把具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组 “[]”。
2、特点
- 长度必须是确定的
- 长度不可以改变
- 元素必须是相同类型
- 可以是任意类型(基本类型和引用类型)
- 数组属于引用类型
3、创建和初始化
3.1、声明数组
type[] 数组变量名; // 推荐使用这种
type 数组变量名[];
3.2、 创建数组
3.2.1、方式一
type[] 数组变量名 = new type[长度];
3.2.2、方式二
type[] 数组变量名 = new type[]{
元素1,元素2,元素3...};
3.2.3、方式三
方式三是简化后的
type[] 数组变量名 = {
元素1,元素2,元素3...};
4、数组的便利
4.1、方式一
public class Test {
public static void main(String[] args) {
int[] a = new int[5];
for (int i = 0;i<5;i++){
System.out.println(a[i]);
}
}
}
4.2、方式二
public class Test {
public static void main(String[] args) {
int[] a = new int[5];
for (int i:a){
System.out.println(i);
}
}
}
5、数组的界限
-
必须开辟空间才能使用否则出现
- java.lang.NullPointerException
-
索引的范围 [0,length) 否则出现
- java.lang.ArrayIndexOutOfBoundsException
-
长度:[0,∞)
- 0 表示空数组,确定了存放的数据类型,该数组不能直接使用,如果使用
- java.lang.ArrayIndexOutOfBoundsException
- -1:编译通过,运行错误
- java.lang.NegativeArraySizeException
6、数组方法
6.1、数组的长度
public class Test {
public static void main(String[] args) {
int[] a = new int[5];
System.out.println(a.length);
}
}
运行结果 |
---|
5 |
6.2、数组比较
6.2.1、相等比较
public class Test {
public static void main(String[] args) {
int[] a = new int[5];
int[] b = new int[5];
System.out.println(a.equals(b));
}
}
运行结果 |
---|
false |
6.2.2、比较
public class Test {
public static void main(String[] args) {
int[] a = new int[5];
int[] b = new int[5];
System.out.println(Arrays.equals(a,b));
}
}
运行结果 |
---|
true |
6.3、排序
public class Test {
public static void main(String[] args) {
int[] a = {
5,1,9,3,7,6};
Arrays.sort(a);
for (int i:a){
System.out.print(i+"\t");
}
}
}
运行结果 |
---|
1 3 5 6 7 9 |
6.4、数组打印
public class Test {
public static void main(String[] args) {
int[] a = {
5,1,9,3,7,6};
System.out.println(Arrays.toString(a));
}
}
运行结果 |
---|
[5, 1, 9, 3, 7, 6] |
6.5、数组拷贝
6.5.1、方式一
public class Test {
public static void main(String[] args) {
int[] a = {
5,1,9,3,7,6};
int[] b = Arrays.copyOf(a,3);
System.out.println(Arrays.toString(b));
}
}
运行结果 |
---|
[5, 1, 9] |
6.5.2、方式二
public class Test {
public static void main(String[] args) {
int[] a = {
5,1,9,3,7,6};
int[] b = new int[3];
System.arraycopy(a,0,b,0,3);
System.out.println(Arrays.toString(b));
}
}
运行结果 |
---|
[5, 1, 9] |
void System.arraycopy(源数组,源起始索引,目标数组,目标起始索引,长度)
7、二维数组
数组中的数组
public class Test {
public static void main(String[] args) {
int[][] a = new int[5][5];
}
}
三、常用类
1、字符相关类
- String
- 不可变字符序列
- StringBuffer
- 可变字符序列,线程安全,效率低
- StringBuilder(一般用它)
- 可变字符序列,线程不安全,效率高
1.1、String
1.1.1、构造器
String str1 = new String(); // 创建一个空字符串对象
System.out.println(str1);
String str2 = new String("啊哈哈哈"); // 创建一个字符串
System.out.println(str2);
String str3 = new String(new char[]{
'啊','哈','哈','哈'}); // 用字符数组创建一个String对象
System.out.println(str3);
String str4 = new String(new char[]{
'啊','哈','哈','哈'},1,3);// 用从字符数组的指定开始位置到指定结束位置,创建一个String字符串对象
System.out.println(str4);
byte[] bytes = str2.getBytes();
String str5 = new String(bytes); // 用byte数字创建一个字符串对象
System.out.println(str5);
String str6 = new String(bytes,"UTF-8"); // 用byte数组创建一个字符串对象,并指定编码
System.out.println(str6);
String str7 = new String(bytes,3,9); // 用byte数组指定的开始位置和指定的结束位置,创建一个字符串
System.out.println(str7);
1.1.2、常用方法
String str8 = new String("Hello Java");
System.out.println(str8.charAt(4)); // 返回字符串指定位置的字符,返回char类型
System.out.println(str8.length()); // 获取字符串的长度,返回int类型
System.out.println(str8.indexOf("l")); // 返回字符串第一次出现'l'的位置,返回int类型
System.out.println(str8.indexOf("a",8)); // 返回从指定位置后的第一次出现'a'的位置,返回int类型
System.out.println("ABC".equalsIgnoreCase("abc")); // 字符串内容比较(忽略大小写),返回boolean类型
str8 = str8.replace(" ","_"); // 字符内内容替换,把" "替换为"_",返回一个新String字符串
System.out.println(str8);
System.out.println(str8.startsWith("Hel")); // 判断字符串是否以指定内容开头,返回boolean类型
System.out.println(str8.startsWith("o",4)); // 判断从指定字符串索引位置开始的子字符串是否以指定内容开头,返回boolean类型
System.out.println(str8.endsWith("eva")); // 判断字符串是否以指定内容结尾,返回boolean类型
String str9 = str8.toUpperCase(); // 返回字符串的大写形式,返回一个新的字符串
System.out.println(str9);
str9 = str9.toLowerCase(); // 返回字符串的小写形式,返回一个新的字符串
System.out.println(str8.substring(3)); // 返回字符串指定索引之后的内容,返回一个新的字符串
System.out.println(str8.substring(3,6)); // 返回字符串指定开始和结束索引之间的内容,返回一个新字符串
str9 = " Hello_World ";
System.out.println(str9.trim()); // 删除字符串首尾的空格,返回一个新的字符串
System.out.println(String.valueOf(true)); // 返回boolean类型的字符串形式
System.out.println(String.valueOf('哈')); // 返回char字符的字符串形式
char[] chars = {
'啊','哈','哈','哈'};
System.out.println(String.valueOf(chars)); // 返回char字符数组的字符串形式
System.out.println(String.valueOf(chars,1,3)); // 返回char字符数组的指定索引之间的字符数组的字符串形式
System.out.println(String.valueOf(3.1415)); // 返回double类型数值的字符串形式
System.out.println(String.valueOf(3.14F)); // 返回float类型数值的字符串形式
System.out.println(String.valueOf(3)); // 返回int类型数值的字符串形式
System.out.println(String.valueOf(123456789L)); // 返回long类型数值的字符串形式