第三章:常见类及API
一:String类
1. 比较方法
1.01:equals(String s)方法,用于判断与s字符串是否相等
String类型最好不要用"=="进行比较,这样比较的是他们的地址值.
public class StringTest {
public static void main(String[] args) {
// 1.equals 测试
String s1 = new String("abc");
String s2 = new String("abc");
if(s1 == s2){
System.out.println("s1 is equal s2");
}else{
System.out.println("s1 is not equal s2");
}
if(s1.equals(s2)){
System.out.println("s1 is equal s2");
}else{
System.out.println("s1 is not equal s2");
}
}
}
运行结果:
1.02:equalsIgnoreCase(String s):判断与s字符串是否相等,忽略大小写
s1 = "aBc";
s2 = "abC";
if(s1.equalsIgnoreCase(s2)){
System.out.println("s1 is equal s2");
}else{
System.out.println("s1 is not equal s2");
}
// 运行结果: s1 is equals s2.
1.03:startsWith(String s):判断是否有以s开头的字符串
System.out.println("------------------startWith测试----------------");
s1 = "abcdefgh";
s2 = "abcd";
if(s1.startsWith(s2)){
System.out.println(s1 + " have " + s2 + " prefix");
}else{
System.out.println(s1 + " not have " + s2 + " prefix");
}
}
/*
运行结果:
------------------startsWith测试----------------
abcdefgh have abcd prefix
*/
1.04:endsWith(String s): 判断是否有以s结尾的字符串
System.out.println("------------------endsWith测试----------------");
s1 = "abcdefgh";
s2 = "fgh";
if(s1.endsWith(s2)){
System.out.println(s1 + " have " + s2 + " postfix");
}else{
System.out.println(s1 + " not have " + s2 + " postfix");
}
/*
运行结果:
------------------endsWith测试----------------
abcdefgh have fgh postfix
*/
1.05:compareTo(String s):判断与s的大小,按字典序大小比较.
1,06:compateToIgnoreCase(String s):判断与s的大小,忽略大小写,按字典序大小比较.
大于返回一个正整数,等于返回0,小于返回一个负整数.
// 比较字符串,按照字典序,相等放回0,
System.out.println("------------------compareTo测试----------------");
s1 = "abc";
s2 = "abd";
System.out.println(s1.compareTo(s2));
System.out.println(s2.compareTo(s1));
s2 = "Abc";
System.out.println(s1.compareTo(s2));
System.out.println("------------------compareToIgnoreCase测试----------------"); //忽略大小写比较
System.out.println(s1.compareToIgnoreCase(s2));
/*
运行结果:
------------------compareTo测试----------------
-1
1
32
------------------compareToIgnoreCase测试----------------
0
*/
2. 获取方法
2.01:获取字符串长度:length()方法;
String s = "666";
System.out.println(s.length());
// 运行结果: 3
2.02 获取子串:substring(int start, int end);
返回以[start,end-1]范围内的子串
String s = "2024/03/30";
System.out.println(s.substring(0,4));
//运行结果: 2024
2.03: 将字符串转化为字符串数组: toCharArray();
System.out.println("------------------toCharArray测试----------------");
s1 = "1 2 3 4 5 6";
char[] array = s1.toCharArray();
System.out.println(array.length);
for(char i : array){
System.out.print(i);
}
/*
运行结果:
------------------toCharArray测试----------------
11
1 2 3 4 5 6
*/
2.04:获取字符串中指定索引的字符:charAt(int index);
s1 = "0123456";
System.out.println(s1.charAt(0));
//运行结果: 0
3.其他方法
3.01 去除字符串最前面和最后面的空格: trim()
System.out.println("------------------trim测试----------------");
s1 = " ssssssssssss ";
System.out.println(s1.trim());
/*
运行结果:
ssssssssssss
*/
3.02 字符串替换:replace(String oldString, String newString);
将.md 替换成.html
System.out.println("------------------replace测试----------------");
s1 = "java.md";
s1 = s1.replace(".md",".html");
System.out.println(s1);
/*
运行结果:
------------------replace测试----------------
java.html
*/
3.03 将字符串进行分割,生成字符串数组:split(String regex)
System.out.println("------------------split测试----------------");
s1 = "abc#efg#hij";
String[] arr1 = s1.split("#");
/*
运行结果:
------------------split测试----------------
abc
efg
hij
*/
二:Random类
位于 java.util.Random包下.
1.构造方法
方法1:Random() 推荐
以某个值作为随机数种子,且与当前创建的所有Random类的种子不同.
方法2:Random(long seed)
自己设定一个数字作为随机数种子
2.常用方法
2.01:nextInt()
等概率产生[ - 2 31 2^{31} 231 ~ 2 31 − 1 2^{31} -1 231−1 ]的int类型数字
Random random = new Random();
for (int i = 0; i < 10; i++) {
int nextInt = random.nextInt();
System.out.println(nextInt);
}
/*
运行结果:
917318282
-1069657429
-386035685
-70675832
87372180
-30473490
624199830
1474955383
467318482
-1616431370
*/
2.02: nextInt(int n)
等概率产生[ 0 ~ n − 1 n-1 n−1 ]的int类型数字.
for (int i = 0; i < 10; i++) {
int nextInt = random.nextInt(100);
System.out.println(nextInt);
}
/*
运行结果:
62
98
32
74
77
42
37
88
1
86
*/
2.03 nextDouble()
等概率产生 [0 ~ 1.0)的double类型数字
for (int i = 0; i < 10; i++) {
System.out.printf("%.2f\n",random.nextDouble());
}
/*
运行结果:
0.30
0.37
0.70
0.73
0.08
0.53
0.94
0.68
0.71
0.56
*/
Arrays类
位于 java.util.Arrays包中.
1.fill(array, num):填充数组
int[] arr = new int[10];
System.out.println("------------------fill测试----------------");
Arrays.fill(arr,666);
for(int i : arr){
System.out.print(i + " ");
}
/*
运行结果:
666 666 666 666 666 666 666 666 666 666
*/
2.toString():以数组形式转化为字符串
Random random = new Random();
for (int i = 0; i < 10; i++) {
// 随机产生[1,100]的数字
arr[i] = random.nextInt(100) + 1;
}
System.out.println("------------------toString测试----------------");
System.out.println(Arrays.toString(arr));
/*
运行结果:
------------------toString测试----------------
[61, 68, 69, 2, 64, 31, 71, 71, 81, 12]
*/
3.sort(array):以升序排序数组
Random random = new Random();
for (int i = 0; i < 10; i++) {
// 随机产生[1,100]的数字
arr[i] = random.nextInt(100) + 1;
}
System.out.println("------------------toString测试----------------");
System.out.println(Arrays.toString(arr));
System.out.println("------------------sort测试----------------");
Arrays.sort(arr);
System.out.println(Arrays.toString(arr));
/*
运行结果:
------------------toString测试----------------
[41, 85, 99, 16, 92, 73, 18, 20, 30, 95]
------------------sort测试----------------
[16, 18, 20, 30, 41, 73, 85, 92, 95, 99]
*/