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] + ", ");
		}
	}
}

}