1.1 String和StringBuffer
String类常用方法:length、indexOf、charAt、equals(比较字符串的内容)、replace、split、substring、trim、format。
length:返回这个字符串的长度;
String str1 = "abc";
String str2 = new String("abc1234");
System.out.println(str1.length());
indexOf:查找这个字符串中某个字符的位置
String str1 = "abc";
String str2 = new String("abc1234");
System.out.println(str1.indexOf("a"));
charAt:查找某个位置的字符
String str2 = new String("abc1234");
System.out.println(str2.charAt(4));
equals:比较两个字符串中的内容是否相等,
String str1 = new String("abc");
String str2 = new String("abc");
System.out.println(str1.equals(str2));
replace:将str1转换为str2
String str1 = "abc";
String str2 = new String("abc1234");
System.out.printf(str1.replace(str1,str2));
split:分割字符串
String str3 = "a=1=2=d";
String str4[] = str3.split("=");
for (int i= 0; i < str4.length;i++) {
System.out.printf(str4[i]);
}
substring:用旧的字符串返回一个新的字符串,这个字符串是旧的字符串里的元素
System.out.println(str1.substring(1));
trim:省略字符串前后的空格
String str3 = " a=1=2=d ";
System.out.println(str3);
System.out.println(str3.trim());
format:使用指定的格式返回字符串
System.out.println(String.format("str1=%s",str1));
StringBuffer类常用方法:append、insert、deleteCharAt、delete、replace、setCharAt、reverse。
String是不可更改,如需更改字符串内容用StringBuffer。
StringBuffer完成字符串的添加,插入和替换等操作
StringBuffer的构造方法:
StringBuffer():构造一个没有任何字符的StringBuffer类
StringBuffer(int length)构造一个没有任何字符的StringBuffer类,并且其长度为length。
StringBuffer(String str)以str为初始值构造一个StringBuffer类
append:将某个参数的字符串附加到这个定义的字符串中
StringBuffer str = newStringBuffer("abc");
str.append("123");
System.out.println(str);
insert:向字符串的某个位置插入字符
str.insert(0,55);
System.out.println(str);
deleteCharAt:删除字符某个位置的字符
System.out.println(str.deleteCharAt(2));
delete:删除指定位置范围内字符串的字符
System.out.println(str.delete(2,4));
replace:用指定字符串中的字符替换这个序列的子串中的字符。
System.out.println(str.replace(0,2,"="));
setCharAt:把字符串中的某个字符替换为指定的char类型元素
str.setCharAt(1, 'a');
reverse:将此字符串转换为相反的形式
System.out.println(str.reverse());
Ararays所有的方法都是静态的,提供数组的排序、查找、填充、获取子数组。
sort、binarySearch、fill、copyOf、copyOfRange
sort:1,给所有数按升序排序
int arr[] = {20,15,30,25,35};
Arrays.sort(arr);
for (int i= 0; i < arr.length;i++) {
System.out.printf(" "+arr[i]);
}
2,给第n位(n开始)到第m位(不包括)排序
Arrays.sort(arr,1,4);
binarySearch:二分查找法找数组中的元素
int arr[] = {20,15,30,25,35};
System.out.printf(" "+Arrays.binarySearch(arr,20));
fill:填充数组
int arr[]=new int[5];
Arrays.fill(arr,1);
for (int i= 0; i < arr.length;i++) {
System.out.printf(" "+arr[i]);
}
copyOf:截取数组的n个元素赋值给新数组
int arr[] = {20,15,30,25,35};
int arr1[] = Arrays.copyOf(arr,3);
for (int i = 0; i <arr1.length; i++) {
System.out.printf(" "+arr1[i]);
}
copyOfRange:从第n位截取到第m位(不包括)
int arr[] = {20,15,30,25,35};
int arr1[] = Arrays.copyOfRange(arr,2,4);
for (int i = 0; i <arr1.length; i++) {
System.out.printf(" "+arr1[i]);
}
Math类方法:abs、ceil(向上取整)、floor、round、max、min、exp(指数)、log、pow、sqrt、random(随机数)、三角函数
abs:求绝对值
System.out.println(Math.abs(-10.5));
ceil:向上取整
System.out.println(Math.ceil(10.1));
floor:向下取整
System.out.println(Math.floor(10.9));
round:四舍五入
System.out.println(Math.round(10.4));
max/min最大值/最小值:
System.out.println(Math.max(5,4));
System.out.println(Math.min(5,4));
exp:数学常量e的n次方
System.out.println(Math.exp(2));
log:对数
System.out.println(Math.log(10));
pow:求一个数的n次幂
System.out.println(Math.pow(2,3));
sqrt:开方
System.out.println(Math.sqrt(9));
random:随机生成大于等于0小于1的随机数
System.out.println(Math.random());
三角函数:正反正弦函数、正反余弦函数、正反正切函数。
System.out.println("|"+Math.sin(1)+"|"+Math.cos(1)+"|"+Math.tan(1)+"|"+Math.asin(1)+"|"+Math.acos(1)+"|"+Math.atan(1));
Math类的方法都是静态方法,所以直接通过类名调用,比如Math.abs