Java常用类(二)
一、StringBuffer类
1、StringBuffer类的概述
线程安全的可变字符序列。一个类似于 String 的字符串缓冲区,但不能修改。虽然在任意时间点上它都包含某种特定的字符
序列,但通过某些方法调用可以改变该序列的长度和内容。
String类和StringBuffer类的区别;
1、String表示的字符串是常量,其长度和内容都无法改变。而StringBuffer表示字符容器,其长度和内容都可以改变;
2、在操作字符串时,如果该字符串仅用于表示数据类型,则使用String类即可,但如果需要对字符串中的字符进行修改操
作,则须使用StringBuffer类;
3、String覆盖了Object类的equals()方法,而StringBuffer类没有覆盖;
4、String类对象可以用操作符"+"进行连接,而StringBuffer类对象不可以。
2、StringBuffer类的构造方法
public class Example16 {
public static void main(String[] args) {
//构造方法
//public StringBuffer(): 无参构造方法,不带字符,其初始容量为16个字符
//public StringBuffer(int capacity): 不带字符,指定容量的字符串缓冲区
//public StringBuffer(String str): 指定字符串内容的字符串缓冲区对象
//方法
//public int capacity(): 返回当前容量, 理论值
//public int length(): 返回长度(字符数, 实际值
StringBuffer sb = new StringBuffer();
System.out.println(sb.capacity());//默认容量16
StringBuffer sb1 = new StringBuffer(100);
System.out.println(sb1.capacity());//指定容量100
StringBuffer sb2 = new StringBuffer("123");
System.out.println(sb2.length());//字符长度 :3
}
}
运行结果:
16
100
3
Process finished with exit code 0
3、StringBuffer的添加,修改,删除功能
public class Example14 {
public static void main(String[] args) {
System.out.println("1、添加---------------");
add();
System.out.println("2、删除---------------");
remove();
System.out.println("3、修改---------------");
String str = alter();
System.out.println(str);
}
private static void add() {
StringBuffer stringBuffer = new StringBuffer();//定义一个字符串缓冲区
stringBuffer.append("abcdefg");//添加参数("append")到StringBuffer容器中
System.out.println(stringBuffer);
System.out.println(stringBuffer.insert(3, "123"));//在指定位置插入字符串
}
private static String alter() {
StringBuffer stringBuffer = new StringBuffer("abcdefg");//构造方法添加字符串
System.out.println(stringBuffer.replace(0, 3, "hij"));//修改指定区间的字符或字符串 //范围含头不含尾
stringBuffer.setCharAt(stringBuffer.length()-1,'1');//修改指定位置的字符
System.out.println(stringBuffer);
System.out.println(stringBuffer.reverse());//反转字符序列
return stringBuffer.toString();//返回StringBuffer缓冲区中的String类型的字符串
}
private static void remove() {
StringBuffer stringBuffer = new StringBuffer("abcdefg");
System.out.println(stringBuffer.deleteCharAt(3));//删除指定位置字符
System.out.println(stringBuffer.delete(3, 5));//删除指定范围字符串 //含头不含尾
System.out.println(stringBuffer.delete(0, stringBuffer.length()));//清空缓冲区
}
}
运行结果:
1、添加---------------
abcdefg
abc123defg
2、删除---------------
abcefg
abcg
3、修改---------------
hijdefg
hijdef1
1fedjih
1fedjih
Process finished with exit code 0
4、StringBuffer和String的相互转换
public class Example17 {
public static void main(String[] args) {
//String -- StringBuffer
String str="123";
StringBuffer sb = new StringBuffer(str);//通过构造方法
StringBuffer sb1 = new StringBuffer().append(str);//通过append()方法
//StringBuffer -- String
String sbStr = sb.substring(0);//使用substring方法
String sbStr1 = new String(sb);//通过构造方法
String sbStr2 = sb.toString();//通过toString()方法
}
}
5、StringBuilder类
一个可变的字符序列。此类提供一个与 StringBuffer 兼容的 API,但不保证同步。该类被设计用作 StringBuffer 的
一个简易替换,用在字符串缓冲区被单个线程使用的时候(这种情况很普遍)。如果可能,建议优先采用该类,因为在大多数
实现中,它比StringBuffer 要快。
二、Arrays类
1、Arrays类概述
针对数组进行操作的工具类。提供了排序,查找等功能。
2、Arrays类的方法使用
public static String toString(int[] a)
public static void sort(int[] a)
public static int binarySearch(int[] a,int key)
演示:
import java.util.Arrays;
public class Example18 {
public static void main(String[] args) {
int[] arr={55,44,66,33,77,22,88,11,99,-1,0};
String arrStr = Arrays.toString(arr);//把数组以字符串形式展示出来
System.out.println(arrStr);
Arrays.sort(arr);//对数组进行排序(快速排序)
String arrSortStr = Arrays.toString(arr);//把数组以字符串形式展示出来
System.out.println(arrSortStr);
int index = Arrays.binarySearch(arr, 55);//二分查找,数组必须有序
System.out.println(index);
}
}
运行结果:
[55, 44, 66, 33, 77, 22, 88, 11, 99, -1, 0]
[-1, 0, 11, 22, 33, 44, 55, 66, 77, 88, 99]
6
Process finished with exit code 0
3、Arrays类方法的源码解析
A:数组字符串
public static String toString(int[] a) {
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[] arr, int ele) {
int maxIndex = arr.length - 1;
int minIndex = 0;
int midIndex = (maxIndex + minIndex) >>> 1;
while (minIndex <= maxIndex) {
if (ele == arr[midIndex]) {
return midIndex;
} else if (ele < arr[midIndex]) {
maxIndex = midIndex - 1;
} else {
minIndex = midIndex + 1;
}
midIndex = (maxIndex + minIndex) >>> 1;
}
return -1;
}
三 、包装类
A:概述:为了对基本数据类型进行更多、更方便的操作,java就针对每一种基本数据类型提供了对应的类类型.
B:基本类型和包装类的对应
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
以Integer为例演示:
1、构造方法
Integer integer = new Integer(100);//将int类型数据转换为Integer类类型
Integer integer1 = new Integer("100");//将String类型(内容必须为int类型)转换为Integer类类型
2、常用方法
int i = integer.intValue();//将Integer类型转换为int类型
String str = integer.toString();//将Integer类型转换为String类型
//获取int的范围
int maxValue = Integer.MAX_VALUE;
int minValue = Integer.MIN_VALUE;
//将String类型(字面值必须为int类型)转为int类型
int num2 = Integer.parseInt("100");
//将字符串(字面值必须为int类型)或int类型转为Integer类型
Integer integer = Integer.valueOf("100");
3、JDK5的新特性自动装箱和拆箱
自动装箱:系统自动把基本类型转换为包装类类型
自动拆箱:系统自动把包装类类型转换为基本类型
演示:
Integer integer=100; //自动装箱
int a=integer; //自动拆箱
注意:
Integer inte = 127;
采用自动装箱这种方式,给他赋值的时候 这个值如果在 -128---127 那么他会从缓存中取对象,
如果超过一个字节的范围,那么就会重新new一个对象
举例:
Integer i5 = 128;// i5= new Integer(128)
Integer i6 = 128; // i6=new Integer(128)
System.out.println(i5 == i6); //false //重新new的对象,地址值不同
System.out.println(i5.equals(i6));//true
//自动装箱
Integer i7 = 127;
Integer i8 = 127;
System.out.println(i7 == i8); //true //从缓存中取的对象,地址值相同
System.out.println(i7.equals(i8));//true