package cn.itcast_01; /* * 線程安全(多線程講解) * 安全 -- 同步 -- 數據是安全的 * 不安全 -- 不一樣步 -- 效率高一些 * 安全和效率問題是永遠困擾咱們的問題。 * 安全:醫院的網站,銀行網站 * 效率:新聞網站,論壇之類的 * * StringBuffer: * 線程安全的可變字符串。 * * StringBuffer和String的區別? * 前者長度和內容可變,後者不可變。 * 若是使用前者作字符串的拼接,不會浪費太多的資源。 * * StringBuffer的構造方法: * public StringBuffer():無參構造方法 * public StringBuffer(int capacity):指定容量的字符串緩衝區對象 * public StringBuffer(String str):指定字符串內容的字符串緩衝區對象 * * StringBuffer的方法: * public int capacity():返回當前容量。 理論值 * public int length():返回長度(字符數)。 實際值 */ public class StringBufferDemo { public static void main(String[] args) { // public StringBuffer():無參構造方法 StringBuffer sb = new StringBuffer(); System.out.println("sb:" + sb); System.out.println("sb.capacity():" + sb.capacity()); System.out.println("sb.length():" + sb.length()); System.out.println("--------------------------"); // public StringBuffer(int capacity):指定容量的字符串緩衝區對象 StringBuffer sb2 = new StringBuffer(50); System.out.println("sb2:" + sb2); System.out.println("sb2.capacity():" + sb2.capacity()); System.out.println("sb2.length():" + sb2.length()); System.out.println("--------------------------"); // public StringBuffer(String str):指定字符串內容的字符串緩衝區對象 StringBuffer sb3 = new StringBuffer("hello"); System.out.println("sb3:" + sb3); System.out.println("sb3.capacity():" + sb3.capacity()); System.out.println("sb3.length():" + sb3.length()); } }
package cn.itcast_02; /* * StringBuffer的添加功能: * public StringBuffer append(String str):能夠把任意類型數據添加到字符串緩衝區裏面,並返回字符串緩衝區自己 * * public StringBuffer insert(int offset,String str):在指定位置把任意類型的數據插入到字符串緩衝區裏面,並返回字符串緩衝區自己 */ public class StringBufferDemo { public static void main(String[] args) { // 建立字符串緩衝區對象 StringBuffer sb = new StringBuffer(); // public StringBuffer append(String str) // StringBuffer sb2 = sb.append("hello"); // System.out.println("sb:" + sb); // System.out.println("sb2:" + sb2); // System.out.println(sb == sb2); // true // 一步一步的添加數據 // sb.append("hello"); // sb.append(true); // sb.append(12); // sb.append(34.56); // 鏈式編程 sb.append("hello").append(true).append(12).append(34.56); System.out.println("sb:" + sb); // public StringBuffer insert(int offset,String // str):在指定位置把任意類型的數據插入到字符串緩衝區裏面,並返回字符串緩衝區自己 sb.insert(5, "world"); System.out.println("sb:" + sb); } }
package cn.itcast_03; /* * StringBuffer的刪除功能 * public StringBuffer deleteCharAt(int index):刪除指定位置的字符,並返回自己 * public StringBuffer delete(int start,int end):刪除從指定位置開始指定位置結束的內容,並返回自己 */ public class StringBufferDemo { public static void main(String[] args) { // 建立對象 StringBuffer sb = new StringBuffer(); // 添加功能 sb.append("hello").append("world").append("java"); System.out.println("sb:" + sb); // public StringBuffer deleteCharAt(int index):刪除指定位置的字符,並返回自己 // 需求:我要刪除e這個字符,腫麼辦? // sb.deleteCharAt(1); // 需求:我要刪除第一個l這個字符,腫麼辦? // sb.deleteCharAt(1); // public StringBuffer delete(int start,int // end):刪除從指定位置開始指定位置結束的內容,並返回自己 // 需求:我要刪除world這個字符串,腫麼辦? // sb.delete(5, 10);//包左不包右 // 需求:我要刪除全部的數據 sb.delete(0, sb.length()); System.out.println("sb:" + sb); } }
package cn.itcast_04; /* * StringBuffer的替換功能: * public StringBuffer replace(int start,int end,String str):從start開始到end用str替換 */ public class StringBufferDemo { public static void main(String[] args) { // 建立字符串緩衝區對象 StringBuffer sb = new StringBuffer(); // 添加數據 sb.append("hello"); sb.append("world"); sb.append("java"); System.out.println("sb:" + sb); // public StringBuffer replace(int start,int end,String // str):從start開始到end用str替換 // 需求:我要把world這個數據替換爲"節日快樂" sb.replace(5, 10, "節日快樂");//包左不包右 System.out.println("sb:" + sb); } }
package cn.itcast_05; /* * StringBuffer的反轉功能: * public StringBuffer reverse() */ public class StringBufferDemo { public static void main(String[] args) { // 建立字符串緩衝區對象 StringBuffer sb = new StringBuffer(); // 添加數據 sb.append("霞青林愛我"); System.out.println("sb:" + sb); // public StringBuffer reverse() sb.reverse(); System.out.println("sb:" + sb); } }
package cn.itcast_06; /* * StringBuffer的截取功能:注意返回值類型再也不是StringBuffer自己了 * public String substring(int start) * public String substring(int start,int end) */ public class StringBufferDemo { public static void main(String[] args) { // 建立字符串緩衝區對象 StringBuffer sb = new StringBuffer(); // 添加元素 sb.append("hello").append("world").append("java"); System.out.println("sb:" + sb); // 截取功能 // public String substring(int start) String s = sb.substring(5); System.out.println("s:" + s); System.out.println("sb:" + sb); // public String substring(int start,int end) String ss = sb.substring(5, 10);//包左不包右,sb不發生變化,append中sb發生變化 System.out.println("ss:" + ss); System.out.println("sb:" + sb); } }
package cn.itcast_07; /* * 爲何咱們要講解類之間的轉換: * A -- B的轉換 * 咱們把A轉換爲B,實際上是爲了使用B的功能。 * B -- A的轉換 * 咱們可能要的結果是A類型,因此還得轉回來。 * * String和StringBuffer的相互轉換? */ public class StringBufferTest { public static void main(String[] args) { // String -- StringBuffer String s = "hello"; // 注意:不能把字符串的值直接賦值給StringBuffer // StringBuffer sb = "hello"; // StringBuffer sb = s; // 方式1:經過構造方法 StringBuffer sb = new StringBuffer(s); // 方式2:經過append()方法 StringBuffer sb2 = new StringBuffer(); sb2.append(s); System.out.println("sb:" + sb); System.out.println("sb2:" + sb2); System.out.println("---------------"); // StringBuffer -- String StringBuffer buffer = new StringBuffer("java"); // String(StringBuffer buffer) // 方式1:經過構造方法 String str = new String(buffer); // 方式2:經過toString()方法 String str2 = buffer.toString(); System.out.println("str:" + str); System.out.println("str2:" + str2); } }
package cn.itcast_07; import java.util.Scanner; /* * 把字符串反轉 */ public class StringBufferTest3 { public static void main(String[] args) { // 鍵盤錄入數據 Scanner sc = new Scanner(System.in); System.out.println("請輸入數據:"); String s = sc.nextLine(); // 方式1:用String作拼接 String s1 = myReverse(s); System.out.println("s1:" + s1); // 方式2:用StringBuffer的reverse()功能 String s2 = myReverse2(s); System.out.println("s2:" + s2); } // 用StringBuffer的reverse()功能 public static String myReverse2(String s) { // StringBuffer sb = new StringBuffer(); // sb.append(s); // StringBuffer sb = new StringBuffer(s); // sb.reverse(); // return sb.toString(); // 簡易版 return new StringBuffer(s).reverse().toString(); } // 用String作拼接 public static String myReverse(String s) { String result = ""; char[] chs = s.toCharArray(); for (int x = chs.length - 1; x >= 0; x--) { // char ch = chs[x]; // result += ch; result += chs[x]; } return result; } }
/* * 判斷一個字符串是不是對稱字符串 * 例如"abc"不是對稱字符串,"aba"、"abba"、"aaa"、"mnanm"是對稱字符串 * * 分析: * 判斷一個字符串是不是對稱的字符串,我只須要把 * 第一個和最後一個比較 * 第二個和倒數第二個比較 * ... * 比較的次數是長度除以2。 */ package stringbuffer; public class Buffer01 { public static void main(String[] args) { String s = "helloworld"; Boolean s2 = reverse(s); System.out.println(s2); } public static boolean reverse(String s){ return new StringBuffer(s).reverse().toString().equals(s); } }
package cn.itcast_08; /* * 面試題: * 1:String,StringBuffer,StringBuilder的區別? * A:String是內容不可變的,而StringBuffer,StringBuilder都是內容可變的。 * B:StringBuffer是同步的,數據安全,效率低;StringBuilder是不一樣步的,數據不安全,效率高 * * 2:StringBuffer和數組的區別? * 兩者均可以看出是一個容器,裝其餘的數據。 * 可是呢,StringBuffer的數據最終是一個字符串數據。 * 而數組能夠放置多種數據,但必須是同一種數據類型的。 * * 3:形式參數問題 * String做爲參數傳遞 * StringBuffer做爲參數傳遞 * * 形式參數: * 基本類型:形式參數的改變不影響實際參數 * 引用類型:形式參數的改變直接影響實際參數 * * 注意: * String做爲參數傳遞,效果和基本類型做爲參數傳遞是同樣的。 */ package stringbuffer; public class Buffer02 { public static void main(String[] args) { String s1 = "hello"; String s2 = "world"; System.out.println(s1 + "---" + s2);// hello---world System.out.println(s1.hashCode()+"---"+s2.hashCode()); //99162322---113318802 change(s1, s2); System.out.println(s1 + "---" + s2);// hello---world System.out.println(s1.hashCode()+"---"+s2.hashCode());//99162322---113318802 StringBuffer sb1 = new StringBuffer("hello"); StringBuffer sb2 = new StringBuffer("world"); System.out.println(sb1 + "---" + sb2);// hello---world System.out.println(sb1.hashCode()+"-----"+sb2.hashCode()); //1704856573-----705927765 change(sb1, sb2); System.out.println(sb1 + "---" + sb2);// hello---worldworld System.out.println(sb1.hashCode()+"-----"+sb2.hashCode()); //1704856573-----705927765 此時sb1的地址仍是sb1的地址,sb2的地址仍是原來的,可是sb2內容發生了變化,可見StirngBuffer的內容能夠發生變化 } public static void change(StringBuffer sb1, StringBuffer sb2) { System.out.println(sb1.hashCode()+"--------change2-------"+sb2.hashCode()); //1704856573--------change2-------705927765 sb1 = sb2; sb2.append(sb1); System.out.println(sb1+"====change2====="+sb2); //worldworld====change2=====worldworld System.out.println(sb1.hashCode()+"------change2------"+sb2.hashCode()); //705927765------change2------705927765此時sb1,sb2都指向的是原sb2的地址,可見StringBuffer可變,String不可變由於上一個另開闢了空間 } public static void change(String s1, String s2) { System.out.println(s1.hashCode()+"-----change1---"+s2.hashCode()); //99162322-----change1---113318802 s1 = s2; s2 = s1 + s2; System.out.println(s1.hashCode()+"---change1----"+s2.hashCode()); //113318802---change1----480013120此時s1指向s2,s2另開闢了空間 System.out.println(s1+"----change1---"+s2); //world----change1---worldworld } }
package cn.itcast_02; /* * Integer的構造方法: * public Integer(int value) * public Integer(String s) * 注意:這個字符串必須是由數字字符組成 */ public class IntegerDemo { public static void main(String[] args) { // 方式1 int i = 100; Integer ii = new Integer(i); System.out.println("ii:" + ii); // 方式2 String s = "100"; // NumberFormatException // String s = "abc"; Integer iii = new Integer(s); System.out.println("iii:" + iii); } }
package cn.itcast_03; /* * int類型和String類型的相互轉換 * * int -- String * String.valueOf(number) * * String -- int * Integer.parseInt(s) */ public class IntegerDemo { public static void main(String[] args) { // int -- String int number = 100; // 方式1 String s1 = "" + number; System.out.println("s1:" + s1); // 方式2 String s2 = String.valueOf(number); System.out.println("s2:" + s2); // 方式3 // int -- Integer -- String Integer i = new Integer(number); String s3 = i.toString(); System.out.println("s3:" + s3); // 方式4 // public static String toString(int i) String s4 = Integer.toString(number); System.out.println("s4:" + s4); System.out.println("-----------------"); // String -- int String s = "100"; // 方式1 // String -- Integer -- int Integer ii = new Integer(s); // public int intValue() int x = ii.intValue(); System.out.println("x:" + x); //方式2 //public static int parseInt(String s) int y = Integer.parseInt(s); System.out.println("y:"+y); } }
package cn.itcast_04; /* * 經常使用的基本進制轉換 * public static String toBinaryString(int i) * public static String toOctalString(int i) * public static String toHexString(int i) * * 十進制到其餘進制 * public static String toString(int i,int radix) * 由這個咱們也看到了進制的範圍:2-36 * 爲何呢?0,...9,a...z * * 其餘進制到十進制 * public static int parseInt(String s,int radix) */ public class IntegerDemo { public static void main(String[] args) { // 十進制到二進制,八進制,十六進制 System.out.println(Integer.toBinaryString(100)); System.out.println(Integer.toOctalString(100)); System.out.println(Integer.toHexString(100)); System.out.println("-------------------------"); // 十進制到其餘進制 System.out.println(Integer.toString(100, 10)); System.out.println(Integer.toString(100, 2)); System.out.println(Integer.toString(100, 8)); System.out.println(Integer.toString(100, 16)); System.out.println(Integer.toString(100, 5)); System.out.println(Integer.toString(100, 7)); System.out.println(Integer.toString(100, -7)); System.out.println(Integer.toString(100, 70)); System.out.println(Integer.toString(100, 1)); System.out.println(Integer.toString(100, 17)); System.out.println(Integer.toString(100, 32)); System.out.println(Integer.toString(100, 37)); System.out.println(Integer.toString(100, 36)); System.out.println("-------------------------"); //其餘進制到十進制 System.out.println(Integer.parseInt("100", 10)); System.out.println(Integer.parseInt("100", 2)); System.out.println(Integer.parseInt("100", 8)); System.out.println(Integer.parseInt("100", 16)); System.out.println(Integer.parseInt("100", 23)); //NumberFormatException //System.out.println(Integer.parseInt("123", 2)); } }
package cn.itcast_05; /* * JDK5的新特性 * 自動裝箱:把基本類型轉換爲包裝類類型 * 自動拆箱:把包裝類類型轉換爲基本類型 * * 注意一個小問題: * 在使用時,Integer x = null;代碼就會出現NullPointerException。 * 建議先判斷是否爲null,而後再使用。 */ public class IntegerDemo { public static void main(String[] args) { // 定義了一個int類型的包裝類類型變量i // Integer i = new Integer(100); Integer ii = 100; ii += 200; System.out.println("ii:" + ii); // 經過反編譯後的代碼 // Integer ii = Integer.valueOf(100); //自動裝箱 // ii = Integer.valueOf(ii.intValue() + 200); //自動拆箱,再自動裝箱 // System.out.println((new StringBuilder("ii:")).append(ii).toString()); Integer iii = null; // NullPointerException if (iii != null) { iii += 1000; System.out.println(iii); } } }
package cn.itcast_06; /* * 看程序寫結果 * * 注意:Integer的數據直接賦值,若是在-128到127之間,會直接從緩衝池裏獲取數據 */ public class IntegerDemo { public static void main(String[] args) { Integer i1 = new Integer(127); Integer i2 = new Integer(127); System.out.println(i1 == i2); System.out.println(i1.equals(i2)); System.out.println("-----------"); Integer i3 = new Integer(128); Integer i4 = new Integer(128); System.out.println(i3 == i4); System.out.println(i3.equals(i4)); System.out.println("-----------"); Integer i5 = 128; Integer i6 = 128; System.out.println(i5 == i6); System.out.println(i5.equals(i6)); System.out.println("-----------"); Integer i7 = 127; Integer i8 = 127; System.out.println(i7 == i8); System.out.println(i7.equals(i8)); // 經過查看源碼,咱們就知道了,針對-128到127之間的數據,作了一個數據緩衝池,若是數據是該範圍內的,每次並不建立新的空間 // Integer ii = Integer.valueOf(127); } }
package cn.itcast_02; /* * public static boolean isUpperCase(char ch):判斷給定的字符是不是大寫字符 * public static boolean isLowerCase(char ch):判斷給定的字符是不是小寫字符 * public static boolean isDigit(char ch):判斷給定的字符是不是數字字符 * public static char toUpperCase(char ch):把給定的字符轉換爲大寫字符 * public static char toLowerCase(char ch):把給定的字符轉換爲小寫字符 */ public class CharacterDemo { public static void main(String[] args) { // public static boolean isUpperCase(char ch):判斷給定的字符是不是大寫字符 System.out.println("isUpperCase:" + Character.isUpperCase('A')); System.out.println("isUpperCase:" + Character.isUpperCase('a')); System.out.println("isUpperCase:" + Character.isUpperCase('0')); System.out.println("-----------------------------------------"); // public static boolean isLowerCase(char ch):判斷給定的字符是不是小寫字符 System.out.println("isLowerCase:" + Character.isLowerCase('A')); System.out.println("isLowerCase:" + Character.isLowerCase('a')); System.out.println("isLowerCase:" + Character.isLowerCase('0')); System.out.println("-----------------------------------------"); // public static boolean isDigit(char ch):判斷給定的字符是不是數字字符 System.out.println("isDigit:" + Character.isDigit('A')); System.out.println("isDigit:" + Character.isDigit('a')); System.out.println("isDigit:" + Character.isDigit('0')); System.out.println("-----------------------------------------"); // public static char toUpperCase(char ch):把給定的字符轉換爲大寫字符 System.out.println("toUpperCase:" + Character.toUpperCase('A')); System.out.println("toUpperCase:" + Character.toUpperCase('a')); System.out.println("-----------------------------------------"); // public static char toLowerCase(char ch):把給定的字符轉換爲小寫字符 System.out.println("toLowerCase:" + Character.toLowerCase('A')); System.out.println("toLowerCase:" + Character.toLowerCase('a')); } }
1:StringBuffer(掌握) (1)用字符串作拼接,比較耗時而且也耗內存,而這種拼接操做又是比較常見的,爲了解決這個問題,Java就提供了 一個字符串緩衝區類。StringBuffer供咱們使用。 (2)StringBuffer的構造方法 A:StringBuffer() B:StringBuffer(int size) C:StringBuffer(String str) (3)StringBuffer的常見功能(本身補齊方法的聲明和方法的解釋) A:添加功能 B:刪除功能 C:替換功能 D:反轉功能 E:截取功能(注意這個返回值) (4)StringBuffer的練習(作一遍) A:String和StringBuffer相互轉換 String -- StringBuffer 構造方法 StringBuffer -- String toString()方法 B:字符串的拼接 C:把字符串反轉 D:判斷一個字符串是否對稱 (5)面試題 小細節: StringBuffer:同步的,數據安全,效率低。 StringBuilder:不一樣步的,數據不安全,效率高。 A:String,StringBuffer,StringBuilder的區別 B:StringBuffer和數組的區別? (6)注意的問題: String做爲形式參數,StringBuffer做爲形式參數。 2:數組高級以及Arrays(掌握) (1)排序 A:冒泡排序 相鄰元素兩兩比較,大的日後放,第一次完畢,最大值出如今了最大索引處。同理,其餘的元素就能夠排好。 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; } } } } B:選擇排序 把0索引的元素,和索引1之後的元素都進行比較,第一次完畢,最小值出如今了0索引。同理,其餘的元素就能夠排好。 public static void selectSort(int[] arr) { for(int x=0; x<arr.length-1; x++) { for(int y=x+1; y<arr.length; y++) { if(arr[y] < arr[x]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } } } } (2)查找 A:基本查找 針對數組無序的狀況 public static int getIndex(int[] arr,int value) { int index = -1; for(int x=0; x<arr.length; x++) { if(arr[x] == value) { index = x; break; } } return index; } B:二分查找(折半查找) 針對數組有序的狀況(千萬不要先排序,在查找) public static int binarySearch(int[] arr,int value) { int min = 0; int max = arr.length-1; int mid = (min+max)/2; while(arr[mid] != value) { if(arr[mid] > value) { max = mid - 1; }else if(arr[mid] < value) { min = mid + 1; } if(min > max) { return -1; } mid = (min+max)/2; } return mid; } (3)Arrays工具類 A:是針對數組進行操做的工具類。包括排序和查找等功能。 B:要掌握的方法(本身補齊方法) 把數組轉成字符串: 排序: 二分查找: (4)Arrays工具類的源碼解析 (5)把字符串中的字符進行排序 舉例: "edacbgf" 獲得結果 "abcdefg" 3:Integer(掌握) (1)爲了讓基本類型的數據進行更多的操做,Java就爲每種基本類型提供了對應的包裝類類型 byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean (2)Integer的構造方法 A:Integer i = new Integer(100); B:Integer i = new Integer("100"); 注意:這裏的字符串必須是由數字字符組成 (3)String和int的相互轉換 A:String -- int Integer.parseInt("100"); B:int -- String String.valueOf(100); (4)其餘的功能(瞭解) 進制轉換 (5)JDK5的新特性 自動裝箱 基本類型--引用類型 自動拆箱 引用類型--基本類型 把下面的這個代碼理解便可: Integer i = 100; i += 200; (6)面試題 -128到127之間的數據緩衝池問題 4:Character(瞭解) (1)Character構造方法 Character ch = new Character('a'); (2)要掌握的方法:(本身補齊) A:判斷給定的字符是不是大寫 B:判斷給定的字符是不是小寫 C:判斷給定的字符是不是數字字符 D:把給定的字符轉成大寫 E:把給定的字符轉成小寫 (3)案例: 統計字符串中大寫,小寫及數字字符出現的次數