一.stringBuffer和stringBuilder
StringBuffer 是线程安全(耗费资源)的 可变序列
StringBuilder 是线程不安全(不耗费资源)的 可变序列
二.StringBuffer
1.添加– append 可以添加多种类型 包括基本数据类型
操作的时候,改变的是stringBuffer本身
public static void fun1() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abc");
System.out.println(stringBuffer);
}
2.插入 – insert 防止数组越界
public static void fun2() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abc");
stringBuffer.insert(2, "zxc");
System.out.println(stringBuffer);
}
3.删除 – delete 防止数组越界
public static void fun3() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abcdef");
// 删头不删尾
stringBuffer.delete(2, 4);
System.out.println(stringBuffer);
}
4.替换 – replace 防止数组越界
public static void fun4() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abcdef");
// 删头不删尾
stringBuffer.replace(2 , 4 , "zx");
System.out.println(stringBuffer);
}
5.反转 – reserve
public static void fun5() {
StringBuffer stringBuffer = new StringBuffer();
stringBuffer.append("abcd");
stringBuffer.reverse();
System.out.println(stringBuffer);
}
6.小练习:
把int[] array = new int[]{1,2,3,4};
输出[1,2,3,4]
要求:使用String和StringBuffer
使用String:
public static void fun6() {
int[] array = new int[]{1,2,3,4};
String string = "[";
for (int i = 0; i < array.length; i++) {
if ( i == array.length - 1) {
string = string + array[i] +"]";
}else {
string = string + array[i] + ",";
}
}
System.out.println(string);
}
使用StringBuffer:
public static void fun7() {
int[] array = new int[]{1,2,3,4};
StringBuffer sb = new StringBuffer();
sb.append("[");
for (int i = 0; i < array.length; i++) {
if (i == array.length - 1) {
sb.append(array[i]).append("]");
}else {
sb.append(array[i]).append(",");
}
}
System.out.println(sb);
}
三.数组 – Arrays:
1.数组的排序 – sort 类名直接调用
int[] array = new int[]{1,5,4,3,2};
Arrays.sort(array);
System.out.println(Arrays.toString(array));
2.二分查找 返回角标 – binarySearch 类名直接调用
int[] array1 = new int[]{11,22,33,44,55,99,111};
int index = Arrays.binarySearch(array1, 15);
System.out.println(index);
index返回的是 -(插入点)-1
防止返回0误以为是数组的第一位
四. Integer
1.把String类型转换成int类型 – parseInt 类名直接调用
int parseInt = Integer.parseInt("123");
System.out.println(parseInt);
2.integer类型 转化成 int类型 – intValue 对象调用
int num = integer.intValue();
System.out.println(num);
五.权限修饰符
public – 公开的
protected – 受保护的
default – 默认的
private – 私有的
public protected default private
本类 ——–: yes yes yes yes
同包类 ——: yes yes yes no
同包子类—-: yes yes yes no
不同包子类–: yes yes no no
不同包类—–: yes no no no
权限修饰符的作用范围,如上图