1. 程式人生 > >JAVA基礎之API && String

JAVA基礎之API && String

API:應用程式編寫(Application Programming Interface) java API指的是JDK中提供的各種功能的Java類 1)首先擁有一份API 2)開啟幫助文件 3)點選顯示,找到索引,看到輸入框 4)你要學習什麼內容,你就在框框裡面輸入什麼內容 舉例:Random 5)看包 java.lang包下的類在使用的時候是不需要導包的 Random是在Java.util下,所以是需要導包的 6)看類的描述 Random類是用於生成隨機數的類 7)看構造方法 Random():無參構造方法 Random r = new Random(); 8)看成員方法 public int nextInt(int n):產生的是一個[0,n)範圍內的隨機數 呼叫方法: 看返回值型別:人家返回什麼型別,你就用什麼型別接收 看方法名:名字不要寫錯了 看形式引數:人家要幾個引數,你就給幾個,人家要什麼資料型別的,你就給什麼資料型別的 int number = r.nextInt(100); 舉例:Scanner:用於獲取鍵盤錄入的資料(基本資料型別,字串資料) public String nextLine():獲取鍵盤錄入的字串資料

2、String:字串類,有多個字元組成的一串資料。 1’:字串其本質是一個字元陣列。 2’:構造方法: String(String original) :把字串資料封裝成字串物件。 String(char[] value) :把字元陣列的資料封裝成字串物件。 String(char[] value, int offset, int count) :把字元陣列中的一部分資料封裝成字串物件。 注意:字串是一種比較特殊的引用資料型別,直接輸出字串物件輸出的是該物件中的資料。

3、通過構造方法建立的字串物件和直接賦值方式建立的字串物件有什麼區別呢? 通過構造方法建立的字串物件是在堆記憶體。 直接賦值方法建立物件是在方法區的常量池中。

4、String的判斷功能介紹 Object:是類層次結構的根類。每個類都使用 Object 作為超類,所有的類都直接或者間接的繼承自該類。 如果一個方法的形式引數是Object,那麼這裡我們就可以傳遞它的任意的子類物件。 String 是Object的子類 boolean equals(Object obj):比較字串的內容是否相同。 boolean equalsIgnoreCase(String str):比較字串內容是否相同,忽略大小寫。 boolean startsWith(String str):判斷字串物件是否以指定的str開頭。 boolesn endsWith(String str):判斷字串物件是否以指定的str結尾。

==:基本資料型別:比較的是基本資料型別的值是否相同。 引用資料型別:比較的是引用資料型別的地址值是否相同。

5、String的獲取功能介紹 int length():獲取字串的長度,其實也就是字元個數。 char charAt(int index):獲取指定索引處的字元。 int indexOf(String str):獲取str在字串物件中第一次出現的索引;若尋找失敗,返回-1。 String substring(int start):從start開始擷取字串。 String substring(int start, int end):從start開始,到end結束擷取字串;包括索引為start的,不包括索引是end的。

6、String類的轉換功能 char[] toCharArray():把字串轉換為字元陣列 String toLowerCase():把字串轉換為小寫字串 String toUpperCase():把字串轉換為大寫字串

7、字串的遍歷: 1’:length()加上charAt() 2’:把字串轉換為字元陣列,然後遍歷

8、字串的其他功能: String trim():去除字串兩端的空格。 String[] split(String str):按照指定符號分割字串。

注意:如果對字串進行拼接操作,每次拼接,都會構建一個新的String物件,既耗時,又浪費空間。 所以一般採用StringBuilder進行拼接,因為它是一個可變的字元序列,字串緩衝區類。 9、String和StringBuilder的區別: String的內容是固定的。 StringBuilder的內容是可變的。

10、StringBuilder的構造方法 StringBuilder() 成員方法: public int capacity():返回當前容量 容量是理論值 public int length():返回長度(字元數) 長度是實際值

11、StringBuilder的常用方法 新增功能:public StringBuilder append(任意型別) 反轉功能:public StringBuilder reserse()

12、 StringBuilder和String的相互轉換 StringBuilder -> String:通過toString()就可以把StringBuilder轉成String。 String -> StringBuilder:通過構造方法就可以實現把String轉成StringBuilder。

public class MyString {
//String類
	public static void main(String[] args) {
		//方式一
		String s1 = new String("hello");
		System.out.println(s1);
		System.out.println("-------------");
		//方式2
		char[] chs = {'h','e','l','l','o'};
		String s2 = new String(chs);
		System.out.println(s2);
		System.out.println("-------------");
		//方式3
		String s3 = new String(chs,0,chs.length-2);//從索引0開始,將chs.length-2個字元拼裝起來
		System.out.println(s3);
		System.out.println("-------------");
		//方式4
		String s4 = "hello";
		
		//區別
		String s5 = new String("hello"); //棧中String s5存放的是new在堆中的地址值,而方法區裡面的地址放到了堆中
		//new在堆記憶體中開闢空間,而hello存放在方法區裡面的,堆記憶體中只有字串hello的地址值
		String s6 = "hello"; //直接放在棧中的,由於是直接賦值的,所以直接從方法區中將字串的地址拿過來
		System.out.println(s5);
		System.out.println(s6);
		System.out.println(s5 == s6);//false
		String s7 = "hello";
		System.out.println(s5 == s7);//false
		System.out.println(s6 == s7);//true
		String s8 = new String("hello");
		System.out.println(s5 == s8); //false
		System.out.println("-------------");
		//字串的內容是儲存在方法區的常量池裡面的,是為了方便字串的重複使用
		
		//建立物件
		String str1 = "hello";
		String str2 = "hello";
		String str3 = "Hello";
		System.out.println(str1.equals(str2));
		System.out.println(str1.equalsIgnoreCase(str3));
		System.out.println(str2.startsWith("abs"));
		System.out.println(str2.endsWith("abs"));
		
		//建立字串物件
		String s = "helloworld";
		System.out.println(s.length());
		System.out.println(s.charAt(0));
		System.out.println(s.charAt(1));
		
		System.out.println(s.indexOf("l"));
		System.out.println(s.indexOf("owo"));//4:表明返回的結果是字串中開始出現時的索引(開頭)
		System.out.println(s.indexOf("ak"));//尋找失敗,返回-1
		System.out.println(s.substring(5));
		System.out.println(s.substring(0,s.length()));
		System.out.println(s.substring(0,6));//
		System.out.println("-------------");
		
		//建立字串物件
		String a = "asngd";
		char[] cha = a.toCharArray();
		for(int x = 0; x < cha.length; x++)
			System.out.println(cha[x]);
		System.out.println("============");
		System.out.println("HelloMusic".toLowerCase());
		System.out.println("HELLOmusic".toUpperCase());
		System.out.println("-------------");
		
		String b1 = "helloword";
		String b2 = "  helloword   ";
		String b3 = "   hello   word   ";
		
		System.out.println(b1.trim());
		System.out.println(b2.trim());
		System.out.println(b3.trim());//中間空格並未去除
		
		String b4 = "aa,bb,cc";
		String[] strArray = b4.split(",");
		for(int x = 0; x < strArray.length; x++)
			System.out.println(strArray[x]);
	
		System.out.println("-------------");
		StringBuilder sb = new StringBuilder();
		System.out.println(sb);//輸出不是地址
		System.out.println(sb.capacity()); //16
		System.out.println(sb.length());//0
		
		System.out.println("-------------");
		StringBuilder sb1 = new StringBuilder();
		StringBuilder sb2 = sb1.append("hello");
		System.out.println(sb1);
		System.out.println(sb2);
		System.out.println(sb1 == sb2);//true指向同一個地方,因為是可變的,所以是在本身上新增物件並返回本省
		
//		sb.append("chen");
//		sb.append("Lin");
		//鏈式程式設計
		sb.append("chen").append("Lin").append("Lin");
		System.out.println(sb);
		
		//反轉功能
		sb.reverse();
		System.out.println(sb);
	}
}