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