1.String类
1.获取功能的方法
- public int length () :返回此字符串的长度。
- public String concat (String str) :将指定的字符串连接到该字符串的末尾。
- public char charAt (int index) :返回指定索引处的 char值。
- public int indexOf (String str) :返回指定子字符串第一次出现在该字符串内的索引。
- public String substring (int beginIndex) :返回一个子字符串,从beginIndex开始截取字符串到字符
串结尾。 - public String substring (int beginIndex, int endIndex) :返回一个子字符串,从beginIndex到
endIndex截取字符串。含beginIndex,不含endIndex
public static void main(String[] args) {
//创建字符串对象
String s =
"helloworld";
// int length():获取字符串的长度,其实也就是字符个数
System.out.println(s.length());
System.out.println(" ‐‐‐‐‐‐‐‐");
// String concat (String str):将将指定的字符串连接到该字符串的末尾.
String s = "helloworld";
String s2 = s.concat("**hello itheima");
System.out.println(s2);// helloworld**hello itheima
// char charAt(int index):获取指定索引处的字符
System.out.println(s.charAt(0));
System.out.println(s.charAt(1));
System.out.println(" ‐‐‐‐‐‐‐‐");
// int indexOf(String str):获取str在字符串对象中第一次出现的索引,没有返回‐1
System.out.println(s.indexOf("l"));
System.out.println(s.indexOf("owo"));
System.out.println(s.indexOf("ak"));
System.out.println("‐‐‐‐‐‐‐");
// String substring(int start):从start开始截取字符串到字符串结尾
System.out.println(s.substring(0));
System.out.println(s.substring(5));
System.out.println(" ‐‐‐‐‐‐‐‐ ");
// String substring(int start,int end):从start到end截取字符串。含start,不含end
System.out.println(s.substring(0, s.length()));
System.out.println(s.substring(3,8));
}
}
2.转换功能的方法
-
public char[] toCharArray () :将此字符串转换为新的字符数组。
-
public byte[] getBytes () :使用平台的默认字符集将该 String编码转换为新的字节数组。
-
public String replace (CharSequence target, CharSequence replacement) :将与target匹配的字符串使用replacement字符串替换
public static void main(String[] args) {
//创建字符串对象
String s ="abcde";
// char[] toCharArray():把字符串转换为字符数组
char[] chs = s.toCharArray();
for(int x = 0; x < chs.length; x++) {
System.out.println(chs[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐‐");
// byte[] getBytes ():把字符串转换为字节数组
byte[] bytes = s.getBytes();
for(int x = 0; x < bytes.length; x++) {
System.out.println(bytes[x]);
}
System.out.println("‐‐‐‐‐‐‐‐‐‐");
// 替换字母it为大写IT
String str ="itcast itheima";
String replace = str.replace("it","IT");
System.out.println(replace); // ITcast ITheima
System.out.println("‐‐‐‐‐‐‐‐‐");
}
3.分割功能的方法
- public String[] split(String regex) :将此字符串按照给定的regex(规则)拆分为字符串数组。
2.static关键字
1.类变量:使用 static关键字修饰的成员变量。
2.类方法:使用 static关键字修饰的成员方法,习惯称为静态方法
3.静态代码块:定义在成员位置,使用static修饰的代码块{ }。
-
位置:类中方法外。
-
执行:随着类的加载而执行且执行一次,优先于main方法和构造方法的执行
-
作用:给类变量进行初始化赋值。用法演示,代码如下:
public class Game {
public static int number;
public static ArrayList<String> list;
static {
// 给类变量赋值
number = 2;
list = new ArrayList<String>();
// 添加元素到集合中
list.add("张三");
list.add("李四");
}
}
3.Arrays类
1.操作数组的方法
- public static String toString(int[] a) :返回指定数组内容的字符串表示形式。
public static void main(String[] args) {
// 定义int 数组
int[] arr = {2,34,35,4,657,8,69,9};
// 打印数组,输出地址值
System.out.println(arr); // [I@2ac1fdc4
// 数组内容转为字符串
String s = Arrays.toString(arr);
// 打印字符串,输出内容
System.out.println(s); // [2, 34, 35, 4, 657, 8, 69, 9]
}
- public static void sort(int[] a) :对指定的 int 型数组按数字升序进行排序。
public static void main(String[] args) {
// 定义int 数组
int[] arr = {24, 7, 5, 48, 4, 46, 35, 11, 6, 2};
System.out.println("排序前:"+ Arrays.toString(arr)); // 排序前:[24, 7, 5, 48, 4, 46, 35, 11, 6,2]
// 升序排序
Arrays.sort(arr);
System.out.println("排序后:"+ Arrays.toString(arr));// 排序后:[2, 4, 5, 6, 7, 11, 24, 35, 46,48]
}
2.练习
- 将一个随机字符串中的所有字符升序排列,并倒序打印
public static void main(String[] args) {
// 定义随机的字符串
String line =
"ysKUreaytWTRHsgFdSAoidq";
// 转换为字符数组
char[] chars = line.toCharArray();
// 升序排序
Arrays.sort(chars);
// 反向遍历打印
for (int i = chars.length‐1; i >= 0 ; i‐‐) {
System.out.print(chars[i]+" "); // y y t s s r q o i g e d d a W U T S R K H F A
}
}
4.Math类常用方法
- 基本运算的方法
- public static double abs(double a) :返回 double 值的绝对值。
- public static double ceil(double a) :返回大于等于参数的最小的整数。
- public static double floor(double a) :返回小于等于参数最大的整数。
- public static long round(double a) :返回最接近参数的 long。(相当于四舍五入方法)。