Scanner類html
Random類java
ArrayList類程序員
String類正則表達式
Arrays類編程
Math類數組
Object類安全
時間相關類app
System類dom
API(Application Programming Interface),應用程序編程接口。Java API是一本程序員的 字典 ,是JDK中提供給 咱們使用的類的說明文檔。這些類將底層的代碼實現封裝了起來,咱們不須要關心這些類是如何實現的,只須要學習這些類如何使用便可。因此咱們能夠經過查詢API的方式,來學習Java提供的類,並得知如何使用它們。
使用步驟:
1. 打開幫助文檔。
2. 點擊顯示,找到索引,看到輸入框。
3. 你要找誰?在輸入框裏輸入,而後回車。
4. 看包。java.lang下的類不須要導包,其餘須要。
5. 看類的解釋和說明。
6. 學習構造方法。
7. 使用成員方法。
Scanner類的功能:能夠實現鍵盤輸入數據,到程序當中。
使用步驟:
1. 導包:import 包路徑.類名稱;
若是須要使用的目標類,和當前類位於同一個包下,則能夠省略導包語句不寫。
只有java.lang包下的內容不須要導包,其餘的包都須要import語句。
2. 建立:類名稱 對象名 = new 類名稱();
3. 使用:對象名.成員方法名()
1 import java.util.Scanner; // 1. 導包 2 3 public static void main(String[] args) { 4 // 2. 建立 5 // 備註:System.in表明從鍵盤進行輸入 6 Scanner sc = new Scanner(System.in); 7 8 // 3. 獲取鍵盤輸入的int數字 9 int num = sc.nextInt(); 10 System.out.println("輸入的int數字是:" + num); 11 12 // 4. 獲取鍵盤輸入的字符串 13 String str = sc.next(); 14 System.out.println("輸入的字符串是:" + str); 15 }
方法:
獲取鍵盤輸入的一個int數字:int num = sc.nextInt();
獲取鍵盤輸入的一個字符串:String str = sc.next();
其他方法經過API來查找。
Random類:用來生成僞隨機數字。
使用步驟:
1. 導包:import java.util.Random;
2. 建立:Random r = new Random(); // 小括號當中留空便可
3. 使用
獲取一個隨機的int數字(範圍是int全部範圍,有正負兩種):int num = r.nextInt()
獲取一個隨機的int數字(參數表明了範圍,左閉右開區間):int num = r.nextInt(3)。實際上表明的含義是:[0,3),也就是0~2
1 // 1,導包 2 import java.util.Random; 3 public class Demo01Random { 4 5 public static void main(String[] args) { 6 // 建立對象 7 Random r = new Random(); 8 9 // 使用建立的對象調用方法 10 int num = r.nextInt(); 11 System.out.println("隨機數是:" + num); 12 } 13 }
1 import java.util.Random; 2 import java.util.Scanner; 3 4 /* 5 題目: 6 用代碼模擬猜數字的小遊戲。 7 8 思路: 9 1. 首先須要產生一個隨機數字,而且一旦產生再也不變化。用Random的nextInt方法 10 2. 須要鍵盤輸入,因此用到了Scanner 11 3. 獲取鍵盤輸入的數字,用Scanner當中的nextInt方法 12 4. 已經獲得了兩個數字,判斷(if)一下: 13 若是太大了,提示太大,而且重試; 14 若是過小了,提示過小,而且重試; 15 若是猜中了,遊戲結束。 16 5. 重試就是再來一次,循環次數不肯定,用while(true)。 17 */ 18 public class Demo04RandomGame { 19 20 public static void main(String[] args) { 21 Random r = new Random(); 22 int randomNum = r.nextInt(100) + 1; // [1,100] 23 Scanner sc = new Scanner(System.in); 24 25 while (true) { 26 System.out.println("請輸入你猜想的數字:"); 27 int guessNum = sc.nextInt(); // 鍵盤輸入猜想的數字 28 29 if (guessNum > randomNum) { 30 System.out.println("太大了,請重試。"); 31 } else if (guessNum < randomNum) { 32 System.out.println("過小了,請重試。"); 33 } else { 34 System.out.println("恭喜你,猜中啦!"); 35 break; // 若是猜中,再也不重試 36 } 37 } 38 39 System.out.println("遊戲結束。"); 40 } 41 42 }
方法:
在API中查看
引例:到目前爲止,咱們想存儲對象數據,選擇的容器,只有對象數組。而數組的長度是固定的,沒法適應數據變化的需求。爲了解決這個問題,Java提供了另外一個容器 java.util.ArrayList 集合類,讓咱們能夠更便捷的存儲和操做對象數據。
1 // 類 2 public class Person { 3 4 private String name; 5 private int age; 6 7 public Person() { 8 } 9 10 public Person(String name, int age) { 11 this.name = name; 12 this.age = age; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 } 31 32 ------------------------------------------- 33 // 對象 34 /* 35 題目: 36 定義一個數組,用來存儲3個Person對象。 37 38 數組有一個缺點:一旦建立,程序運行期間長度不能夠發生改變。 39 */ 40 public class Demo01Array { 41 42 public static void main(String[] args) { 43 // 首先建立一個長度爲3的數組,裏面用來存放Person類型的對象 44 Person[] array = new Person[3]; 45 46 Person one = new Person("迪麗熱巴", 18); 47 Person two = new Person("古力娜扎", 28); 48 Person three = new Person("瑪爾扎哈", 38); 49 50 // 將one當中的地址值賦值到數組的0號元素位置 51 array[0] = one; 52 array[1] = two; 53 array[2] = three; 54 55 System.out.println(array[0]); // 地址值 56 System.out.println(array[1]); // 地址值 57 System.out.println(array[2]); // 地址值 58 59 System.out.println(array[1].getName()); // 古力娜扎 60 } 61 }
java.util.ArrayList 是大小可變的數組的實現,存儲在內的數據稱爲元素。此類提供一些方法來操做內部存儲 的元素。 ArrayList 中可不斷添加元素,其大小也自動增加。
1,查看類:java.util.ArrayList <E> :該類須要 import導入使後使用。
<E> ,表示一種指定的數據類型,叫作泛型。 E ,取自Element(元素)的首字母。在出現 E 的地方,咱們使 用一種引用數據類型將其替換便可,表示咱們將存儲哪一種引用類型的元素。代碼以下:ArrayList <String>、ArrayList <People>
2,查看構造方法
public ArrayList() :構造一個內容爲空的集合。
基本格式:ArrayList<String> list = new ArrayList<String>();
在JDK 7後,右側泛型的尖括號以內能夠留空,可是<>仍然要寫。簡化格式:ArrayList<String> list = new ArrayList<>();
3,查當作員方法
public boolean add(E e) : 將指定的元素添加到此集合的尾部。 參數 E e ,在構造ArrayList對象時, <E> 指定了什麼數據類型,那麼 add(E e) 方法中,只能添加什麼數據 類型的對象。
1 import java.util.ArrayList; 2 /* 3 數組的長度不能夠發生改變。 4 可是ArrayList集合的長度是能夠隨意變化的。 5 6 對於ArrayList來講,有一個尖括號<E>表明泛型。 7 泛型:也就是裝在集合當中的全部元素,全都是統一的什麼類型。 8 注意:泛型只能是引用類型,不能是基本類型。 9 10 注意事項: 11 對於ArrayList集合來講,直接打印獲得的不是地址值,而是內容。 12 若是內容是空,獲得的是空的中括號:[] 13 */ 14 public class Demo02ArrayList { 15 16 public static void main(String[] args) { 17 // 建立了一個ArrayList集合,集合的名稱是list,裏面裝的全都是String字符串類型的數據 18 // 備註:從JDK 1.7+開始,右側的尖括號內部能夠不寫內容,可是<>自己仍是要寫的。 19 ArrayList<String> list = new ArrayList<>(); 20 System.out.println(list); // [] 21 22 // 向集合當中添加一些數據,須要用到add方法。 23 list.add("趙麗穎"); 24 System.out.println(list); // [趙麗穎] 25 26 list.add("迪麗熱巴"); 27 list.add("古力娜扎"); 28 list.add("瑪爾扎哈"); 29 System.out.println(list); // [趙麗穎, 迪麗熱巴, 古力娜扎, 瑪爾扎哈] 30 31 // list.add(100); // 錯誤寫法!由於建立的時候尖括號泛型已經說了是字符串,添加進去的元素就必須都是字符串才行 32 } 33 }
public boolean add(E e):向集合當中添加元素,參數的類型和泛型一致。返回值表明添加是否成功。
備註:對於ArrayList集合來講,add添加動做必定是成功的,因此返回值可用可不用。
可是對於其餘集合(從此學習)來講,add添加動做不必定成功。
public E get(int index):從集合當中獲取元素,參數是索引編號,返回值就是對應位置的元素。
public E remove(int index):從集合當中刪除元素,參數是索引編號,返回值就是被刪除掉的元素。
public int size():獲取集合的尺寸長度,返回值是集合中包含的元素個數。
1 import java.util.ArrayList; 2 3 /* 4 ArrayList當中的經常使用方法有: 5 6 public boolean add(E e):向集合當中添加元素,參數的類型和泛型一致。返回值表明添加是否成功。 7 備註:對於ArrayList集合來講,add添加動做必定是成功的,因此返回值可用可不用。 8 可是對於其餘集合(從此學習)來講,add添加動做不必定成功。 9 10 public E get(int index):從集合當中獲取元素,參數是索引編號,返回值就是對應位置的元素。 11 12 public E remove(int index):從集合當中刪除元素,參數是索引編號,返回值就是被刪除掉的元素。 13 14 public int size():獲取集合的尺寸長度,返回值是集合中包含的元素個數。 15 */ 16 public class Demo03ArrayListMethod { 17 18 public static void main(String[] args) { 19 ArrayList<String> list = new ArrayList<>(); 20 System.out.println(list); // [] 21 22 // 向集合中添加元素:add 23 boolean success = list.add("柳巖"); 24 System.out.println(list); // [柳巖] 25 System.out.println("添加的動做是否成功:" + success); // true 26 27 list.add("高圓圓"); 28 list.add("趙又廷"); 29 list.add("李小璐"); 30 list.add("賈乃亮"); 31 System.out.println(list); // [柳巖, 高圓圓, 趙又廷, 李小璐, 賈乃亮] 32 33 // 從集合中獲取元素:get。索引值從0開始 34 String name = list.get(2); 35 System.out.println("第2號索引位置:" + name); // 趙又廷 36 37 // 從集合中刪除元素:remove。索引值從0開始。 38 String whoRemoved = list.remove(3); 39 System.out.println("被刪除的人是:" + whoRemoved); // 李小璐 40 System.out.println(list); // [柳巖, 高圓圓, 趙又廷, 賈乃亮] 41 42 // 獲取集合的長度尺寸,也就是其中元素的個數 43 int size = list.size(); 44 System.out.println("集合的長度是:" + size); 45 } 46 }
1 import java.util.ArrayList; 2 3 public class Demo04ArrayListEach { 4 5 public static void main(String[] args) { 6 ArrayList<String> list = new ArrayList<>(); 7 list.add("迪麗熱巴"); 8 list.add("古力娜扎"); 9 list.add("瑪爾扎哈"); 10 11 // 遍歷集合 12 for (int i = 0; i < list.size(); i++) { 13 System.out.println(list.get(i)); 14 } 15 } 16 }
爲何引入包裝類:若是但願向集合ArrayList當中存儲基本類型數據,必須使用基本類型對應的「包裝類」。
從JDK 1.5+開始,支持自動裝箱、自動拆箱。
自動裝箱:基本類型 --> 包裝類型
自動拆箱:包裝類型 --> 基本類型
1 import java.util.ArrayList; 2 3 /* 4 若是但願向集合ArrayList當中存儲基本類型數據,必須使用基本類型對應的「包裝類」。 5 6 基本類型 包裝類(引用類型,包裝類都位於java.lang包下) 7 byte Byte 8 short Short 9 int Integer 【特殊】 10 long Long 11 float Float 12 double Double 13 char Character 【特殊】 14 boolean Boolean 15 16 從JDK 1.5+開始,支持自動裝箱、自動拆箱。 17 18 自動裝箱:基本類型 --> 包裝類型 19 自動拆箱:包裝類型 --> 基本類型 20 */ 21 public class Demo05ArrayListBasic { 22 23 public static void main(String[] args) { 24 ArrayList<String> listA = new ArrayList<>(); 25 // 錯誤寫法!泛型只能是引用類型,不能是基本類型 26 // ArrayList<int> listB = new ArrayList<>(); 27 28 ArrayList<Integer> listC = new ArrayList<>(); 29 listC.add(100); 30 listC.add(200); 31 System.out.println(listC); // [100, 200] 32 33 int num = listC.get(1); 34 System.out.println("第1號元素是:" + num);// 200 35 } 36 }
使用ArrayList存儲自定義對象
1 // 自定義對象 2 public class Student { 3 4 private String name; 5 private int age; 6 7 public Student() { 8 } 9 10 public Student(String name, int age) { 11 this.name = name; 12 this.age = age; 13 } 14 15 public String getName() { 16 return name; 17 } 18 19 public void setName(String name) { 20 this.name = name; 21 } 22 23 public int getAge() { 24 return age; 25 } 26 27 public void setAge(int age) { 28 this.age = age; 29 } 30 } 31 32 33 34 ----------------------------------------- 35 package g.demo05; 36 37 import java.util.ArrayList; 38 39 /* 40 題目: 41 自定義4個學生對象,添加到集合,並遍歷。 42 43 思路: 44 1. 自定義Student學生類,四個部分。 45 2. 建立一個集合,用來存儲學生對象。泛型:<Student> 46 3. 根據類,建立4個學生對象。 47 4. 將4個學生對象添加到集合中:add 48 5. 遍歷集合:for、size、get 49 */ 50 public class Demo02ArrayListStudent { 51 52 public static void main(String[] args) { 53 ArrayList<Student> list = new ArrayList<>(); 54 55 Student one = new Student("洪七公", 20); 56 Student two = new Student("歐陽鋒", 21); 57 Student three = new Student("黃藥師", 22); 58 Student four = new Student("段智興", 23); 59 60 list.add(one); 61 list.add(two); 62 list.add(three); 63 list.add(four); 64 65 // 遍歷集合 66 for (int i = 0; i < list.size(); i++) { 67 Student stu = list.get(i); 68 System.out.println("姓名:" + stu.getName() + ",年齡" + stu.getAge()); 69 } 70 } 71 }
使用ArrayList做爲方法的參數和返回值
1 import java.util.ArrayList; 2 import java.util.Random; 3 /* 4 題目: 5 用一個大集合存入20個隨機數字,而後篩選其中的偶數元素,放到小集合當中。 6 要求使用自定義的方法來實現篩選。 7 8 分析: 9 1. 須要建立一個大集合,用來存儲int數字:<Integer> 10 2. 隨機數字就用Random nextInt 11 3. 循環20次,把隨機數字放入大集合:for循環、add方法 12 4. 定義一個方法,用來進行篩選。 13 篩選:根據大集合,篩選符合要求的元素,獲得小集合。 14 三要素 15 返回值類型:ArrayList小集合(裏面元素個數不肯定) 16 方法名稱:getSmallList 17 參數列表:ArrayList大集合(裝着20個隨機數字) 18 5. 判斷(if)是偶數:num % 2 == 0 19 6. 若是是偶數,就放到小集合當中,不然不放。 20 */ 21 public class Demo04ArrayListReturn { 22 23 public static void main(String[] args) { 24 ArrayList<Integer> bigList = new ArrayList<>(); 25 Random r = new Random(); 26 for (int i = 0; i < 20; i++) { 27 int num = r.nextInt(100) + 1; // 1~100 28 bigList.add(num); 29 } 30 31 ArrayList<Integer> smallList = getSmallList(bigList); 32 33 System.out.println("偶數總共有多少個:" + smallList.size()); 34 for (int i = 0; i < smallList.size(); i++) { 35 System.out.println(smallList.get(i)); 36 } 37 } 38 39 // 這個方法,接收大集合參數,返回小集合結果 40 public static ArrayList<Integer> getSmallList(ArrayList<Integer> bigList) { 41 // 建立一個小集合,用來裝偶數結果 42 ArrayList<Integer> smallList = new ArrayList<>(); 43 for (int i = 0; i < bigList.size(); i++) { 44 int num = bigList.get(i); 45 if (num % 2 == 0) { 46 smallList.add(num); 47 } 48 } 49 return smallList; 50 } 51 }
java.lang.String 類表明字符串。Java程序中全部的字符串文字(例如 "abc" )均可以被看做是實現此類的實例。
String 類中包括用於檢查各個字符串的方法,好比用於比較字符串,搜索字符串,提取子字符串以及建立具備翻 譯爲大寫或小寫的全部字符的字符串的副本。
1. 字符串不變:字符串的值在建立後不能被更改。
2. 由於String對象是不可變的,因此它們能夠被共享
3. "abc" 等效於 char[] data={ 'a' , 'b' , 'c' }
1,查看類 java.lang.String :此類不須要導入。
2,查看構造方法
三種構造方法:
1,public String():建立一個空白字符串,不含有任何內容。
2,public String(char[ ] array):根據字符數組的內容,來建立對應的字符串。
3,public String(byte[ ] array):根據字節數組的內容,來建立對應的字符串。
4,另外,還有一種不須要使用構造方法來建立對象:String str = "Hello"; // 右邊直接用雙引號
注意:
直接寫上雙引號,就是字符串對象。
字符串常量池:程序當中直接寫上的雙引號字符串,就在字符串常量池中。
1 /* 2 建立字符串的常見3+1種方式。 3 三種構造方法: 4 public String():建立一個空白字符串,不含有任何內容。 5 public String(char[] array):根據字符數組的內容,來建立對應的字符串。 6 public String(byte[] array):根據字節數組的內容,來建立對應的字符串。 7 一種直接建立: 8 String str = "Hello"; // 右邊直接用雙引號 9 10 注意:直接寫上雙引號,就是字符串對象。 11 */ 12 public class Demo01String { 13 14 public static void main(String[] args) { 15 // 使用空參構造 16 String str1 = new String(); // 小括號留空,說明字符串什麼內容都沒有。 17 System.out.println("第1個字符串:" + str1); 18 19 // 根據字符數組建立字符串 20 char[] charArray = { 'A', 'B', 'C' }; 21 String str2 = new String(charArray); 22 System.out.println("第2個字符串:" + str2); 23 24 // 根據字節數組建立字符串 25 byte[] byteArray = { 97, 98, 99 }; 26 String str3 = new String(byteArray); 27 System.out.println("第3個字符串:" + str3); 28 29 // 直接建立 30 String str4 = "Hello"; 31 System.out.println("第4個字符串:" + str4); 32 } 33 34 }
1,判斷內容的方法
public boolean equals (Object anObject) :將此字符串與指定對象進行比較。
public boolean equalsIgnoreCase (String anotherString) :將此字符串與指定對象進行比較,忽略大小寫。
爲何引入這兩個方法不使用 == 進行判斷
對於基本類型來講,==是進行數值的比較。
對於引用類型來講,==是進行【地址值】的比較。
1 /* 2 ==是進行對象的地址值比較,若是確實須要字符串的內容比較,可使用兩個方法: 3 4 public boolean equals(Object obj):參數能夠是任何對象,只有參數是一個字符串而且內容相同的纔會給true;不然返回false。 5 注意事項: 6 1. 任何對象都能用Object進行接收。 7 2. equals方法具備對稱性,也就是a.equals(b)和b.equals(a)效果同樣。 8 3. 若是比較雙方一個常量一個變量,推薦把常量字符串寫在前面。 9 推薦:"abc".equals(str) 不推薦:str.equals("abc") 10 11 public boolean equalsIgnoreCase(String str):忽略大小寫,進行內容比較。 12 */ 13 public class Demo01StringEquals { 14 15 public static void main(String[] args) { 16 String str1 = "Hello"; 17 String str2 = "Hello"; 18 char[] charArray = {'H', 'e', 'l', 'l', 'o'}; 19 String str3 = new String(charArray); 20 21 System.out.println(str1.equals(str2)); // true 22 System.out.println(str2.equals(str3)); // true 23 System.out.println(str3.equals("Hello")); // true 24 System.out.println("Hello".equals(str1)); // true 25 26 String str4 = "hello"; 27 System.out.println(str1.equals(str4)); // false 28 System.out.println("================="); 29 30 String str5 = null; 31 System.out.println("abc".equals(str5)); // 推薦:false 32 // System.out.println(str5.equals("abc")); // 不推薦:報錯,空指針異常NullPointerException 33 System.out.println("================="); 34 35 String strA = "Java"; 36 String strB = "java"; 37 System.out.println(strA.equals(strB)); // false,嚴格區分大小寫 38 System.out.println(strA.equalsIgnoreCase(strB)); // true,忽略大小寫 39 40 // 注意,只有英文字母區分大小寫,其餘都不區分大小寫 41 System.out.println("abc一123".equalsIgnoreCase("abc壹123")); // false 42 } 43 }
2,獲取功能的方法
public int length():獲取字符串當中含有的字符個數,拿到字符串長度。
public String concat(String str):將當前字符串和參數字符串拼接成爲返回值新的字符串。
public char charAt(int index):獲取指定索引位置的單個字符。(索引從0開始。)
public int indexOf(String str):查找參數字符串在本字符串當中首次出現的索引位置,若是沒有返回-1值。
1 /* 2 String當中與獲取相關的經常使用方法有: 3 4 public int length():獲取字符串當中含有的字符個數,拿到字符串長度。 5 public String concat(String str):將當前字符串和參數字符串拼接成爲返回值新的字符串。 6 public char charAt(int index):獲取指定索引位置的單個字符。(索引從0開始。) 7 public int indexOf(String str):查找參數字符串在本字符串當中首次出現的索引位置,若是沒有返回-1值。 8 */ 9 public class Demo02StringGet { 10 11 public static void main(String[] args) { 12 // 獲取字符串的長度 13 int length = "asdasfeutrvauevbueyvb".length(); 14 System.out.println("字符串的長度是:" + length); 15 16 // 拼接字符串 17 String str1 = "Hello"; 18 String str2 = "World"; 19 String str3 = str1.concat(str2); 20 System.out.println(str1); // Hello,原封不動 21 System.out.println(str2); // World,原封不動 22 System.out.println(str3); // HelloWorld,新的字符串 23 System.out.println("=============="); 24 25 // 獲取指定索引位置的單個字符 26 char ch = "Hello".charAt(1); 27 System.out.println("在1號索引位置的字符是:" + ch); 28 System.out.println("=============="); 29 30 // 查找參數字符串在原本字符串當中出現的第一次索引位置 31 // 若是根本沒有,返回-1值 32 String original = "HelloWorldHelloWorld"; 33 int index = original.indexOf("llo"); 34 System.out.println("第一次索引值是:" + index); // 2 35 36 System.out.println("HelloWorld".indexOf("abc")); // -1 37 } 38 }
public String substring(int index):截取從參數位置一直到字符串末尾,返回新字符串。
public String substring(int begin, int end):截取從begin開始,一直到end結束,中間的字符串。備註:[begin,end),包含左邊,不包含右邊。
1 /* 2 字符串的截取方法: 3 public String substring(int index):截取從參數位置一直到字符串末尾,返回新字符串。 4 public String substring(int begin, int end):截取從begin開始,一直到end結束,中間的字符串。 5 備註:[begin,end),包含左邊,不包含右邊。 6 */ 7 public class Demo03Substring { 8 9 public static void main(String[] args) { 10 String str1 = "HelloWorld"; 11 String str2 = str1.substring(5); 12 System.out.println(str1); // HelloWorld,原封不動 13 System.out.println(str2); // World,新字符串 14 System.out.println("================"); 15 16 String str3 = str1.substring(4, 7); 17 System.out.println(str3); // oWo 18 System.out.println("================"); 19 20 // 下面這種寫法,字符串的內容仍然是沒有改變的 21 // 下面有兩個字符串:"Hello","Java" 22 // strA當中保存的是地址值。 23 // 原本地址值是Hello的0x666, 24 // 後來地址值變成了Java的0x999 25 String strA = "Hello"; 26 System.out.println(strA); // Hello 27 strA = "Java"; 28 System.out.println(strA); // Java 29 } 30 }
3,轉換功能的方法
public char[] toCharArray():將當前字符串拆分紅爲字符數組做爲返回值。
public byte[] getBytes():得到當前字符串底層的字節數組。
public String replace(CharSequence oldString, CharSequence newString):將全部出現的老字符串替換成爲新的字符串,返回替換以後的結果新字符串。
備註:CharSequence意思就是說能夠接受字符串類型。
1 /* 2 String當中與轉換相關的經常使用方法有: 3 4 public char[] toCharArray():將當前字符串拆分紅爲字符數組做爲返回值。 5 public byte[] getBytes():得到當前字符串底層的字節數組。 6 public String replace(CharSequence oldString, CharSequence newString): 7 將全部出現的老字符串替換成爲新的字符串,返回替換以後的結果新字符串。 8 備註:CharSequence意思就是說能夠接受字符串類型。 9 */ 10 public class Demo04StringConvert { 11 12 public static void main(String[] args) { 13 // 轉換成爲字符數組 14 char[] chars = "Hello".toCharArray(); 15 System.out.println(chars[0]); // H 16 System.out.println(chars.length); // 5 17 System.out.println("=============="); 18 19 // 轉換成爲字節數組 20 byte[] bytes = "abc".getBytes(); 21 for (int i = 0; i < bytes.length; i++) { 22 System.out.println(bytes[i]); 23 } 24 System.out.println("=============="); 25 26 // 字符串的內容替換 27 String str1 = "How do you do?"; 28 String str2 = str1.replace("o", "*"); 29 System.out.println(str1); // How do you do? 30 System.out.println(str2); // H*w d* y*u d*? 31 System.out.println("=============="); 32 33 String lang1 = "會不會玩兒呀!你大爺的!你大爺的!你大爺的!!!"; 34 String lang2 = lang1.replace("你大爺的", "****"); 35 System.out.println(lang2); // 會不會玩兒呀!****!****!****!!! 36 } 37 }
4,分隔功能的方法
public String[] split(String regex):按照參數的規則,將字符串切分紅爲若干部分。
注意事項:
split方法的參數實際上是一個「正則表達式」。
今天要注意:若是按照英文句點「.」進行切分,必須寫"\\."(兩個反斜槓)
1 /* 2 分割字符串的方法: 3 public String[] split(String regex):按照參數的規則,將字符串切分紅爲若干部分。 4 5 注意事項: 6 split方法的參數實際上是一個「正則表達式」,從此學習。 7 今天要注意:若是按照英文句點「.」進行切分,必須寫"\\."(兩個反斜槓) 8 */ 9 public class Demo05StringSplit { 10 11 public static void main(String[] args) { 12 String str1 = "aaa,bbb,ccc"; 13 String[] array1 = str1.split(","); 14 for (int i = 0; i < array1.length; i++) { 15 System.out.println(array1[i]); 16 } 17 System.out.println("==============="); 18 19 String str2 = "aaa bbb ccc"; 20 String[] array2 = str2.split(" "); 21 for (int i = 0; i < array2.length; i++) { 22 System.out.println(array2[i]); 23 } 24 System.out.println("==============="); 25 26 String str3 = "XXX.YYY.ZZZ"; 27 String[] array3 = str3.split("\\."); 28 System.out.println(array3.length); // 0 29 for (int i = 0; i < array3.length; i++) { 30 System.out.println(array3[i]); 31 } 32 } 33 }
方法的綜合使用示例
1 import java.util.Scanner; 2 /* 3 題目: 4 鍵盤輸入一個字符串,而且統計其中各類字符出現的次數。 5 種類有:大寫字母、小寫字母、數字、其餘 6 7 思路: 8 1. 既然用到鍵盤輸入,確定是Scanner 9 2. 鍵盤輸入的是字符串,那麼:String str = sc.next(); 10 3. 定義四個變量,分別表明四種字符各自的出現次數。 11 4. 須要對字符串一個字、一個字檢查,String-->char[],方法就是toCharArray() 12 5. 遍歷char[]字符數組,對當前字符的種類進行判斷,而且用四個變量進行++動做。 13 6. 打印輸出四個變量,分別表明四種字符出現次數。 14 */ 15 public class Demo07StringCount { 16 17 public static void main(String[] args) { 18 Scanner sc = new Scanner(System.in); 19 System.out.println("請輸入一個字符串:"); 20 String input = sc.next(); // 獲取鍵盤輸入的一個字符串 21 22 int countUpper = 0; // 大寫字母 23 int countLower = 0; // 小寫字母 24 int countNumber = 0; // 數字 25 int countOther = 0; // 其餘字符 26 27 char[] charArray = input.toCharArray(); 28 for (int i = 0; i < charArray.length; i++) { 29 char ch = charArray[i]; // 當前單個字符 30 if ('A' <= ch && ch <= 'Z') { 31 countUpper++; 32 } else if ('a' <= ch && ch <= 'z') { 33 countLower++; 34 } else if ('0' <= ch && ch <= '9') { 35 countNumber++; 36 } else { 37 countOther++; 38 } 39 } 40 41 System.out.println("大寫字母有:" + countUpper); 42 System.out.println("小寫字母有:" + countLower); 43 System.out.println("數字有:" + countNumber); 44 System.out.println("其餘字符有:" + countOther); 45 } 46 }
java.util.Arrays 此類包含用來操做數組的各類方法,好比排序和搜索等。其全部方法均爲靜態方法,調用起來很是簡單
方法:
1,public static String toString(數組):將參數數組變成字符串(按照默認格式:[元素1, 元素2, 元素3...])
2,public static void sort(數組):按照默認升序(從小到大)對數組的元素進行排序。
sort方法使用注意:
1. 若是是數值,sort默認按照升序從小到大
2. 若是是字符串,sort默認按照字母升序
3. 若是是自定義的類型,那麼這個自定義的類須要有Comparable或者Comparator接口的支持。
1 import java.util.Arrays; 2 3 public class Demo01Arrays { 4 5 public static void main(String[] args) { 6 int[] intArray = {10, 20, 30}; 7 // 將int[]數組按照默認格式變成字符串 8 String intStr = Arrays.toString(intArray); 9 System.out.println(intStr); // [10, 20, 30] 10 11 int[] array1 = {2, 1, 3, 10, 6}; 12 Arrays.sort(array1); 13 System.out.println(Arrays.toString(array1)); // [1, 2, 3, 6, 10] 14 15 String[] array2 = {"bbb", "aaa", "ccc"}; 16 Arrays.sort(array2); 17 System.out.println(Arrays.toString(array2)); // [aaa, bbb, ccc] 18 } 19 }
練習:請使用Arrays相關的API,將一個隨機字符串中的全部字符升序排列,並倒序打印。
1 import java.util.Arrays; 2 3 /* 4 題目: 5 請使用Arrays相關的API,將一個隨機字符串中的全部字符升序排列,並倒序打印。 6 */ 7 public class Demo02ArraysPractise { 8 9 public static void main(String[] args) { 10 String str = "asv76agfqwdfvasdfvjh"; 11 12 // 如何進行升序排列:sort 13 // 必須是一個數組,才能用Arrays.sort方法 14 // String --> 數組,用toCharArray 15 char[] chars = str.toCharArray(); 16 Arrays.sort(chars); // 對字符數組進行升序排列 17 18 // 須要倒序遍歷 19 for (int i = chars.length - 1; i >= 0; i--) { 20 System.out.println(chars[i]); 21 } 22 } 23 }
java.lang.Math 類包含用於執行基本數學運算的方法,如初等指數、對數、平方根和三角函數。相似這樣的工具 類,其全部方法均爲靜態方法,而且不會建立對象,調用起來很是簡單。
方法:
public static double abs(double num):獲取絕對值。有多種重載。
public static double ceil(double num):向上取整。
public static double floor(double num):向下取整。
public static long round(double num):四捨五入。
Math.PI表明近似的圓周率常量(double)。
1 public class Demo03Math { 2 3 public static void main(String[] args) { 4 // 獲取絕對值 5 System.out.println(Math.abs(3.14)); // 3.14 6 System.out.println(Math.abs(0)); // 0 7 System.out.println(Math.abs(-2.5)); // 2.5 8 System.out.println("================"); 9 10 // 向上取整 11 System.out.println(Math.ceil(3.9)); // 4.0 12 System.out.println(Math.ceil(3.1)); // 4.0 13 System.out.println(Math.ceil(3.0)); // 3.0 14 System.out.println("================"); 15 16 // 向下取整,抹零 17 System.out.println(Math.floor(30.1)); // 30.0 18 System.out.println(Math.floor(30.9)); // 30.0 19 System.out.println(Math.floor(31.0)); // 31.0 20 System.out.println("================"); 21 22 System.out.println(Math.round(20.4)); // 20 23 System.out.println(Math.round(10.5)); // 11 24 } 25 }
練習:計算在-10.8到5.9之間,絕對值大於6或者小於2.1的整數有多少個?
1 /* 2 題目: 3 計算在-10.8到5.9之間,絕對值大於6或者小於2.1的整數有多少個? 4 5 分析: 6 1. 既然已經肯定了範圍,for循環 7 2. 起點位置-10.8應該轉換成爲-10,兩種辦法: 8 2.1 可使用Math.ceil方法,向上(向正方向)取整 9 2.2 強轉成爲int,自動捨棄全部小數位 10 3. 每個數字都是整數,因此步進表達式應該是num++,這樣每次都是+1的。 11 4. 如何拿到絕對值:Math.abs方法。 12 5. 一旦發現了一個數字,須要讓計數器++進行統計。 13 14 備註:若是使用Math.ceil方法,-10.8能夠變成-10.0。注意double也是能夠進行++的。 15 */ 16 public class Demo04MathPractise { 17 18 public static void main(String[] args) { 19 int count = 0; // 符合要求的數量 20 21 double min = -10.8; 22 double max = 5.9; 23 // 這樣處理,變量i就是區間以內全部的整數 24 for (int i = (int) min; i < max; i++) { 25 int abs = Math.abs(i); // 絕對值 26 if (abs > 6 || abs < 2.1) { 27 System.out.println(i); 28 count++; 29 } 30 } 31 32 System.out.println("總共有:" + count); // 9 33 } 34 }
1 public class MyClass /*extends Object*/ { 2 // ... 3 }
方法:
public String toString():返回該對象的字符串表示。
public boolean equals(Object obj):指示其餘某個對象是否與此對象「相等」。
其他方法查API
toString方法返回該對象的字符串表示,其實該字符串內容就是對象的類型+@+內存地址值。
因爲toString方法返回的結果是內存地址,而在開發中,常常須要按照對象的屬性獲得相應的字符串表現形式,所以也須要重寫它。
在咱們直接使用輸出語句輸出對象名的時候,其實經過該對象調用了其toString()方法。
1 import java.util.Objects; 2 3 public class Person { 4 private String name; 5 private int age; 6 7 public Person() { 8 } 9 10 public Person(String name, int age) { 11 this.name = name; 12 this.age = age; 13 } 14 15 16 /* 17 直接打印對象的地址值沒有意義,須要重寫Object類中的toString方法 18 打印對象的屬性(name,age) 19 */ 20 /*@Override 21 public String toString() { 22 //return "abc"; 23 return "Person{name="+name+" ,age="+age+"}"; 24 }*/ 25 @Override 26 public String toString() { 27 return "Person{" + 28 "name='" + name + '\'' + 29 ", age=" + age + 30 '}'; 31 } 32 33 public String getName() { 34 return name; 35 } 36 37 public void setName(String name) { 38 this.name = name; 39 } 40 41 public int getAge() { 42 return age; 43 } 44 45 public void setAge(int age) { 46 this.age = age; 47 } 48 }
注意:重寫toSting方法return任何內容。可是通常返回成員變量。還能夠自動重寫toString方法。按art+insert選擇toString便可,自動重寫。
1 package a.demo01; 2 3 import java.util.ArrayList; 4 import java.util.Random; 5 import java.util.Scanner; 6 7 public class Demo01ToString{ 8 public static void main(String[] args) { 9 /* 10 Person類默認繼承了Object類,因此可使用Object類中的toString方法 11 String toString() 返回該對象的字符串表示。 12 */ 13 Person p = new Person("張三",18); 14 String s = p.toString(); 15 System.out.println(s);//com.itheima.demo01.Object.Person@75412c2f | abc | Person{name=張三 ,age=18} 16 17 //直接打印對象的名字,其實就是調用對象的toString p=p.toString(); 18 System.out.println(p);//com.itheima.demo01.Object.Person@5f150435 | abc | Person{name=張三 ,age=18} 19 20 //看一個類是否重寫了toString,直接打印這個類的對象便可,若是沒有重寫toString方法那麼打印的是對象的地址值 21 Random r = new Random(); 22 System.out.println(r);//java.util.Random@3f3afe78 沒有重寫toString方法 23 24 Scanner sc = new Scanner(System.in); 25 System.out.println(sc);//java.util.Scanner[delimiters=\p{javaWhitespace}+.. 重寫toString方法 26 27 ArrayList<Integer> list = new ArrayList<>(); 28 list.add(1); 29 list.add(2); 30 list.add(3); 31 System.out.println(list);//[1, 2, 3] 重寫toString方法 32 } 33 }
public boolean equals(Object obj)`:指示其餘某個對象是否與此對象「相等」。
調用成員方法equals並指定參數爲另外一個對象,則能夠判斷這兩個對象是不是相同的。這裏的「相同」有默認和自定義兩種方式。
若是但願進行對象的內容比較,即全部或指定的部分紅員變量相同就斷定兩個對象相同,則能夠覆蓋重寫equals方法。
1 package a.demo01; 2 3 import java.util.Objects; 4 5 public class Person { 6 private String name; 7 private int age; 8 9 public Person() { 10 } 11 12 public Person(String name, int age) { 13 this.name = name; 14 this.age = age; 15 } 16 17 /* 18 Object類的equals方法,默認比較的是兩個對象的地址值,沒有意義 19 因此咱們要重寫equals方法,比較兩個對象的屬性(name,age) 20 問題: 21 隱含着一個多態 22 多態的弊端:沒法使用子類特有的內容(屬性和方法) 23 Object obj = p2 = new Person("古力娜扎",19); 24 解決:可使用向下轉型(強轉)把obj類型轉換爲Person 25 */ 26 27 28 // 本身重寫的equals方法 29 /*@Override 30 public boolean equals(Object obj) { 31 //增長一個判斷,傳遞的參數obj若是是this自己,直接返回true,提升程序的效率 32 if(obj==this){ 33 return true; 34 } 35 36 //增長一個判斷,傳遞的參數obj若是是null,直接返回false,提升程序的效率 37 if(obj==null){ 38 return false; 39 } 40 41 //增長一個判斷,防止類型轉換一次ClassCastException 42 if(obj instanceof Person){ 43 //使用向下轉型,把obj轉換爲Person類型 44 // 由於有多態,這裏須要向下轉型 45 Person p = (Person)obj; 46 //比較兩個對象的屬性,一個對象是this(p1),一個對象是p(obj->p2) 47 boolean b = this.name.equals(p.name) && this.age==p.age; 48 return b; 49 } 50 //不是Person類型直接返回false 51 return false; 52 }*/ 53 54 // 使用編輯器自動插入的equals方法 55 @Override 56 public boolean equals(Object o) { 57 if (this == o) return true; 58 if (o == null || getClass() != o.getClass()) return false; 59 Person person = (Person) o; 60 return age == person.age && 61 Objects.equals(name, person.name); 62 } 63 64 @Override 65 public int hashCode() { 66 67 return Objects.hash(name, age); 68 } 69 70 public String getName() { 71 return name; 72 } 73 74 public void setName(String name) { 75 this.name = name; 76 } 77 78 public int getAge() { 79 return age; 80 } 81 82 public void setAge(int age) { 83 this.age = age; 84 } 85 } 86
在剛纔IDEA自動重寫equals代碼中,使用到了java.util.Objects
類,那麼這個類是什麼呢?
在JDK7
在比較兩個對象的時候,Object的equals方法容易拋出空指針異常,而Objects類中的equals方法就優化了這個問題。方法以下:
public static boolean equals(Object a, Object b)
:判斷兩個對象是否相等。
Objects類中equals方法源碼:
1 public static boolean equals(Object a, Object b) { 2 return (a == b) || (a != null && a.equals(b)); 3 }
java.util.Date
類 表示特定的瞬間,精確到毫秒。
繼續查閱Date類的描述,發現Date擁有多個構造函數,只是部分已通過時,可是其中有未過期的構造函數能夠把毫秒值轉成日期對象。
構造方法:
public Date(long date)
1 import java.util.Date; 2 3 public class Demo02Date { 4 public static void main(String[] args) { 5 demo01(); 6 demo02(); 7 } 8 9 /* 10 Date類的帶參數構造方法 11 Date(long date) :傳遞毫秒值,把毫秒值轉換爲Date日期 12 */ 13 private static void demo02() { 14 Date date = new Date(0L); 15 System.out.println(date);// Thu Jan 01 08:00:00 CST 1970 16 17 date = new Date(3742767540068L); 18 System.out.println(date);// Sun Aug 08 09:39:00 CST 2088 19 } 20 21 /* 22 Date類的空參數構造方法 23 Date() 獲取當前系統的日期和時間 24 */ 25 private static void demo01() { 26 Date date = new Date(); 27 System.out.println(date);//Sun Aug 08 12:23:03 CST 2088 28 } 29 }
注意:在使用println方法時,會自動調用Date類中的toString方法。Date類對Object類中的toString方法進行了覆蓋重寫,因此結果爲指定格式的字符串。
經常使用方法:
1 import java.util.Date; 2 3 public class Demo02Date { 4 public static void main(String[] args) { 5 demo01(); 6 7 8 // System.currentTimeMillis()方法 9 System.out.println(System.currentTimeMillis());//獲取當前系統時間到1970 年 1 月 1 日 00:00:00經歷了多少毫秒 10 } 11 12 /* 13 long getTime() 把日期轉換爲毫秒值(至關於System.currentTimeMillis()方法) 14 返回自 1970 年 1 月 1 日 00:00:00 GMT 以來此 Date 對象表示的毫秒數。 15 */ 16 private static void demo03() { 17 Date date = new Date(); 18 long time = date.getTime(); 19 System.out.println(time);//3742777636267 20 } 21 }
解析
因爲DateFormat爲抽象類,不能直接使用,因此須要經常使用的子類java.text.SimpleDateFormat
。這個類須要一個模式(格式)來指定格式化或解析的標準。
構造方法爲:
public SimpleDateFormat(String pattern)
:用給定的模式和默認語言環境的日期格式符號構造SimpleDateFormat。參數pattern是一個字符串,表明日期時間的自定義格式。
含義 | |
---|---|
y | 年 |
M | 月 |
d | 日 |
H | 時 |
m | 分 |
s |
成員方法:
String format(Date date) 按照指定的模式,把Date日期,格式化爲符合模式的字符串
1 package a.demo03; 2 3 import java.text.SimpleDateFormat; 4 import java.util.Date; 5 6 public class Demo01DateFormat { 7 public static void main(String[] args) throws ParseException { 8 demo02(); 9 } 10 11 /* 12 使用DateFormat類中的方法format,把日期格式化爲文本 13 使用步驟: 14 1.建立SimpleDateFormat對象,構造方法中傳遞指定的模式 15 2.調用SimpleDateFormat對象中的方法format,按照構造方法中指定的模式,把Date日期格式化爲符合模式的字符串(文本) 16 */ 17 private static void demo01() { 18 //1.建立SimpleDateFormat對象,構造方法中傳遞指定的模式 19 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒"); 20 //2.調用SimpleDateFormat對象中的方法format,按照構造方法中指定的模式,把Date日期格式化爲符合模式的字符串(文本) 21 //String format(Date date) 按照指定的模式,把Date日期,格式化爲符合模式的字符串 22 Date date = new Date(); 23 String d = sdf.format(date); 24 System.out.println(date);//Sun Aug 08 15:51:54 CST 2088 25 System.out.println(d);//2088年08月08日 15時51分54秒 26 } 27 }
Date parse(String source) 把符合模式的字符串,解析爲Date日期
1 import java.text.ParseException; 2 import java.text.SimpleDateFormat; 3 import java.util.Date; 4 5 public class Demo01DateFormat { 6 public static void main(String[] args) throws ParseException { 7 demo02(); 8 } 9 10 /* 11 使用DateFormat類中的方法parse,把文本解析爲日期 12 使用步驟: 13 1.建立SimpleDateFormat對象,構造方法中傳遞指定的模式 14 2.調用SimpleDateFormat對象中的方法parse,把符合構造方法中模式的字符串,解析爲Date日期 15 注意: 16 public Date parse(String source) throws ParseException 17 parse方法聲明瞭一個異常叫ParseException 18 若是字符串和構造方法的模式不同,那麼程序就會拋出此異常 19 調用一個拋出了異常的方法,就必須的處理這個異常,要麼throws繼續拋出這個異常,要麼try catch本身處理 20 */ 21 private static void demo02() throws ParseException { 22 //1.建立SimpleDateFormat對象,構造方法中傳遞指定的模式 23 SimpleDateFormat sdf = new SimpleDateFormat("yyyy年MM月dd日 HH時mm分ss秒"); 24 25 //2.調用SimpleDateFormat對象中的方法parse,把符合構造方法中模式的字符串,解析爲Date日期 26 //Date parse(String source) 把符合模式的字符串,解析爲Date日期 27 Date date = sdf.parse("2088年08月08日 15時51分54秒"); 28 29 System.out.println(date); 30 } 31 32 }
Calendar類沒法直接建立對象使用,裏邊有一個靜態方法叫getInstance(),該方法返回了Calendar類的子類對象
static Calendar getInstance() 使用默認時區和語言環境得到一個日曆。
1 public static void main(String[] args) { 2 Calendar c = Calendar.getInstance();//多態 3 System.out.println(c); 4 }
經常使用方法
public int get(int field):返回給定日曆字段的值。
1 import java.util.Calendar; 2 import java.util.Date; 3 4 public class Demo02Calendar { 5 public static void main(String[] args) { 6 demo04(); 7 } 8 9 /* 10 public int get(int field):返回給定日曆字段的值。 11 參數:傳遞指定的日曆字段(YEAR,MONTH...) 12 返回值:日曆字段表明的具體的值 13 */ 14 private static void demo01() { 15 //使用getInstance方法獲取Calendar對象 16 Calendar c = Calendar.getInstance(); 17 18 int year = c.get(Calendar.YEAR); 19 System.out.println(year); 20 21 int month = c.get(Calendar.MONTH); 22 System.out.println(month);//西方的月份0-11 東方:1-12 23 24 //int date = c.get(Calendar.DAY_OF_MONTH); 25 int date = c.get(Calendar.DATE); 26 System.out.println(date); 27 } 28 }
public void set(int field, int value):將給定的日曆字段設置爲給定值。
1 import java.util.Calendar; 2 import java.util.Date; 3 4 public class Demo02Calendar { 5 public static void main(String[] args) { 6 demo02(); 7 } 8 9 10 /* 11 public void set(int field, int value):將給定的日曆字段設置爲給定值。 12 參數: 13 int field:傳遞指定的日曆字段(YEAR,MONTH...) 14 int value:給指定字段設置的值 15 */ 16 private static void demo02() { 17 //使用getInstance方法獲取Calendar對象 18 Calendar c = Calendar.getInstance(); 19 20 //設置年爲9999 21 c.set(Calendar.YEAR,9999); 22 //設置月爲9月 23 c.set(Calendar.MONTH,9); 24 //設置日9日 25 c.set(Calendar.DATE,9); 26 27 //同時設置年月日,可使用set的重載方法 28 c.set(8888,8,8); 29 30 int year = c.get(Calendar.YEAR); 31 System.out.println(year);// 8888 32 33 int month = c.get(Calendar.MONTH); 34 System.out.println(month);//8 西方的月份0-11 東方:1-12 35 36 int date = c.get(Calendar.DATE); 37 System.out.println(date);// 8 38 } 39 }
public abstract void add(int field, int amount):根據日曆的規則,爲給定的日曆字段添加或減去指定的時間量。
1 import java.util.Calendar; 2 import java.util.Date; 3 4 public class Demo02Calendar { 5 public static void main(String[] args) { 6 demo03(); 7 } 8 9 10 /* 11 public abstract void add(int field, int amount):根據日曆的規則,爲給定的日曆字段添加或減去指定的時間量。 12 把指定的字段增長/減小指定的值 13 參數: 14 int field:傳遞指定的日曆字段(YEAR,MONTH...) 15 int amount:增長/減小指定的值 16 正數:增長 17 負數:減小 18 */ 19 private static void demo03() { 20 //使用getInstance方法獲取Calendar對象 21 Calendar c = Calendar.getInstance(); 22 23 //把年增長2年 24 c.add(Calendar.YEAR,2); 25 //把月份減小3個月 26 c.add(Calendar.MONTH,-3); 27 28 29 int year = c.get(Calendar.YEAR); 30 System.out.println(year);//當前年增長2年 31 32 int month = c.get(Calendar.MONTH); 33 System.out.println(month);//當前月減小三個月 西方的月份0-11 東方:1-12 34 35 //int date = c.get(Calendar.DAY_OF_MONTH); 36 int date = c.get(Calendar.DATE); 37 System.out.println(date);// 當前天 38 } 39 }
public Date getTime():返回一個表示此Calendar時間值(從曆元到如今的毫秒偏移量)的Date對象。
1 import java.util.Calendar; 2 import java.util.Date; 3 4 public class Demo02Calendar { 5 public static void main(String[] args) { 6 demo04(); 7 } 8 9 /* 10 public Date getTime():返回一個表示此Calendar時間值(從曆元到如今的毫秒偏移量)的Date對象。 11 把日曆對象,轉換爲日期對象 12 */ 13 private static void demo04() { 14 //使用getInstance方法獲取Calendar對象 15 Calendar c = Calendar.getInstance(); 16 17 Date date = c.getTime(); 18 System.out.println(date);// Thu Apr 23 16:33:47 CST 2020 19 } 20 21 }
Calendar類中提供不少成員常量,表明給定的日曆字段:
含義 | |
---|---|
YEAR | 年 |
MONTH | 月(從0開始,能夠+1使用) |
DAY_OF_MONTH/DATE | 月中的天(幾號) |
HOUR | 時(12小時制) |
HOUR_OF_DAY | 時(24小時制) |
MINUTE | 分 |
SECOND | 秒 |
DAY_OF_WEEK |
1 public static void main(String[] args) { 2 System.out.println(System.currentTimeMillis());//獲取當前系統時間到1970 年 1 月 1 日 00:00:00經歷了多少毫秒 3 }
public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
參數名稱 | 參數類型 | 參數含義 | |
---|---|---|---|
1 | src | Object | 源數組 |
2 | srcPos | int | 源數組索引發始位置 |
3 | dest | Object | 目標數組 |
4 | destPos | int | 目標數組索引發始位置 |
5 | length | int |
1 import java.util.Arrays; 2 3 public class Demo01System { 4 public static void main(String[] args) { 5 demo02(); 6 } 7 8 /* 9 練習: 10 將src數組中前3個元素,複製到dest數組的前3個位置上 11 複製元素前: 12 src數組元素[1,2,3,4,5],dest數組元素[6,7,8,9,10] 13 複製元素後: 14 src數組元素[1,2,3,4,5],dest數組元素[1,2,3,9,10] 15 */ 16 private static void demo02() { 17 //定義源數組 18 int[] src = {1,2,3,4,5}; 19 //定義目標數組 20 int[] dest = {6,7,8,9,10}; 21 System.out.println("複製前:"+ Arrays.toString(dest)); 22 23 //使用System類中的arraycopy把源數組的前3個元素複製到目標數組的前3個位置上 24 System.arraycopy(src,0,dest,0,3); 25 26 System.out.println("複製後:"+ Arrays.toString(dest)); 27 } 28 29 }
java.lang.StringBuilder類:字符串緩衝區,能夠提升字符串的效率 。
它的內部擁有一個數組用來存放字符串內容,進行字符串拼接時,直接在數組中加入新內容。StringBuilder會自動維護數組的擴容。(默認16字符空間,超過自動擴充)
構造方法
public StringBuilder(String str)
1 public static void main(String[] args) { 2 //空參數構造方法 3 StringBuilder bu1 = new StringBuilder(); 4 System.out.println("bu1:"+bu1);//bu1:"" 5 6 //帶字符串的構造方法 7 StringBuilder bu2 = new StringBuilder("abc"); 8 System.out.println("bu2:"+bu2);//bu2:abc 9 }
經常使用方法
1 public class Demo02StringBuilder { 2 public static void main(String[] args) { 3 //建立StringBuilder對象 4 StringBuilder bu = new StringBuilder(); 5 //使用append方法往StringBuilder中添加數據 6 //append方法返回的是this,調用方法的對象bu,this==bu 7 8 StringBuilder bu2 = bu.append("abc");//把bu的地址賦值給了bu2 9 System.out.println(bu);//"abc" 10 System.out.println(bu2);//"abc" 11 System.out.println(bu==bu2);//比較的是地址 true 12 13 //使用append方法無需接收返回值 14 bu.append("abc"); 15 bu.append(1); 16 bu.append(true); 17 bu.append(8.8); 18 bu.append('中'); 19 System.out.println(bu);//abc1true8.8中 20 21 //鏈式編程:方法返回值是一個對象,能夠繼續調用方法 22 System.out.println("abc".toUpperCase().toLowerCase().toUpperCase().toLowerCase()); 23 bu.append("abc").append(1).append(true).append(8.8).append('中'); 24 System.out.println(bu);//abc1true8.8中 25 26 } 27 }
public String toString()
使用StringBuilder(String str) 構造函數,構造一個字符串生成器,並初始化爲指定的字符串內容。能夠將String轉換爲StringBuilder
public String toString():將當前StringBuilder對象轉換爲String對象。
1 /* 2 StringBuilder和String能夠相互轉換: 3 String->StringBuilder:可使用StringBuilder的構造方法 4 StringBuilder(String str) 構造一個字符串生成器,並初始化爲指定的字符串內容。 5 StringBuilder->String:可使用StringBuilder中的toString方法 6 public String toString():將當前StringBuilder對象轉換爲String對象。 7 */ 8 public class Demo03StringBuilder { 9 public static void main(String[] args) { 10 11 //String->StringBuilder 12 String str = "hello"; 13 System.out.println("str:"+str);//str:hello 14 StringBuilder bu = new StringBuilder(str); 15 16 //往StringBuilder中添加數據 17 bu.append("world"); 18 System.out.println("bu:"+bu);// bu:helloworld 19 20 //StringBuilder->String 21 String s = bu.toString(); 22 System.out.println("s:"+s);// str:helloworld 23 } 24 }
---------------------