<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                >[success] # Java -- StringBuilder 和 StringBuffer 1. 由于**String**類描述的字符串內容是個**常量不可改變**,當需要在Java代碼中描述**大量類似的字符串時**,只能**單獨申請和存儲**,此時會造成內存**空間的浪費** ~~~ public class TestStr { public static void main(String[] args) { // 下面相當于開辟了三次空間 String 類型修改是字符串本身是個常量不會改變 String str = "123"; str = "123456"; str = "123456789"; } } ~~~ 2. 當我們使用大數據量字符串拼接時候發現會出現卡頓例如下面的代碼會卡頓一會才會出現結果 ~~~ public class StringBuilderDemo1 { public static void main(String[] args) { String s = ""; for (int i = 0; i < 1000000; i++) { s = s + "abc"; } System.out.println(s); } } ~~~ * 這種情況下就可以使用`StringBuilder` ~~~ public class StringBuilderDemo2 { public static void main(String[] args) { StringBuilder sb = new StringBuilder(); for (int i = 0; i < 1000000; i++) { sb.append("abc"); } System.out.println(sb); } } ~~~ 3. 使用**java.lang.StringBuilder**類和**java.lang.StringBuffer**類來描述字符序列可**以改變的字符串** 解決每次都要單獨申請和儲存的問題 4. `StringBuffffer`類是從jdk1.0開始存在,**屬于線程安全的類,因此效率比較低**,`StringBuilder`類是從jdk1.5開始存在,**屬于非線程安全的類,效率比較高**。 5. 因為`StringBuilder`是Java已經寫好的類java在底層對他做了一些特殊處理。**打印對象不是地址值而是屬性值。** 6. 總結在使用場景上,**字符串的拼接**、**字符串的反轉** 這兩個場景推薦使用`StringBuilder` >[info] ## 使用 StringBuilder * 常用的構造方法 |方法聲明| 功能介紹| |--|--| |StringBuilder() |使用無參方式構造對象,容量為16| |StringBuilder(int capacity) |根據參數指定的容量來構造對象,容量為參數指定大小| |StringBuilder(String str) |根據參數指定的字符串來構造對象,容量為:16+字符串長度| * 構造方法使用 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(29); StringBuilder sb3 = new StringBuilder("hello"); } } ~~~ * 常見的成員方法 |方法聲明 |功能介紹| |--|--| |int capacity() |用于返回調用對象的容量| |int length() |用于返回字符串的長度,也就是字符的個數| |StringBuilder insert(int offset, String str) |插入字符串并返回調用對象的引用,就是自己| |StringBuilder append(String str) |追加字符串| |StringBuilder deleteCharAt(int index)|將當前字符串中下標為index位置的單個字符刪除| |StringBuilder delete(int start,int end) |刪除字符串| |StringBuilder replace(int start,int end,String str)|替換字符串| |StringBuilder reverse() |字符串反轉| >[danger] ##### capacity 和 length 用法 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder(); StringBuilder sb2 = new StringBuilder(29); StringBuilder sb3 = new StringBuilder("hello"); // 打印字符串 自動調用toString方法 啥也沒有 System.out.println(sb1); // '' System.out.println(sb2); // '' System.out.println(sb3); // hello // sb1 印容量capacity 和 長度length System.out.println(sb1.capacity()); // 16 System.out.println(sb1.length()); // 0 // sb2 印容量capacity 和 長度length System.out.println(sb2.capacity()); // 29 System.out.println(sb2.length()); // 0 // sb3 印容量capacity 和 長度length System.out.println(sb3.capacity()); // 16 +5 = 21 System.out.println(sb3.length()); // 5 } } ~~~ >[danger] ##### insert 插入字符串 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); // 向下標為0的位置插入字符串內容"abcd",也就是向開頭位置插入字符串內容 StringBuilder sb2 = sb1.insert(0, "abcd"); System.out.println(sb1); // abcdhello System.out.println(sb2); // abcdhello System.out.println(sb2 == sb1); // true // 會改變原字符串 // 從第四字符位置插入字符串"1234" sb2.insert(4, "1234"); System.out.println(sb2); // abcd1234hello // 從末尾添加字符串 // 向末尾位置插入字符串"ABCD" sb2.insert(sb2.length(), "ABCD"); System.out.println(sb2); // abcd1234helloABCD } } ~~~ >[danger] ##### append -- 向末尾位置追加 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); // 向末尾位置追加字符串內容 sb1.append("aaa"); System.out.println(sb1); // helloaaa } } ~~~ >[danger] ##### deleteCharAt -- 刪除下角標對應位置字符 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); // 刪除刪除下標為1的單個字符 System.out.println(sb1.deleteCharAt(1)); // hllo StringBuilder sb2 = new StringBuilder("abcdef"); // 使用循環依次刪除從腳本為2字符串 int index = 2; for (int i = index; i < sb2.length(); i++) { // 刪除一個字符后就跳過一個字符繼續刪除,因為每當刪除一個字符后后面的字符會向前補位,因為下標會發生變化 // 始終刪除下標為2的字符 sb2.deleteCharAt(i); } System.out.println(sb2); // abdf // 利用倒序刪除 StringBuilder sb3 = new StringBuilder("abcdef"); int index1 = 2; for (int i = sb3.length() - 1; i > index1; i--) { sb3.deleteCharAt(i); } System.out.println(sb3); // abc } } ~~~ >[danger] ##### delete -- 刪除字符串 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); // 刪除刪除0 - 2的字符 System.out.println(sb1.delete(0, 2)); // llo StringBuilder sb2 = new StringBuilder("hello"); // 刪除刪除1 - 結尾的字符 System.out.println(sb2.delete(1, sb2.length())); // h } } ~~~ >[danger] ##### setCharAt -- 內容的修改 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); sb1.setCharAt(0, 'z'); System.out.println(sb1); // zello } } ~~~ >[danger] ##### replace -- 替換字符 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); sb1.replace(0, 2, "asdfg"); System.out.println(sb1); // asdfgllo } } ~~~ >[danger] ##### reverse -- 字符串反轉 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); sb1.reverse(); System.out.println(sb1); // olleh } } ~~~ >[danger] ##### indexOf/lastIndexOf -- 根據字符串找到對應位置 ~~~ public class TestStr { public static void main(String[] args) { StringBuilder sb1 = new StringBuilder("hello"); int l = sb1.indexOf("l"); int ll = sb1.lastIndexOf("l"); System.out.println(l); // 2 System.out.println(ll); // 3 } } ~~~ >[info] ## 綜合案例 * 將鍵盤錄入的值進行反轉并且轉換為字符串(`StringBuilder`場景反轉) ~~~ import java.util.Scanner; public class StringTest { public static void main(String[] args) { Scanner sc = new Scanner(System.in); String str = sc.next(); StringBuilder strB = new StringBuilder(); str = strB.append(str).reverse().toString(); System.out.print(str); } } ~~~
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看