--------------------ASP.Net+Android+IOS開發、.Net培訓、期待與您交流! --------------------
1. StringBuffer
1.概述
StringBuffer是一個容器,是字符串的緩衝區,線程安全序列
2.特點
長度可以變換,可以操作多種類型數組
2.增加和插入
1.在尾部插入數組,append(int a),此方法是子字符串的尾部插入的數據,有很多重載方法,此方法返回的是StringBuffer對象,還是本身對象
2.在字符串的指定位置插入數據,用insert(int a),此方法也有很多重載的方法,此方法返回的是StringBuffer對象,還是本身對象
3.StringBuffer bu=new StringBuffer(“abc”);
bu.insert(bu.length,”def”);和bu.append(“def”);是一樣的。
public class StringBufferDemo {
public static void main(String [] agrs){
StringBuffer bu=new StringBuffer();
bu.append("abc").append(123).append(true);//在尾部連續添加
System.out.println("bu="+bu);
bu.insert(3, "xx");//在3的位置插入"xx",就是在"abc"後面
System.out.println("bu="+bu);
}
}
結果:
bu=abc123true
bu=abcxx123true
3. 一般操作
1.獲取subString();
public void getChars(int srcBegin,int srcEnd,char[] dst,
int dstBegin)將字符串指定範圍的字符串方法指定字節數組的範圍內。
2替換replay()
3.刪除 delete(start,end) deleteCharAt(int index)
public class StringBufferDemo {
public static void main(String [] agrs){
StringBuffer bu=new StringBuffer();
bu.append("abc").append(123).append(true);//在尾部連續添加
System.out.println("bu="+bu);
char []cs=new char[5];
bu.getChars(0, 4, cs, 1);//將bu中充0~4,包含0不包含4的字符方法字節數組中從1開始存放
for(char c:cs)
System.out.print(c+" ");
bu.deleteCharAt(2);//將第三個字符刪除
System.out.println("bu="+bu);
bu.delete(0, bu.length());//清空字符串
System.out.println("bu="+bu);
}
}
4. StringBuilder
1.5版本之後纔有的。線程不同步,不安全,但是能提高效率。可變的字符序列。簡化書寫,提高安全性用法和StringBuffer一樣,其中也會有一個差異的。
5. 包裝類
1. 概述
包裝類就是把基本數據類型封裝成了包和對象,這樣就可以使用對象來操作了。
byte-----Byte int-----Integer
short-----Shortbyte-----Byte
double-----Double float---Float
char ---Character boolean---Boolean
作用,一般就是用於數據類型和字符串之間的轉換,基本數據類型轉換成字符串和字符串轉換成基本數據類型
public class BaoZhuang {
public static void main(String[] args) {
String s1="123";
int a=Integer.parseInt(s1);//將字符串轉換成整形
String s2=a+"abc";//將整形轉換成字符串
System.out.println("s1="+s1);
System.out.println("a="+a);
System.out.println("s2="+s2);
}
}
結果:
s1=123
a=123
s2=123abc
2. 進制轉換
public class BaoZhuang {
public static void main(String[] args) {
System.out.println("10轉換成二進制:" + Integer.toBinaryString(10));
System.out.println("10轉換成八進制:" + Integer.toOctalString(10));
System.out.println("10轉換成十六進制:" + Integer.toHexString(10));
System.out.println("110轉換成十進制:" + Integer.parseInt("110", 2));
System.out.println("076轉換成十進制:" + Integer.parseInt("076", 8));
System.out.println("3c轉換成十進制:" + Integer.parseInt("3c", 16));
}
}
結果:
10轉換成二進制:1010
10轉換成八進制:12
10轉換成十六進制:a
110轉換成十進制:6
076轉換成十進制:62
3c轉換成十進制:60
3.整形和Integer之間的轉換。
Integer in=new Integer(“123”);
Int num=in.intValue();
Int n=Integer.parseInt(“123”);
6. 裝箱和拆箱
public class Text {
public static void main(String[] args) {
//JDK1.5新特性
Integer n=3;//自動裝箱,n是一個對象,而3是int類型,此時3會自動轉換成Integer類型,這是自動裝箱
n=n+2;//x是對象,2是基本數據類型,因爲對象沒有加法操作,所以
//n會自動的轉換成in類型,然後和2想加,會把結果x+2自動的轉換成Integer(裝箱)
Integer x=127;
Integer x1=127;
System.out.println(x==x1);//true
System.out.println(x.equals(x1));//true
Integer y1=128;
Integer y2=128;
System.out.println(y1==y2);//false
System.out.println(y1.equals(y2));//true
/*在1.5新特性中,只要是在byte類型的範圍 -128~127之間,他們都存在,不會在開闢新的空間,任何數據都相等
* 超過這個範圍,那麼他們的地址就不相等了*/
}
}
結果:
true
true
false
true
在1.5新特性中,只要是在byte類型的範圍 -128~127之間,他們都存在,不會在開闢新的空間,任何數據都相等,超過這個範圍,那麼他們的地址就不相等了
--------------------ASP.Net+Android+IOS開發、.Net培訓、期待與您交流!
--------------------