類中的String,StringBuffer,冒泡排序

來源:互聯網
上載者:User

標籤:buffer   網站   字串長度   another   value   方法   最小值   dem   二進位   

String的擷取功能
int length():擷取字串長度
public char charAt(int index):擷取指定索引處的字元
public int indexOf(int ch)返回指定字元在此字串中第一次出現處的索引
參數:
為什麼不是char類型,
‘a‘--->97 兩個意思一樣
public int indexOf(String str):子字串在大字串中第一次出現的索引
public int lastIndexOf(int ch)返回指定字元在此字串中最後一次出現處的索引
public String substring(int beginIndex):從指定位置開始截取,預設截取到末尾
public String substring(int beginIndex, int endIndex):從指定位置開始截取,截取到指定位置結束(包前,不包後)
String類型的轉換功能:
byte[] getBytes() :將字串轉換成位元組數組
public char[] toCharArray():將字串轉換為字元數組
valueOf(XXX 變數名) ;可以將任意類型轉換為String類型
public String toLowerCase() :將字串轉換小寫
public String toUpperCase():將字串轉換成大寫

編碼和解碼
編碼:String---->看不懂的 byte[] --->二進位 (編碼格式:必須一致)
解碼:byte[] --->String (解碼格式:必須一致)
字串中的其他功能:
替換功能:
String replace(char oldChar, char newChar) :將指定的字元替換以前的字元
String replace(String oldString, String newString):
將以前的子字串替換新的子字串
去除兩端空格
public String trim()
String類的方法CompareTo的源碼

      這個方法會將字串自動轉換成字元數組       this---->s1--->[‘h‘,‘e‘,‘l‘,‘l‘,‘o‘]       anotherString --->s2---->[‘h‘,‘e‘,‘l‘]       public int compareTo(String anotherString) {    //擷取的是當前兩個字元數組的長度    int len1 = value.length;//this.value.length------>5    int len2 = anotherString.value.length;------->3    int lim = Math.min(len1, len2);//擷取最小值Math.min(5,3) ;  =3    char v1[] = value;      //this.value:[‘h‘,‘e‘,‘l‘,‘l‘,‘o‘]    char v2[] = anotherString.value;  // [‘h‘,‘e‘,‘l‘]    int k = 0;    while (k < lim) {   // while(k<3)           while(1<3)  while(2<3)        char c1 = v1[k];    //char c1 = ‘h‘ ,‘e‘,‘l‘        char c2 = v2[k];    //char c2 = ‘h‘ , ‘e‘,‘l‘        if (c1 != c2) {            return c1 - c2;        }        k++;    }    return len1 - len2; //第一個字元數組的長度-第二個字元數組長度            5-3 =2}

多線程中:
成員變數被多個語句共有

        使用同步代碼快 來解決多安全執行緒問題        synchronized(鎖對象){            public synchronized...show(){}  (非靜態方法:鎖對象:this)              多個語句多共用資料進行的操作;        }

StringBuffer:安全執行緒的可變字元序列

安全執行緒--->同步---->執行效率低
舉例:銀行的網站...
安全執行緒和執行效率:兩個問題:在多線程中經常會考慮到問題!
線程不安全(單線程中:使用StringBulider代替StringBuffer)--->不同步---->執行效率高
論壇網站

面試題:
StringBuffer(容器)和String的區別?
前者是安全執行緒的類,可變的字元序列,記憶體中提供字串緩衝區;後者是不可變的字元序列
從記憶體角度考慮:前者優於後者: String類型:在方法區中開闢空間--->占記憶體
StringBuffer :裡面存在初始容量
裡面不斷的追擊字串(append)

StringBuffer的構造方法:
public StringBuffer()構造一個其中不帶字元的字串緩衝區,其初始容量為 16 個字元。
public StringBuffer(int capacity):指定容量大小
public StringBuffer(String str):構造一個字串緩衝區:裡面的容量(字串內容英文)大小:字串長度+16初始容量

擷取功能:
public int capacity():初始容量
int length():返回字串長度
StringBuffer的添加功能:
StringBuffer append(xxx x):將指定的內容追加(末尾追加)到字串緩衝區中,返回StringBuffer本身

        public StringBuffer insert(int index,xxx x):在指定位置處,插入一個新的內容,返回StringBuffer本身 

StringBuffer的刪除功能:

StringBuffer deleteCharAt(int index)  :刪除指定位置出的字元,返回字串緩衝區本身StringBuffer delete(int start, int end) :刪除從指定位置開始到指定位置結束,傳回值字串緩衝區本身

StringBuffer的替換功能:
public StringBuffer replace(int start,int end,String str)
從指定位置開始到指定位置結束,用給定的str字串替換指定的字元內容
StringBuffer的反轉功能:
例:public class StringBufferDemo5 {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer() ;
sb.append("我愛高圓圓") ;
System.out.println("sb:"+sb);
sb.reverse() ;
System.out.println("sb:"+sb);
}
}
StringBuffer的截取功能:
String substring(int start) :從指定位置預設截取到末尾,返回一個新的字串
String substring(int start,int end):從指定位置開始截取到指定位置結束
冒泡排序:
思路: 兩兩比較,大的資料往後方法,第一次比較完畢,最大值出現在最大索引出, 依次這樣比較,即可排序!
例:public class ArrayTest {

public static void main(String[] args) {    // 定義一個數組,靜態初始化    int[] arr = { 24, 69, 80, 57, 13 };    System.out.println("排序前:");    printArray(arr);    bubbleSort(arr);    System.out.println("排序後:");    printArray(arr);}public static void bubbleSort(int[] arr) {    for (int x = 0; x < arr.length - 1; x++) {        for (int y = 0; y < arr.length - 1 - x; y++) {            if (arr[y] > arr[y + 1]) {                // 中間變數的方式互換                int temp = arr[y];                arr[y] = arr[y + 1];                arr[y + 1] = temp;            }        }    }}// 遍曆數組的功能public static void printArray(int[] arr) {    System.out.print("[");    // 遍曆數組    for (int x = 0; x < arr.length; x++) {        if (x == arr.length - 1) {            System.out.println(arr[x] + "]");        } else {            System.out.print(arr[x] + ", ");        }    }}

}

類中的String,StringBuffer,冒泡排序

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.