文章目錄
-
- 裝箱拆箱
-
- 封裝類-Number類
- 基本類型轉封裝類-封裝類轉基本類型
- 自動裝箱-自動拆箱
- 字元串轉換
-
- 數字轉字元串(ValueOf/toString)
- 字元串轉數字(Integer.parseInt(str))
- 常用常量
- 數學方法:
- 格式化輸出
- 字元
- 字元串
- 操縱字元串
- 比較字元串:
- StringBuffer
-
- String與StringBuffer的性能差別?
- MyStringBuffer
裝箱拆箱
封裝類-Number類
封裝類:
所有的基本類型,都有對應的類類型
比如
int
對應的類是
Integer
這種類就叫做封裝類
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//把一個基本類型的變量,轉換為Integer對象
Integer it = new Integer(i);
//把一個Integer對象,轉換為一個基本類型的int
int i2 = it.intValue();
}
}
Number類:
數字封裝類有
Byte,Short,Integer,Long,Float,Double
這些類都是抽象類
Number
的子類
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
Integer it = new Integer(i);
//Integer是Number的子類,是以列印true
System.out.println(it instanceof Number);
}
}
基本類型轉封裝類-封裝類轉基本類型
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//基本類型轉換成封裝類型
Integer it = new Integer(i);
//封裝類型轉換成基本類型
int i2 = it.intValue();
}
}
自動裝箱-自動拆箱
不需要調用構造方法,通過
=
符号自動把 基本類型 轉換為 類類型 就叫裝箱
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//基本類型轉換成封裝類型
Integer it = new Integer(i);
//自動轉換就叫裝箱
Integer it2 = i;
}
}
不需要調用
Integer
的
intValue
方法,通過
=
就自動轉換成
int
類型,就叫拆箱
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
Integer it = new Integer(i);
//封裝類型轉換成基本類型
int i2 = it.intValue();
//自動轉換就叫拆箱
int i3 = it;
}
}
int的最大值可以通過其對應的封裝類
Integer.MAX_VALUE
擷取
那到底是使用new呢,還是使用valueOf方法呢。new每次都會建立一個新的對象,而除了Float和Double 外的其他包裝類,都會緩存包裝類對象,減少需要建立對象的此數,節省空間提升性能。實際上,從java 9開始,這些構造方法已經被标記為過時了,推薦使用valueOf方法。
![](https://img.laitimes.com/img/_0nNw4CM6IyYiwiM6ICdiwiIyVGduV2YfNWawNyZuBnLwYDO4ETNykDM3ATMxAjMwIzLc52YucWbp5GZzNmLn9Gbi1yZtl2Lc9CX6MHc0RHaiojIsJye.png)
false
true //這裡因為是int,與基本類型比較時,都是比較的值
true
Integer a = 10;
Integer b = 10;
a==b 也是true,因為 在-128-127内的數,是直接用的緩沖區的數(IntegerCache)
但是:
Integer a = 1000;
Integer b = 1000;
a==b 是false,不在緩沖區的數還需要再new
字元串轉換
數字轉字元串(ValueOf/toString)
方法1: 使用
String
類的靜态方法
valueOf
方法2: 先把基本類型裝箱為對象,然後調用對象的
toString
package digit;
public class TestNumber {
public static void main(String[] args) {
int i = 5;
//方法1
String str = String.valueOf(i );
//方法2
Integer it = i;
String str2 = it.toString();
}
}
字元串轉數字(Integer.parseInt(str))
除了Character外,
1、每一個包裝類都有一個靜态的valueOf(String)方法,根據字元串表示傳回包裝類對象
Boolean b = Boolean.valueOf("true");
Float f = Float.valueOf("132.23f");
2、也都有一個parseXXX(String)方法,根據字元串表示傳回基本類型。
調用
Integer
的靜态方法
parseInt
int i= Integer.parseInt("123");
boolean b = Boolean.parseBoolean("true");
但是輸入的字元串必須和所有的方法對應。
String str3 = "3.1a4";
float i3 = Float.parseFloat(str3);
//異常 System.out.println(i3);//java.lang.NumberFormatException
對于整數類型,字元串表示除了預設的十進制外,還可以表示為其他進制,如二進制,八進制,十六進制,包裝類有靜态方法進行
互相轉換。
System.out.println(Integer.toBinaryString(1234));//輸出二進制
System.out.println(Integer.toHexString(1234));//輸出十六進制
System.out.println(Integer.parseInt("3039",16));//按十六進制解析
'輸出:'
11000000111001
3039
12345
常用常量
包裝類中除了定義靜态方法和執行個體方法外,還定義了一些靜态變量。
如,TRUE,數值類型的MIN_VALUE,MAX_VALUE,無窮大等、
public static final boolean TRUE= new Boolean(true);
public static final int MIN_VALUE= 0x80000000;
public static final int MAX_VALUE= 0x7fffffff;
public static final double POSITIVE_INFINITY= 1.0/0.0;//正無窮大
public static final double NEGATIVE_INFINITY= -1.0/0.0;//負無窮大
public static final double NaN= 0.0d/0.0;//非數值
外部可以調用:
int b = Integer.MIN_VALUE;
數學方法:
java.lang.Math
提供了一些常用的數學運算方法,并且都是以靜态方法的形式存在
四舍五入, 随機數,開方,次方,π,自然常數
package digit;
public class TestNumber {
public static void main(String[] args) {
float f1 = 5.4f;
float f2 = 5.5f;
//5.4四舍五入即5
System.out.println(Math.round(f1));
//5.5四舍五入即6
System.out.println(Math.round(f2));
//得到一個0-1之間的随機浮點數(取不到1)
System.out.println(Math.random());
//得到一個0-10之間的随機整數 (取不到10)
System.out.println((int)( Math.random()*10));
//開方
System.out.println(Math.sqrt(9));
//次方(2的4次方)
System.out.println(Math.pow(2,4));
//π
System.out.println(Math.PI);
//自然常數
System.out.println(Math.E);
}
}
格式化輸出
如果不使用格式化輸出,就需要進行
字元串連接配接
,如果變量比較多,拼接就會顯得繁瑣。使用格式化輸出,就可以簡潔明了
%s 表示字元串
%d 表示數字
%n 表示換行
package digit;
public class TestNumber {
public static void main(String[] args) {
String name ="蓋倫";
int kill = 8;
String title="超神";
//直接使用+進行字元串連接配接,編碼感覺會比較繁瑣,并且維護性差,易讀性差
String sentence = name+ " 在進行了連續 " + kill + " 次擊殺後,獲得了 " + title +" 的稱号";
System.out.println(sentence);
//使用格式化輸出
//%s表示字元串,%d表示數字,%n表示換行
String sentenceFormat ="%s 在進行了連續 %d 次擊殺後,獲得了 %s 的稱号%n";
System.out.printf(sentenceFormat,name,kill,title);
}
}
printf和format
printf和format
能夠達到一模一樣的效果,如何通過eclipse檢視java源代碼 可以看到,在
printf
中直接調用了
format
package digit;
public class TestNumber {
public static void main(String[] args) {
String name ="蓋倫";
int kill = 8;
String title="超神";
String sentenceFormat ="%s 在進行了連續 %d 次擊殺後,獲得了 %s 的稱号%n";
//使用printf格式化輸出
System.out.printf(sentenceFormat,name,kill,title);
//使用format格式化輸出
System.out.format(sentenceFormat,name,kill,title);
}
}
換行符
換行符就是另起一行 — ‘\n’ 換行(newline)
回車符就是回到一行的開頭 — ‘\r’ 回車(return)
在eclipse裡敲一個回車,實際上是回車換行符
Java是跨平台的程式設計語言,同樣的代碼,可以在不同的平台使用,比如Windows,Linux,Mac
然而在不同的作業系統,換行符是不一樣的
(1)在DOS和Windows中,每行結尾是
“\r\n”
;
(2)Linux系統裡,每行結尾隻有
“\n”
;
(3)Mac系統裡,每行結尾是隻有
"\r"
。
為了使得同一個
java
程式的換行符在所有的作業系統中都有一樣的表現,使用
%n
,就可以做到平台無關的換行
System.out.printf("這是換行符%n");
System.out.printf("這是換行符%n");
字元
儲存一個字元的時候使用char
package character;
public class TestChar {
public static void main(String[] args) {
char c1 = 'a';
char c2 = '1';//字元1,而非數字1
char c3 = '中';//漢字字元
char c4 = 'ab'; //隻能放一個字元
}
}
char對應的封裝類: Character
public static void main(String[] args) {
char c1 = 'a';
char c2 = '3';
Character c = c1; //自動裝箱
c1 = c;//自動拆箱
Character常見方法
package character;
public class TestChar {
public static void main(String[] args) {
System.out.println(Character.isLetter('a'));//判斷是否為字母
System.out.println(Character.isLetter(c3));//判斷是否為字母
System.out.println(Character.isDigit('a')); //判斷是否為數字
System.out.println(Character.isWhitespace(' ')); //是否是空白
System.out.println(Character.isUpperCase('a')); //是否是大寫
System.out.println(Character.isLowerCase('a')); //是否是小寫
System.out.println(Character.isUpperCase(c1)&&Character.isLowerCase(c2));//邏輯使用
System.out.println(Character.toUpperCase('a')); //轉換為大寫
System.out.println(Character.toLowerCase('A')); //轉換為小寫
String a = 'a'; //不能夠直接把一個字元轉換成字元串
String a2 = Character.toString('a'); //轉換為字元串
}
}
常見轉義:
package character;
public class TestChar {
public static void main(String[] args) {
System.out.println("使用空格無法達到對齊的效果");
System.out.println("abc def");
System.out.println("ab def");
System.out.println("a def");
System.out.println("使用\\t制表符可以達到對齊的效果");
System.out.println("abc\tdef");
System.out.println("ab\tdef");
System.out.println("a\tdef");
System.out.println("一個\\t制表符長度是8");
System.out.println("12345678def");
System.out.println("換行符 \\n");
System.out.println("abc\ndef");
System.out.println("單引号 \\'");
System.out.println("abc\'def");
System.out.println("雙引号 \\\"");
System.out.println("abc\"def");
System.out.println("反斜杠本身 \\");
System.out.println("abc\\def");
}
}
字元串
建立字元串
字元串即字元的組合,在Java中,字元串是一個類,是以我們見到的字元串都是對象
常見建立字元串手段:
每當有一個字面值出現的時候,虛拟機就會建立一個字元串
1.
調用String的構造方法建立一個字元串對象
2.
通過
3.
加号進行字元串拼接也會建立新的字元串對象
+
package character;
public class TestString {
public static void main(String[] args) {
String garen ="蓋倫"; //字面值,虛拟機碰到字面值就會建立一個字元串對象
String teemo = new String("提莫"); //建立了兩個字元串對象
char[] cs = new char[]{'崔','斯','特'};
String hero = new String(cs);// 通過字元數組建立一個字元串對象
String hero3 = garen + teemo;// 通過+加号進行字元串拼接
}
}
final
String
被修飾為
final
,是以是不能被繼承的
package character;
public class TestString {
public static void main(String[] args) {
MyString str = new MyString();
}
/*這裡會報錯,因為String不能被繼承*/
static class MyString extends String{
}
}
immutable
immutable 是指不可改變的
比如建立了一個字元串對象
不可改變的具體含義是指:
不能增加長度
不能減少長度
不能插入字元
不能删除字元
不能修改字元
一旦建立好這個字元串,裡面的内容 永遠 不能改變
String
的表現就像是一個常量
字元串格式化
如果不使用字元串格式化,就需要進行字元串連接配接,如果變量比較多,拼接就會顯得繁瑣
字元串長度
length
方法傳回目前字元串的長度
可以有長度為0的字元串,即空字元串
String name ="蓋倫";
System.out.println(name.length());
'數字和字元之間可以通過互相轉換'
char c = 'A';
short s = (short) c;
操縱字元串
- charAt(int index)擷取指定位置的字元
String sentence = "蓋倫,在進行了連續8次擊殺後,獲得了 超神 的稱号";
char c = sentence.charAt(0);
- toCharArray() 擷取對應的字元數組
String sentence = "蓋倫,在進行了連續8次擊殺後,獲得了超神 的稱号";
char[] cs = sentence.toCharArray(); //擷取對應的字元數組
System.out.println(sentence.length() == cs.length);
- subString截取子字元串
String sentence = "蓋倫,在進行了連續8次擊殺後,獲得了 超神 的稱号";
//截取從第3個開始的字元串 (基0)
String subString1 = sentence.substring(3);
System.out.println(subString1);
//截取從第3個開始的字元串 (基0)
//到5-1的位置的字元串
//左閉右開
String subString2 = sentence.substring(3,5);
System.out.println(subString2);
- split 根據分隔符進行分隔
String sentence = "蓋倫,在進行了連續8次擊殺後,獲得了 超神 的稱号";
//根據,進行分割,得到3個子字元串
String subSentences[] = sentence.split(",");
for (String sub : subSentences) {
System.out.println(sub);
}
//以字母作為分隔符
String yi = "asdgjhfsafgsjfgsjafgalsdjkgfalfgwierwihsdjkfhsjk";
for (String each : yi.split("s")) {
System.out.println(each);
}
- trim 去掉首尾空格
String sentence = " 蓋倫,在進行了連續8次擊殺後,獲得了 超神 的稱号 ";
System.out.println(sentence);
//去掉首尾空格
System.out.println(sentence.trim());
- toLowerCase 全部變成小寫 toUpperCase 全部變成大寫
String sentence = "Garen";
//全部變成小寫
System.out.println(sentence.toLowerCase());
//全部變成大寫
System.out.println(sentence.toUpperCase());
- indexOf 判斷字元或者子字元串出現的位置 contains 是否包含子字元串
String sentence = "蓋倫,在進行了連續8次擊殺後,獲得了超神 的稱号";
System.out.println(sentence.indexOf('8')); //字元第一次出現的位置
System.out.println(sentence.indexOf("超神")); //字元串第一次出現的位置
System.out.println(sentence.lastIndexOf("了")); //字元串最後出現的位置
System.out.println(sentence.indexOf(',',5)); //從位置5開始,出現的第一次,的位置
System.out.println(sentence.contains("擊殺")); //是否包含字元串"擊殺"
- replaceAll 替換所有的 replaceFirst 隻替換第一個
String sentence = "蓋倫,在進行了連續8次擊殺後,獲得了超神 的稱号";
String temp = sentence.replaceAll("擊殺", "被擊殺"); //替換所有的
temp = temp.replaceAll("超神", "超鬼");
System.out.println(temp);
temp = sentence.replaceFirst(",","");//隻替換第一個
System.out.println(temp);
比較字元串:
- 是否是同一個對象
和str1
str2
的内容一定是一樣的!
但是,并不是同一個字元串對象
String str1 = "the light";
String str2 = new String(str1);
//==用于判斷是否是同一個字元串對象
System.out.println( str1 == str2)
- 是否是同一個對象-特例**
str1 = "the light";
str3 = "the light";
一般說來,編譯器每碰到一個字元串的字面值,就會建立一個新的對象
是以在第6行會建立了一個新的字元串"the light"
但是在第7行,編譯器發現已經存在現成的"the light",那麼就直接拿來使用,而沒有進行重複建立
-
内容是否相同
使用equals進行字元串内容的比較,必須大小寫一緻
equalsIgnoreCase,忽略大小寫判斷内容是否一緻
String str1 = "the light";
String str2 = new String(str1);
String str3 = str1.toUpperCase();
//==用于判斷是否是同一個字元串對象
System.out.println( str1 == str2);
System.out.println(str1.equals(str2));//完全一樣傳回true
System.out.println(str1.equals(str3));//大小寫不一樣,傳回false
System.out.println(str1.equalsIgnoreCase(str3));//忽略大小寫的比較,傳回true
-
是否以子字元串開始或者結束
startsWith //以…開始
endsWith //以…結束
String str1 = "the light";
String start = "the";
String end = "Ight";
System.out.println(str1.startsWith(start));//以...開始
System.out.println(str1.endsWith(end));//以...結束
StringBuffer
StringBuffer是可變長的字元串
StringBuffer常用方法:
StringBuffer sf=new StringBuffer();
// 用于字元串拼接
sf.append("abc");
sf.append(12);
System.out.println(sf);
// 删除指定位置的内容 [start,end)
sf.delete(3,5);
System.out.println(sf);
// 把[start,end)位置替換為str
sf.replace(1,3,"go");
System.out.println(sf);
// 在指定位置插入資料
sf.insert(2,"xxx");
System.out.println(sf);
// 截取[start,end)字元串
System.out.println(sf.substring(1,5));
// 傳回字元串長度
System.out.println(sf.length());
// 擷取索引為1的字元
System.out.println(sf.charAt(1));
// 将索引為1c處的值替換為'm'
sf.setCharAt(1,'m');
System.out.println(sf);
// 将字元串翻轉
System.out.println(sf.reverse());
追加 删除 插入 反轉
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "let there ";
StringBuffer sb = new StringBuffer(str1); //根據str1建立一個StringBuffer對象
sb.append("be light"); //在最後追加
System.out.println(sb);
sb.delete(4, 10);//删除4-10之間的字元
System.out.println(sb);
sb.insert(4, "there ");//在4這個位置插入 there
System.out.println(sb);
sb.reverse(); //反轉
System.out.println(sb);
}
}
長度 容量
為什麼
StringBuffer
可以變長?
和
String
内部是一個字元數組一樣,
StringBuffer
也維護了一個字元數組。 但是,這個字元數組,留有備援長度
比如說
new StringBuffer("the")
,其内部的字元數組的長度,是
19
,而不是
3
,這樣調用插入和追加,在現成的數組的基礎上就可以完成了。
如果追加的長度超過了
19
,就會配置設定一個新的數組,長度比原來多一些,把原來的資料複制到新的數組中,看上去 數組長度就變長了 參考
MyStringBuffer
length:
“the”的長度 3
capacity:
配置設定的總空間 19
注: 19這個數量,不同的JDK數量是不一樣的
package character;
public class TestString {
public static void main(String[] args) {
String str1 = "the";
StringBuffer sb = new StringBuffer(str1);
System.out.println(sb.length()); //内容長度
System.out.println(sb.capacity());//總空間
}
}
String與StringBuffer的性能差別?
生成10位長度的随機字元串
然後,先使用String的+,連接配接10000個随機字元串,計算消耗的時間
然後,再使用StringBuffer連接配接10000個随機字元串,計算消耗的時間
提示: 使用
System.currentTimeMillis()
擷取目前時間(毫秒)
補充一下:事實上,從JDK1.5後,用加号拼接字元串幾乎沒有什麼性能損失了。
還有另外要說明的(摘自網上):
最早是沒有 stringbuilder 的,sun 的人不知處于何種考慮,決定讓 stringbuffer 是線程安全的,然後大約 10 年之後,人們終于意識到這是一個多麼愚蠢的決定,意識到在這 10 年之中這個愚蠢的決定為 java 運作速度慢這樣的流言貢獻了多大的力量,于是,在 jdk1.5 的時候,終于決定提供一個非線程安全的 stringbuffer 實作,并命名為 stringbuilder。順便,javac 好像大概也是從這個版本開始,把所有用加号連接配接的 string 運算都隐式的改寫成 stringbuilder,也就是說,從 jdk1.5 開始,用加号拼接字元串已經沒有任何性能損失了。
上面說的"用加号拼接字元串已經沒有任何性能損失了"并不嚴謹,嚴格的說,如果沒有循環的情況下,單行用加号拼接字元串是沒有性能損失的,java 編譯器會隐式的替換成 stringbuilder,但在有循環的情況下,編譯器沒法做到足夠智能的替換,仍然會有不必要的性能損耗,是以,用循環拼接字元串的時候,還是老老實實的用 stringbuilder 吧。
MyStringBuffer
根據接口
IStringBuffer
,自己做一個
MyStringBuffer
IStringBuffer
接口:
package character;
public interface IStringBuffer {
public void append(String str); //追加字元串
public void append(char c); //追加字元
public void insert(int pos,char b); //指定位置插入字元
public void insert(int pos,String b); //指定位置插入字元串
public void delete(int start); //從開始位置删除剩下的
public void delete(int start,int end); //從開始位置删除結束位置-1
public void reverse(); //反轉
public int length(); //傳回長度
}
value和capacity
value:
用于存放字元數組
capacity:
容量
無參構造方法: 根據容量初始化
value
public MyStringBuffer(){
value = new char[capacity];
}
package character;
public class MyStringBuffer implements IStringBuffer{
int capacity = 16;
int length = 0;
char[] value;
public MyStringBuffer(){
value = new char[capacity];
}
@Override
public void append(String str) {
// TODO Auto-generated method stub
}
@Override
public void append(char c) {
// TODO Auto-generated method stub
}
@Override
public void insert(int pos, char b) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start) {
// TODO Auto-generated method stub
}
@Override
public void delete(int start, int end) {
// TODO Auto-generated method stub
}
@Override
public void reverse() {
// TODO Auto-generated method stub
}
@Override
public int length() {
// TODO Auto-generated method stub
return 0;
}
}
帶參構造方法
//有參構造方法
public MyStringBuffer(String str){
if(null!=str)
value =str.toCharArray();
length = value.length;
if(capacity<value.length)
capacity = value.length*2;
}
反轉reverse
@Override
public void reverse() {
for (int i = 0; i < length / 2; i++) {
char temp = value[i];
value[i] = value[length - i - 1];
value[length - i - 1] = temp;
}
}
插入insert 和 append
邊界條件判斷
插入之前,首先要判斷的是一些邊界條件。 比如插入位置是否合法,插入的字元串是否為空
- 擴容
- 要判斷是否需要擴容。 如果插入的字元串加上已經存在的内容的總長度超過了容量,那麼就需要擴容。
- 數組的長度是固定的,不能改變的,數組本身不支援擴容。 我們使用變通的方式來解決這個問題。
- 根據需要插入的字元串的長度和已經存在的内容的長度,計算出一個新的容量。 然後根據這個容量,建立一個新的數組,接着把原來的數組的内容,複制到這個新的數組中來。并且讓value這個引用,指向新的數組,進而達到擴容的效果。
- 插入字元串
- 找到要插入字元串的位置,從這個位置開始,把原資料看成兩段,把後半段向後挪動一個距離,這個距離剛好是插入字元串的長度
- 然後把要插入的資料,插入這個挪出來的,剛剛好的位置裡。
- 修改
length
的值
最後修改
的值,是原來的值加上插入字元串的長度length
-
參數是字元的insert方法,通過調用insert(int, char)
也就實作了。insert(int, String)
-
追加,就是在最後位置插入。 是以不需要單獨開發方法,直接調用append
方法,就能達到最後位置插入的效果insert
@Override
public void insert(int pos, String b) {
//邊界條件判斷
if(pos<0)
return;
if(pos>length)
return;
if(null==b)
return;
//擴容
while(length+b.length()>capacity){
capacity = (int) ((length+b.length())*1.5f);
char[] newValue = new char[capacity];
System.arraycopy(value, 0, newValue, 0, length);
value = newValue;
}
char[] cs = b.toCharArray();
//先把已經存在的資料往後移
System.arraycopy(value, pos, value,pos+ cs.length, length-pos);
//把要插入的資料插入到指定位置
System.arraycopy(cs, 0, value, pos, cs.length);
length = length+cs.length;
}
删除 delete
package character;
public class MyStringBuffer implements IStringBuffer{
int capacity = 16;
int length = 0;
char[] value;
public MyStringBuffer(){
value = new char[capacity];
}
//有參構造方法
public MyStringBuffer(String str){
this();
if(null==str)
return;
if(capacity<str.length()){
capacity = value.length*2;
value=new char[capacity];
}
if(capacity>=str.length())
System.arraycopy(str.toCharArray(), 0, value, 0, str.length());
length = str.length();
}
@Override
public void append(String str) {
insert(length,str);
}
@Override
public void append(char c) {
append(String.valueOf(c));
}
@Override
public void insert(int pos, char b) {
insert(pos,String.valueOf(b));
}
@Override
public void delete(int start) {
delete(start,length);
}
@Override
public void delete(int start, int end) {
//邊界條件判斷
if(start<0)
return;
if(start>length)
return;
if(end<0)
return;
if(end>length)
return;
if(start>=end)
return;
System.arraycopy(value, end, value, start, length- end);
length-=end-start;
}
@Override
public void reverse() {
for (int i = 0; i < length/2; i++) {
char temp = value[i];
value[i] = value[length-i-1];
value[length-i-1] = temp;
}
}
@Override
public int length() {
// TODO Auto-generated method stub
return length;
}
@Override
public void insert(int pos, String b) {
//邊界條件判斷
if(pos<0)
return;
if(pos>length)
return;
if(null==b)
return;
//擴容
while(length+b.length()>capacity){
capacity = (int) ((length+b.length())*1.5f);
char[] newValue = new char[capacity];
System.arraycopy(value, 0, newValue, 0, length);
value = newValue;
}
char[] cs = b.toCharArray();
//先把已經存在的資料往後移
System.arraycopy(value, pos, value,pos+ cs.length, length-pos);
//把要插入的資料插入到指定位置
System.arraycopy(cs, 0, value, pos, cs.length);
length = length+cs.length;
}
public String toString(){
char[] realValue = new char[length];
System.arraycopy(value, 0, realValue, 0, length);
return new String(realValue);
}
public static void main(String[] args) {
MyStringBuffer sb = new MyStringBuffer("there light");
System.out.println(sb);
sb.insert(0, "let ");
System.out.println(sb);
sb.insert(10, "be ");
System.out.println(sb);
sb.insert(0, "God Say:");
System.out.println(sb);
sb.append("!");
System.out.println(sb);
sb.append('?');
System.out.println(sb);
sb.reverse();
System.out.println(sb);
sb.reverse();
System.out.println(sb);
sb.delete(0,4);
System.out.println(sb);
sb.delete(4);
System.out.println(sb);
}
}