一、StringBuffer
1.StringBuffer类的概述
线程安全的可变字符序列,长度是可变的
StringBuffer和String的区别
StringBuffers线程安全的可变字符序列,长度是可变的
String一但定义他的值不能变
StringBuffer可以认为是一个字符容器,你可以不断的往容器中追加内容
2.StringBuffer类的构造方法
A:StringBuffer的构造方法:
public StringBuffer():无参构造方法,构造一个其中不带字符的字符串缓冲区,初始容量为 16 个字符。
public StringBuffer(int capacity): 指定容量的字符串缓冲区对象
public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
B:StringBuffer的方法:
public int capacity():返回当前容量。 理论值
public int length():返回长度(字符数)。 实际值
StringBuffer 重写了toString方法,会把他容器中的内容转换成字符串
3.StringBuffer类的添加和删除功能
A:StringBuffer的添加功能
public StringBuffer append(String str):可以把任意类型数据添加到字符串缓冲区里面,并返回字符串缓冲区本身
public StringBuffer insert(int offset,String str):在指定位置把任意类型的数据插入到字符串缓冲区里面,并返回字符串缓冲区本身
往字符串缓冲区追加内容,返回的还是原来的那个对象,因为我们调用完append()方法后,返回的还是那个对象,我们就可以链式编程
B:案例演示
StringBuffer的添加功能
sb.append(“aaa”).(“bbb”).insert(0,“0000”);
C:StringBuffer的删除功能
public StringBuffer deleteCharAt(int index):删除指定位置的字符,并返回本身
public StringBuffer delete(int start,int end):删除从指定位置开始指定位置结束的内容,并返回本身 (删除的内容含头不含尾)
D:案例演示
StringBuffer的删除功能
StringBuffer sb=new StringBuffer();
sb.append(“ffafafda”);
sb.deleteCharAt(0);
sb.toString();//
4.StringBuffer类的替换和反转功能和查找功能
A:StringBuffer的替换功能
public StringBuffer replace(int start,int end,String str): 从start开始到end用str替换
(替换的内容含头不含尾)
B:StringBuffer的反转功能
public StringBuffer reverse(): 字符串反转,返回的还是容器本身
C: 从头查找该字符串,在容器中第一次出现的索引,如果找不到就返回-1
int indexOf (String str)
从指定索引处开始查找该字符串第一次出现的索引,如果找不到就返回-1
int indexOf (String str,int fromIndex)
从后往前找
int lastIndexOf (String str)
int lastIndexOf (String str,int fromIndex)
5.StringBuffer的截取功能及注意事项
A:StringBuffer的截取功能
public String substring(int start): 从指定位置截取到末尾
public String substring(int start,int end): 截取从指定位置开始到结束位置,包括开始位置,不包括结束位置
B:注意事项
注意:返回值类型不再是StringBuffer本身
6.StringBuffer和String的相互转换
A:String – StringBuffer
a:通过构造方法
b:通过append()方法
String str=“abc”;
StringBuffer sb=new StringBuffer(str);
StringBuffer sb2=new StringBuffer().append(str);
B:StringBuffer – String
a: 使用substring方法
b:通过构造方法
c:通过toString()方法
StringBuffer sb3=new StringBuffer("aaaa");
String s2=sb3.substirng(0);
String s3=new String(sb3);
String s=sb3.toString();
7.把数组转成字符串
案例演示
需求:把数组中的数据按照指定个格式拼接成一个字符串
举例:
int[] arr = {1,2,3};
输出结果:
“[1, 2, 3]”
int [] arr={1,2,3};
StringBuffer sb=new StringBuffer();
sb.append("[");
for(int i=0;i<arr.length;i++){
if(i==arr.length-1){
sb.append(arr[i]).append("]");
}else {
sb.append(arr[i]).append(",");
}
}
return sb.toString();
8.字符串反转
需求:把字符串反转
举例:键盘录入"abc"
输出结果:“cba”
用StringBuffer的功能实现
Scanner sc=new Scanner(System.in);
System.out.println("请输入");
String s=sc.nextLine();
String s1=new StringBuffer(s).reverse.toString();
9.StringBuffer和String分别作为参数传递
A:形式参数问题
引用类型作为参数传递,属于引用传递,传递的是地址值,形参改变会影响实参
基本类型作为参数传递,属于值传递,传递的是值,形参的改变不影响实参
String类比较特殊,它作为参数传递属于值传递,传递的是字符串的值
String作为参数传递 String虽然是引用类型,但是它是一个常量,所以在做传递的时候,完全可以将其看成基本数据类型数据进行传递,String类型输入值传递
StringBuffer作为参数传递 ,属于引用传递。
二、StringBuilder
1.概述
线程不安全的可变字符序列
2.StringBuffer和StringBuilder的区别
StringBuffer 线程安全效率低一些
StringBuilder 线程不安全效率高一些
3.补充:StringJoiner
StringJoiner(CharSequence delimiter)
构建了一个字符容器,指定分隔符
StringJoiner(CharSequence delimiter, CharSequence prefix, CharSequence suffix)
构建了一个字符容器,指定分隔符,前缀,后缀
StringJoiner add(CharSequence newElement)
增加了一份给 CharSequence值的 StringJoiner值的下一个元素。
int length()
返回该 StringJoiner的 String表示长度。
String toString() 把容器中的数据以字符串返回
三、Arrays类
1.Arrays类的概述和方法使用
A:概述
针对数组进行操作的工具类,提供了查找和排序等功能
B:成员方法:
public static String toString(int[] a)
public static void sort(int[] a) //快速排序
public static int binarySearch(int[] a,int key) //二分查找
static boolean equals(int[] a, int[] a2) 比较两个数组中的元素,是否一样
static int[] copyOf(int[] original, int newLength) 复制旧数组中的元素到一个新的数组中,新的数组长度是newLength 从0开始复制旧数组
static int[] copyOfRange(int[] original, int from, int to) 复制旧数组中的指定范围间的几个元素到新数组中(含头不含尾)
2.Arrays类的源码分析
A:源码解析
public static String toString(int[] a)
public static String toString(int[] a) {
//如果没有,返回null
if (a == null)
return "null";
//如果数组为空,就返回[]
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
//到最后一个元素时,加 ] 然后返回。
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
B:源码解析
public static int binarySearch(int[] a,int key)
前提数组有序
public static int binarySearch(int[] a, int key) {
return binarySearch0(a, 0, a.length, key);
}
private static int binarySearch0 ( int[] a, int fromIndex, int toIndex,
int key){
int low = fromIndex;
int high = toIndex - 1;
while (low <= high) {
//无符号位移,相当于除以2
int mid = (low + high) >>> 1;
int midVal = a[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
四、基本类型包装类
1.概述
为什么会有基本类型包装类?
为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型。
常用的操作之一:用于基本数据类型与字符串之间的转换。
基本类型和包装类的对应
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
2.Integer类的概述和构造方法
A:Integer类概述
Integer 类在对象中包装了一个基本类型 int 的值,
该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,
还提供了处理 int 类型时非常有用的其他一些常量和方法
B:构造方法
public Integer(int value)
public Integer(String s) //要个一个字面上是数字的字符串,如果不是就会报错
3.String和int类型的相互转换
A:int – String
a:和""进行拼接
b:public static String valueOf(int i)
c:int – Integer – String
d:public static String toString(int i)
B:String – int
a:String – Integer – intValue();
b:public static int parseInt(String s)
C:案例演示
int num =0;
String s1=num+"";
String s2=String.valueOf(num);
String s3=new Intrger(num).toString;
System.out.println("================");
String str="100";
int i1=new Integer(str).intValue();
Integer integer = new Integer(str);
int i2=integer.parseInt(str);
4.JDK5的新特性自动装箱和拆箱
A:JDK5的新特性
自动装箱:把基本类型转换为对应包装类类型
自动拆箱:把包装类类型转换为对应基本类型
B:案例演示
JDK5的新特性自动装箱和拆箱
Integer ii = 100;//自动装箱 Integer ii=Integer.valueOf(100);
ii += 200;//自动拆箱,自动装箱 ii=Integer.valueOf(ii.intValue()+200);
C:注意事项
在使用时,Integer x = null;代码就会出现NullPointerException。
建议先判断是否为null,然后再使用。
5.Integer的面试题
A:Integer的面试题
看程序写结果
Integer i1 = new Integer(127);
Integer i2 = new Integer(127);
System.out.println(i1 == i2);//false
//重写了equals方法,比较的是他包装的值是否相同
System.out.println(i1.equals(i2));//true
System.out.println("-----------");
Integer i3 = new Integer(128);
Integer i4 = new Integer(128);
System.out.println(i3 == i4);//false
System.out.println(i3.equals(i4));//true
System.out.println("-----------");
Integer i5 = 128;
Integer i6 = 128;)
System.out.println(i5 == i6);//false
System.out.println(i5.equals(i6))//true
System.out.println("-----------");
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8);//true
System.out.println(i7.equals(i8));//true
//i5 i6 i7 i8 是自动装箱,用了Integer.valueOf(int i)方法
//从下面的源码来看为什么?
public static Integer valueOf(int i) {
//判断i是否在-128和127之间,存在则从IntegerCache中获取包装类的实例,否则new一个新实例
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
else
return new Integer(i);
}
private static class IntegerCache {
static final int high;
static final Integer cache[];
//静态方法,类加载的时候进行初始化cache[],静态变量存放在常量池中
static {
final int low = -128;
// high value may be configured by property
int h = 127;
if (integerCacheHighPropValue != null) {
// Use Long.decode here to avoid invoking methods that
// require Integer's autoboxing cache to be initialized
int i = Long.decode(integerCacheHighPropValue).intValue();
i = Math.max(i, 127);
// Maximum array size is Integer.MAX_VALUE
h = Math.min(i, Integer.MAX_VALUE - -low);
}
high = h;
cache = new Integer[(high - low) + 1];
int j = low;
for(int k = 0; k < cache.length; k++)
cache[k] = new Integer(j++);
}
private IntegerCache() {}
}