Java本项目知识基础—下篇
1. String
拼接字符串
- 进行头部、中部、尾部拼接 (简单、内存占用大) StringBuffer或StringBuilder进行append拼接(复杂、内存占用小)
public static void main(String[] args) {
String str = "abc";
//头部
str = "头部字符串"+str;
System.out.println(str);
//尾部
str+="尾部字符串";//str = str+"尾部字符串";
System.out.println(str);
//中部: 需要在a和b之间拼接 “中部123”
String str2 = "中部123";
str2 = "a"+str2+"bc";
System.out.println(str2);
}
字符串常见方法
判断操作:
public static void main(String[] args) {
System.out.println("你好世界".startsWith("你好"));//true
System.out.println("你好世界".startsWith("好"));//false
System.out.println("你好世界".endsWith("世界"));//true
System.out.println("你好世界".endsWith("世"));//false
System.out.println("abc".equals("abc"));//true
System.out.println("abc".equals("Abc"));//false
System.out.println("abc".equalsIgnoreCase("abc"));//true
System.out.println("abc".equalsIgnoreCase("ABc"));//true
System.out.println("你好世界".contains("好世"));//true
System.out.println("你好世界".contains("好界"));//false
System.out.println("".isEmpty());//true
System.out.println(" ".isEmpty());//false
}
对字符串本身进行业务操作:
public static void main(String[] args) {
String str = " hello World ";
System.out.println(str.length());//13
System.out.println(str.trim().length());//11
System.out.println(str.charAt(1));//h
char[] array = str.toCharArray();
System.out.println(Arrays.toString(array));//[ , h, e, l, l, o, , W, o, r, l, d, ]
String str2 = "HelloWorld";
System.out.println(str2.toUpperCase());//HELLOWORLD
System.out.println(str2.toLowerCase());//helloworld
String s1 = str2.substring(5);
System.out.println(s1);//World
String s2 = str2.substring(0, 5);
System.out.println(s2);//Hello
String str3 = "abc#cde#fff";
String[] split = str3.split("#");
System.out.println(Arrays.toString(split));//[abc, cde, fff]
String str4 = "192.168.1.1";
String[] split1 = str4.split("\\.");
System.out.println(Arrays.toString(split1));
String str5 = "HelloabcWorldabc你好abc";
String abc = str5.replaceAll("abc", "");
System.out.println(abc);//HelloWorld你好
//寻找第一个abc的出现位置
int i = str5.indexOf("abc");
System.out.println(i);//5
//寻找最后一个abc的出现位置
int i2 = str5.lastIndexOf("abc");
System.out.println(i2);//18
String str6 = "你";
byte[] bytes = str6.getBytes();
System.out.println(Arrays.toString(bytes));//[-28, -67, -96]
}
2. 日期
Date
java.util.Date
封装当前日期
public static void main(String[] args) throws InterruptedException {
Date d1 = new Date();//封装这个时刻的时间
System.out.println(d1);
System.out.println(d1.toLocaleString());
Thread.sleep(1000);
Date d2 = new Date();
System.out.println(d2.toLocaleString());
}
日期进行移动操作:
毫秒值: 1秒=1000毫秒
获取昨天这个时刻
获取明天这个时刻
获取一个月后这个时刻
public static void main(String[] args) {
Date d1 = new Date();
//获取当前时刻的毫秒值
long t1 = d1.getTime();//System.currentTimeMillis()
//1天的毫秒值
long oneDay = 1000L*60*60*24;
//变为昨天
Date y = new Date(t1 - oneDay);
System.out.println(y.toLocaleString());
//变为明天
Date t = new Date(t1 + oneDay);
System.out.println(t.toLocaleString());
//变为三个月后
Date m = new Date(t1 + oneDay * 90);
System.out.println(m.toLocaleString());
}
3.SimpleDataFormat
字符串转日期
日期转字符串
public static void main(String[] args) throws ParseException {
String str = "2023/09/01 10:10:10";
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date parse = sdf.parse(str);
System.out.println(parse.toLocaleString());
Date date = new Date();
String format = sdf.format(date);
System.out.println(format);//2023/09/07 10:01:08
}
4.Math
需要哪个,就先取Math中进行查找,Math中没有,再来自己进行书写。
5.JSON和Java互转知识
JSON知识
服务器之间交互、服务器和前端交互的 必有数据格式。
格式1:(数组格式)
[元素1,元素2,元素N]
格式2:(对象格式)
{"属性名":"属性值","属性名":"属性值"}
保存几个学生信息:
[
{"name":"小张","sex":"男"},
{"name":"小红","sex":"女"}
]
保存股票列表信息:
{
"basic":"xx股票信息"
"list":[
"2001","2002"
]
}
6. Java转Json
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.17</version>
</dependency>
import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Student {
//JsonProperty 在SpringBoot项目中jackson的指定属性名操作,json就和stuName进行交互了
@JsonProperty("stuName")
//JSONField 在SpringBoot项目中,fastjson的指定属性名操作,json就和stuName进行交互了
//@JSONField(name="stuName")
private String name;
private String sex;
private Double score;
}
public static void main(String[] args) {
ArrayList<Student> slist = new ArrayList<>();
Student s1 = new Student("小明", "男", 100.0);
Student s2 = new Student("小张", "男", 95.0);
Student s3 = new Student("小红", "女", 80.0);
Collections.addAll(slist,s1,s2,s3);
System.out.println(slist);
//数组格式转换
String jsonString = JSONArray.toJSONString(slist);
System.out.println(jsonString);//[{"name":"小明","score":100.0,"sex":"男"},{"name":"小张","score":95.0,"sex":"男"},{"name":"小红","score":80.0,"sex":"女"}]
//对象格式转换
String jsonString2 = JSONObject.toJSONString(s1);
System.out.println(jsonString2);//{"name":"小明","score":100.0,"sex":"男"}
}
7. Json转Java
public static void main(String[] args) {
String json = "[{\"name\":\"小明\",\"score\":100.0,\"sex\":\"男\"},{\"name\":\"小张\",\"score\":95.0,\"sex\":\"男\"},{\"name\":\"小红\",\"score\":80.0,\"sex\":\"女\"}]";
String json2 = "{\"name\":\"小明\",\"score\":100.0,\"sex\":\"男\"}";
Object slist = JSONArray.parse(json);
System.out.println(slist);
Object parse = JSONObject.parse(json2);
System.out.println(parse);
Student student = JSONObject.parseObject(json2, Student.class);
System.out.println(student);
}
8. 异常处理体系
异常处理体系知识
异常:程序不正常的状态。(程序设计导致的、程序的突发特殊值导致的)Throwable顶层异常(一般不用)
----Error (错误,不需要捕获处理,程序设计缺陷,需要停止程序弥补缺陷的)
public class Demo1 {
public static void main(String[] args) {
run1();
}
public static void run1(){
run1();//StackOverflowError
}
}
----Exception (异常,需要捕获处理)
try...catch
快捷键 Ctrl+alt+t
public static void main(String[] args) {
String str = null;
String[] split = new String[0];//NullPointerException
try {
//ctrl+alt+t
split = str.split(",");
} catch (Exception e) {
System.out.println("有问题");
}
System.out.println(Arrays.toString(split));
}
finally
无论如何一定执行
public static void main(String[] args) {
try {
int a = 1/0;
} catch (Exception e) {
throw new RuntimeException(e);
} finally {
System.out.println("无论如何一定执行");
}
}