日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

(JAVA)StringBuffer类

發布時間:2023/12/10 编程问答 41 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (JAVA)StringBuffer类 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
可變數組:StringBuffer類(靜態緩沖區) 提高字符串的操作效率 StringBuffer 底層實現原理是字符數組,沒有final StringBuffer,含有數組char[] value 默認長度是16 在JAVA中,數組是固定長度,一旦創建不能改變 StringBuffer 通過數組的賦值,實現數組的可變性,但數組本身不能改變長度 1.將任意數據類型,添加到緩沖區中append() 2.返回一個StringBuffer,因此可以使用方法調用鏈 3.將任意數據,插入到緩沖區的某一個索引上index(索引,插入內容) 4.緩沖區的刪除方法:delete(int start, int end);deleteCharAt( int index); 5.修改緩沖區的單個字符setCharAt( int index ,char ch); 6.緩沖區字符串翻轉,reverse() 7,緩沖區字符串截取,substring()返回的是String結構 8.將緩沖區變成不可變對象toString() 9.字符串對象與緩沖區對象互轉 字符串變成緩沖區 String s = new String("asdf"); StringBuffer b = new StringBuffer(); b.append(s);緩沖區變成字符串 StringBuffer buffer = new StringBuffer("vbnm"); String string = buffer.toString(); 注意返回值是String 要接收變量 /* public final class StringBufferextends AbstractStringBuilderimplements java.io.Serializable, CharSequenceA cache of the last value returned by toString. Clearedwhenever the StringBuffer is modified.private transient char[] toStringCache;use serialVersionUID from JDK 1.0.2 for interoperability static final long serialVersionUID = 3388685877147921107L;/*** Constructs a string buffer with no characters in it and an* initial capacity of 16 characters.public StringBuffer() {super(16);}/*** Constructs a string buffer with no characters in it and* the specified initial capacity.** @param capacity the initial capacity.* @exception NegativeArraySizeException if the {@code capacity}* argument is less than {@code 0}.public StringBuffer(int capacity) {super(capacity);}/*** Constructs a string buffer initialized to the contents of the* specified string. The initial capacity of the string buffer is* {@code 16} plus the length of the string argument.** @param str the initial contents of the buffer.public StringBuffer(String str) {super(str.length() + 16);append(str);}/*** Constructs a string buffer that contains the same characters* as the specified {@code CharSequence}. The initial capacity of* the string buffer is {@code 16} plus the length of the* {@code CharSequence} argument.* <p>* If the length of the specified {@code CharSequence} is* less than or equal to zero, then an empty buffer of capacity* {@code 16} is returned.** @param seq the sequence to copy.* @since 1.5public StringBuffer(CharSequence seq) {this(seq.length() + 16);append(seq);}*/import java.util.Scanner;public class test {public static void main(String[] args) {//System.out.println(getStringCont());//System.out.println(getString_1());//System.out.println(getString_2());//getString_3();//System.out.println(getString_4());System.out.println(getString_5());}//定義一個共能,從一個字符串中查找另一個字符串出現的次數public static int getStringCont() {Scanner sc1 = new Scanner(System.in);System.out.println("請輸入一個字符串:");String s1 = sc1.nextLine();Scanner sc2 = new Scanner(System.in);System.out.println("請輸入要查找的內容:");String s2 = sc2.nextLine();//定義計數器int count = 0;//定義下角標int index = 0;if (s1.length() == 0 || s2.length() == 0)return count;else {//判斷s1第一次出現的位置,只要第一次出現的位置大于零,就進入循環while((index = s1.indexOf(s2))>0){count++;//新的字符串長度為查找內容后的下角標到結尾s1 = s1.substring(index+s2.length());}}return count;}public static StringBuffer getString_1(){StringBuffer buffer = new StringBuffer("abc");buffer.append("sdf");buffer.insert(2,"bnm");return buffer;}public static StringBuffer getString_2(){StringBuffer buffer_1 = new StringBuffer();buffer_1.append("poiu");System.out.println(buffer_1+"1");//刪除指定下角標buffer_1.deleteCharAt(1);System.out.println(buffer_1+"2");//刪除一堆buffer_1.delete(0,2);return buffer_1;}public static void getString_3(){StringBuffer buffer_2 = new StringBuffer();buffer_2.append("abcdefg");buffer_2.setCharAt(3,'p');System.out.println(buffer_2);}public static StringBuffer getString_4(){String s = "asdf";StringBuffer b = new StringBuffer();b.append(s);return b;}public static String getString_5(){StringBuffer buffer = new StringBuffer("vbnm");String string = buffer.toString();return string;}

總結

以上是生活随笔為你收集整理的(JAVA)StringBuffer类的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。