日本免费全黄少妇一区二区三区-高清无码一区二区三区四区-欧美中文字幕日韩在线观看-国产福利诱惑在线网站-国产中文字幕一区在线-亚洲欧美精品日韩一区-久久国产精品国产精品国产-国产精久久久久久一区二区三区-欧美亚洲国产精品久久久久

字符串是什么意思 字符怎么輸入( 五 )



9.8 比較字符串是否是同一個(gè)對(duì)象str1和str2的內(nèi)容一定是一樣的!但是,并不是同一個(gè)字符串對(duì)象 。
==用于判斷是否是同一個(gè)字符串對(duì)象 。
是否是同一個(gè)對(duì)象-特例String str1 = "the light";String str3 = "the light";System.out.println( str1==str3);
一般說來 , 編譯器每碰到一個(gè)字符串的字面值 , 就會(huì)創(chuàng)建一個(gè)新的對(duì)象 。所以在第6行會(huì)創(chuàng)建了一個(gè)新的字符串"the light"; 但是在第7行,編譯器發(fā)現(xiàn)已經(jīng)存在現(xiàn)成的"the light",那么就直接拿來使用,而沒有進(jìn)行重復(fù)創(chuàng)建 。
內(nèi)容是否相同使用equals進(jìn)行字符串內(nèi)容的比較,必須大小寫一致; equalsIgnoreCase,忽略大小寫判斷內(nèi)容是否一致 。
是否以子字符串開始或者結(jié)束startsWith //以...開始;endsWith //以...結(jié)束 。
練習(xí)-比較字符串創(chuàng)建一個(gè)長(zhǎng)度是100的字符串?dāng)?shù)組,使用長(zhǎng)度是2的隨機(jī)字符填充該字符串?dāng)?shù)組 。統(tǒng)計(jì)這個(gè)字符串?dāng)?shù)組里重復(fù)的字符串有多少種
學(xué)了hash再來做吧..
9.9 StringBuffer*
StringBuffer是可變長(zhǎng)的字符串
追加 刪除 插入 反轉(zhuǎn)append追加;delete 刪除;insert 插入;reverse 反轉(zhuǎn).
package character;public class TestString {public static void main(String[] args) {String str1 = "let there ";StringBuffer sb = new StringBuffer(str1); //根據(jù)str1創(chuàng)建一個(gè)StringBuffer對(duì)象sb.append("be light"); //在最后追加System.out.println(sb);sb.delete(4, 10);//刪除4-10之間的字符System.out.println(sb);sb.insert(4, "there ");//在4這個(gè)位置插入 thereSystem.out.println(sb);sb.reverse(); //反轉(zhuǎn)System.out.println(sb);}}
長(zhǎng)度 容量為什么StringBuffer可以變長(zhǎng)? 和String內(nèi)部是一個(gè)字符數(shù)組一樣,StringBuffer也維護(hù)了一個(gè)字符數(shù)組 。但是 , 這個(gè)字符數(shù)組,留有冗余長(zhǎng)度 。
比如說new StringBuffer("the") , 其內(nèi)部的字符數(shù)組的長(zhǎng)度,是19,而不是3,這樣調(diào)用插入和追加,在現(xiàn)成的數(shù)組的基礎(chǔ)上就可以完成了 。如果追加的長(zhǎng)度超過了19 , 就會(huì)分配一個(gè)新的數(shù)組,長(zhǎng)度比原來多一些,把原來的數(shù)據(jù)復(fù)制到新的數(shù)組中,看上去 數(shù)組長(zhǎng)度就變長(zhǎng)了 。length: “the”的長(zhǎng)度 3;capacity: 分配的總空間 19 。
public class TestString {public static void main(String[] args) {String str1 = "the";StringBuffer sb = new StringBuffer(str1);System.out.println(sb.length()); //內(nèi)容長(zhǎng)度System.out.println(sb.capacity());//總空間} }練習(xí)-StringBuffer性能String與StringBuffer的性能區(qū)別?
生成10位長(zhǎng)度的隨機(jī)字符串 然后,先使用String的+,連接10000個(gè)隨機(jī)字符串,計(jì)算消耗的時(shí)間 然后,再使用StringBuffer連接10000個(gè)隨機(jī)字符串,計(jì)算消耗的時(shí)間
提示: 使用System.currentTimeMillis() 獲取當(dāng)前時(shí)間(毫秒)
public static void main(String[] args) {long start = System.currentTimeMillis();String s1 = "";for (int i=0;i<10000;i++){s1 += 'a';}long end = System.currentTimeMillis();System.out.println("string總共耗時(shí)了:"+(end-start));long start2 = System.currentTimeMillis();StringBuffer sb = new StringBuffer();for (int i=0;i<10000;i++){sb.append('a');}long end2 = System.currentTimeMillis();System.out.println("stringbuffer總共耗時(shí)了:"+(end2-start2));}
//java學(xué)習(xí)交流:737251827 進(jìn)入可領(lǐng)取學(xué)習(xí)資源及對(duì)十年開發(fā)經(jīng)驗(yàn)大佬提問,免費(fèi)解答!
練習(xí)-MyStringBuffer根據(jù)接口IStringBuffer,自己做一個(gè)MyStringBuffer
IStringBuffer.java
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); //從開始位置刪除結(jié)束位置-1public void reverse(); //反轉(zhuǎn)public int length(); //返回長(zhǎng)度}MyStringBuffer.java

推薦閱讀