点击勘误issues (opens new window),哪吒感谢大家的阅读

java进阶:常用类;异常处理;集合;json及其解析;io流

# String类常用方法

length; substring; compareTo; indexOf; lastIndexOf; split; charAt; matches

# jdk api 文档

# 正则表达式

类 Pattern

public final class Pattern
extends Object
implements Serializable
1
2
3
. 任何字符
\d 数字:[0-9]
\D 非数字:[^0-9]
\s 空白字符:[\t\n\x0B\f\r]
\S 非空白字符:[^\s]
\w 单词字符:[a-zA-Z_0-9]
\W 非单词字符:[^\w]
1
2
3
4
5
6
7
public class Test {
 public static void main(String[] args) {
  String str1 = "ab23lsl345mol";
  String[] strArr = str1.split("\\d{2,3}");
  System.out.println(Arrays.toString(strArr));
  
  String name = '2343lsdf';
  if(name.matches("[a-zA-Z0-9_]")) {}
 }
}
1
2
3
4
5
6
7
8
9
10

# Integer

Java中的数据类型:

  1. 基本数据类型:

byte, short, int, long, float, double, char, boolean

  1. 基本数据类型对应的包装类:

Byte, Short, Integer, Long, Float, Double, Character, Boolean

  1. 引用数据类型:类,接口,数组,枚举等都是引用数据类型

Integer: 常用:

  1. 获取int类型的最大最小值
  2. 获取十进制整数对应的二进制,八进制,十六进制的字符串形式
  3. 将字符串转为int类型

# 异常简介

不正常的情况:

  1. 运行时异常
  2. 非运行异常

# try-catch基础用法

String s1 = null;
try {
 char ch = s1.charAt(0);
 System.out.println(ch);
} catch(Exception e) {
 e.printStackTrace();
}
1
2
3
4
5
6
7
package ...;

public class Test {
 public static void main(String[] args) {
  String s1 = null;
  try {
   // 放可能会发生异常的代码
   char ch = s1.charAt(0); // 向上抛出一个NullPointerException的对象
   System.out.println(ch);
  } catch (Exception e) {
   // 当try中抛出异常时,并处理。Exception e = new NullPointerException();
   e.printStackTrace(); // 打印异常的堆栈信息
  }
  System.out.println("其他的代码")
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16

# try-catch-finally完整用法

// 检测时异常(非运行时异常)必须进行异常的处理
public class TestNullPointerException {
 public static void main(String[] args) {
  try {
   FileInputStream is = new FileInputStream(new File("E:/a.txt"));
  } catch (FIleNotFoundException e) {
   e.printStackTrace();
  }
 }
}
1
2
3
4
5
6
7
8
9
10
public class Test() {
 public static void main(String[] args) {
  FileInputStream is = null;
  try {
   is = new FileInputStream(new File("E:/a.txt"))
  } catch (FileNotFoundException e) {
   e.printStackTrace();
  } finally {
   // 指肯定会执行的代码
   try {
    is.close();
   } catch (IOException e) {
    e.printStackTrace();
   }
  }
 }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

# throws

throws 向上抛异常
1

# catch 和 finally 补充说明

# 异常总结

# 集合概述

集合,是存储数据的容器,存储类型不同,个数不同

# ArrayList的添加修改和删除

public class Test {
 public static void main(String[] args) {
  // 1.定义ArrayList
  ArrayList list = new ArrayList();
 }
}
1
2
3
4
5
6

# ArrayList根据元素的equals方法来删除元素

# ArrayList中的查询和泛型

上次更新: 3/1/2022, 7:25:12 AM