day04-常用API,正規表達式,Collection集合
今日目标
- BigInteger類
- BigDecimal類
- Arrays類
- 包裝類
- String類的常用方法
- 正規表達式
- Collection集合
1 BigInteger類
1.1 概述
- 概述 : java.math.BigInteger類是一個引用資料類型 , 可以用于計算一些大的整數 , 當超出基本資料類型資料範圍的整數運算時就可以使用BigInteger了。
1.2 構造方法
- 構造方法 : 可以将整數的字元串 . 轉成BigInteger類型的對象
1.3 成員方法
- 方法聲明描述public BigInteger add (BigInteger value)超大整數加法運算public BigInteger subtract (BigInteger value)超大整數減法運算public BigInteger multiply (BigInteger value)超大整數乘法運算public BigInteger divide (BigInteger value)超大整數除法運算,除不盡取整數部分
package com.itheima.api_demo.biginteger_demo;
import java.math.BigInteger;
/*
構造方法 :
BigInteger(String value) 可以将整數的字元串,轉換為BigInteger對象
成員方法 :
public BigInteger add (BigInteger value) 超大整數加法運算
public BigInteger subtract (BigInteger value) 超大整數減法運算
public BigInteger multiply (BigInteger value) 超大整數乘法運算
public BigInteger divide (BigInteger value) 超大整數除法運算,除不盡取整數部分
*/
public class BigIntegerDemo {
public static void main(String[] args) {
// 擷取大整數對象
BigInteger bigInteger1 = new BigInteger("2147483648");
// 擷取大整數對象
BigInteger bigInteger2 = new BigInteger("2");
// public BigInteger add (BigInteger value) 超大整數加法運算
BigInteger add = bigInteger1.add(bigInteger2);
System.out.println(add);
System.out.println("=============");
// public BigInteger subtract (BigInteger value) 超大整數減法運算
BigInteger subtract = bigInteger1.subtract(bigInteger2);
System.out.println(subtract);
System.out.println("=============");
// public BigInteger multiply (BigInteger value) 超大整數乘法運算
BigInteger multiply = bigInteger1.multiply(bigInteger2);
System.out.println(multiply);
System.out.println("=============");
// public BigInteger divide (BigInteger value) 超大整數除法運算,除不盡取整數部分
BigInteger divide = bigInteger1.divide(bigInteger2);
System.out.println(divide);
}
}
2 BigDecimal類
2.1 概述
- 概述 : java.math.BigDecimal可以對大浮點數進行運算,保證運算的準确性。float,double 他們在存儲及運算的時候,會導緻資料精度的丢失。如果要保證運算的準确性,就需要使用BigDecimal。
2.2 構造方法
- 構造方法 :
- public BigDecimal(String val) : 将 BigDecimal 的字元串表示形式轉換為 BigDecimal
2.3 成員方法
- 成員方法 :
- 方法聲明描述public BigDecimal add(BigDecimal value)加法運算public BigDecimal subtract(BigDecimal value)減法運算public BigDecimal multiply(BigDecimal value)乘法運算public BigDecimal divide(BigDecimal value)除法運算(除不盡會有異常)public BigDecimal divide(BigDecimal divisor, int roundingMode)除法運算(除不盡,使用該方法)參數說明:scale 精确位數,roundingMode取舍模式 BigDecimal.ROUND_HALF_UP 四舍五入 BigDecimal.ROUND_FLOOR 去尾法 BigDecimal.ROUND_UP 進一法
package com.itheima.api_demo.bigdecimal_demo;
import java.math.BigDecimal;
/*
構造方法 :
public BigDecimal(String val) 将 BigDecimal 的字元串表示形式轉換為 BigDecimal
成員方法 :
public BigDecimal add(BigDecimal value) 加法運算
public BigDecimal subtract(BigDecimal value) 減法運算
public BigDecimal multiply(BigDecimal value) 乘法運算
public BigDecimal divide(BigDecimal value) 除法運算(除不盡會有異常)
public BigDecimal divide(BigDecimal value, int scale, RoundingMode roundingMode) 除法運算(除不盡,使用該方法)
參數說明:
scale 精确位數,
roundingMode取舍模式
BigDecimal.ROUND_HALF_UP 四舍五入
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_UP 進一法
*/
public class BigDecimalDemo {
public static void main(String[] args) {
BigDecimal bigDecimal1 = new BigDecimal("0.1");
BigDecimal bigDecimal2 = new BigDecimal("0.2");
// public BigDecimal add(BigDecimal value) 加法運算
BigDecimal add = bigDecimal1.add(bigDecimal2);
System.out.println(add);
System.out.println("=================");
// public BigDecimal subtract(BigDecimal value) 減法運算
BigDecimal subtract = bigDecimal1.subtract(bigDecimal2);
System.out.println(subtract);
System.out.println("=================");
// public BigDecimal multiply(BigDecimal value) 乘法運算
BigDecimal multiply = bigDecimal1.multiply(bigDecimal2);
System.out.println(multiply);
System.out.println("=================");
// public BigDecimal divide(BigDecimal value) 除法運算(除不盡會有異常)
// BigDecimal divide = bigDecimal1.divide(bigDecimal2);
// System.out.println(divide);
/*
public BigDecimal divide(BigDecimal divisor, int roundingMode) 除法運算(除不盡,使用該方法)
參數說明:
scale 精确位數,
roundingMode : 取舍模式
BigDecimal.ROUND_HALF_UP 四舍五入
BigDecimal.ROUND_FLOOR 去尾法
BigDecimal.ROUND_UP 進一法
*/
// BigDecimal divide = bigDecimal1.divide(bigDecimal2, 3, BigDecimal.ROUND_HALF_UP);
// BigDecimal divide = bigDecimal1.divide(bigDecimal2, 3, BigDecimal.ROUND_FLOOR);
// BigDecimal divide = bigDecimal1.divide(bigDecimal2, 3, BigDecimal.ROUND_UP);
// System.out.println(divide);
}
}
3 Arrays類
3.1 概述
- 概述 : java.util.Arrays是數組的工具類,裡面有很多靜态的方法用來對數組進行操作(如排序和搜尋),還包含一個靜态工廠,可以将數組轉換為List集合(後面會講到集合知識
3.2 構造方法
- 構造方法 : private Arrays(){}
- public static void sort(int[] a)按照數字順序排列指定的數組public static String toString(int[] a)傳回指定數組的内容的字元串表示形式
package com.itheima.api_demo.arrays_demo;
import java.util.Arrays;
import java.util.Random;
/*
1 随機生成10個[0,100]之間的整數,放到整數數組中,按照從小到大的順序排序并列印元素内容。
*/
public class ArraysDemo {
public static void main(String[] args) {
// 建立數組
int[] arr = new int[10];
// 建立随機數對象
Random r = new Random();
// 采用随機數給數組的每一個元素指派
for (int i = 0; i < arr.length; i++) {
arr[i] = r.nextInt(101);
}
// 對數組進行排序
Arrays.sort(arr);
// 把數組轉成指定格式的字元串
System.out.println(Arrays.toString(arr));
}
}
4 包裝類
4.1 概述
- 概述 :
- Java中基本資料類型對應的引用資料類型
4.2 包裝類的作用
- 包裝類的作用 :
- 基本資料類型 , 沒有變量 , 沒有方法 , 包裝類就是讓基本資料類型擁有變量和屬性 , 實作對象化互動
- 基本資料類型和字元串之間的轉換
4.3 基本資料類型和包裝類對應
- 基本資料類型和包裝類的對應關系基本資料類型包裝類型byteByteshortShortintIntegerlongLongfloatFloatdoubleDoublecharCharacterbooleanBoolean
4.4 自動裝箱和自動拆箱
- 自動轉型和自動拆箱
- 自動裝箱和拆箱是JDK1.5開始的
- 自動裝箱 : 基本資料類型自動轉成對應的包裝類類型
- 自動拆箱 : 包裝類類型自動轉成對應的基本資料類型
-
Integer i1 = 10;
int i2 = i1;
4.5 基本資料類型和字元串之間的轉換
- 使用包裝類, 對基本資料類型和字元串之間的轉換
- 在開發過程中資料在不同平台之間傳輸時都以字元串形式存在的,有些資料表示的是數值含義,如果要用于計算我們就需要将其轉換基本資料類型.
- 基本資料類型--> String
- 直接在數值後加一個空字元串
- 通過String類靜态方法valueOf()
- String --> 基本資料類型
- public static byte parseByte(String s):将字元串參數轉換為對應的byte基本類型。public static short parseShort(String s):将字元串參數轉換為對應的short基本類型。public static int parseInt(String s):将字元串參數轉換為對應的int基本類型。public static long parseLong(String s):将字元串參數轉換為對應的long基本類型。public static float parseFloat(String s):将字元串參數轉換為對應的float基本類型。public static double parseDouble(String s):将字元串參數轉換為對應的double基本類型。public static boolean parseBoolean(String s):将字元串參數轉換為對應的boolean基本類型。
- 注意事項 :
- 包裝類對象的初始值為null(是一個對象)
- Java中除了float和double的其他基本資料類型,都有常量池
- 整數類型:[-128,127]值在常量池
- 字元類型:[0,127]對應的字元在常量池
- 布爾類型:true,false在常量池
- 在常量池中的資料 , 會進行共享使用 , 不在常量池中的資料會建立一個新的對象
5 String類的常用方法
5.1 常用方法
package com.itheima.api_demo.string_demo;
/*
已知字元串,完成需求
String str = "I Love Java, I Love Heima";
判斷是否存在 “Java”
判斷是否以Heima字元串結尾
判斷是否以Java開頭
判斷 Java在字元串中的第一次出現位置
判斷 itcast 所在的位置
*/
public class Test {
public static void main(String[] args) {
String str = "I Love Java, I Love Heima";
// 判斷是否存在 “Java”
System.out.println(str.contains("Java"));// true
// 判斷是否以Heima字元串結尾
System.out.println(str.endsWith("Heima"));// true
// 判斷是否以Java開頭
System.out.println(str.startsWith("Java"));// false
// 判斷 Java在字元串中的第一次出現位置
System.out.println(str.indexOf("Java"));// 7
// 判斷 itcast 所在的位置
System.out.println(str.indexOf("itcast"));// -1
}
}
package com.itheima.api_demo.string_demo;
/*
已知字元串,完成右側需求
String str = "I Love Java, I Love Heima";
需求:
1.将所有 Love 替換為 Like ,列印替換後的新字元串
2.截取字元串 "I Love Heima"
3.截取字元串 "Java"
*/
public class Test2 {
public static void main(String[] args) {
String str = "I Love Java, I Love Heima";
// 1.将所有 Love 替換為 Like ,列印替換後的新字元串
System.out.println(str.replace("Love", "Like"));
// 2.截取字元串 "I Love Heima"
System.out.println(str.substring(13));
// 3.截取字元串 "Java"
System.out.println(str.substring(7 , 11));
}
}
package com.itheima.api_demo.string_demo;
/*
已知字元串,完成右側需求
String str = "I Love Java, I Love Heima";
需求:
1 計算字元 a 出現的次數(要求使用toCharArray)
2 計算字元 a 出現的次數(要求使用charAt)
3 将字元串中所有英文字母變成小寫
4 将字元串中所有英文字母變成大寫
*/
public class Test3 {
public static void main(String[] args) {
String str = "I Love Java, I Love Heima";
// 1 計算字元 a 出現的次數(要求使用toCharArray)
int count1 = 0;
char[] chars = str.toCharArray();
for (int i = 0; i < chars.length; i++) {
if (chars[i] == 'a') {
count1++;
}
}
System.out.println("字元a出現了" + count1 + "次");
// 2 計算字元 a 出現的次數(要求使用charAt)
int count2 = 0;
for (int i = 0; i < str.length(); i++) {
char ch = str.charAt(i);
if(ch == 'a'){
count2++;
}
}
System.out.println("字元a出現了" + count2 + "次");
// 3 将字元串中所有英文字母變成小寫
String s1 = str.toLowerCase();
System.out.println(s1);
// 4 将字元串中所有英文字母變成大寫
String s2 = str.toUpperCase();
System.out.println(s2);
}
}
6 正規表達式
6.1 概述 :
- 正規表達式通常用來校驗,檢查字元串是否符合規則的
6.2 體驗正規表達式
package com.itheima.regex_demo;
import java.util.Scanner;
/*
設計程式讓使用者輸入一個QQ号碼,驗證QQ号的合法性:
1. QQ号碼必須是5--15位長度
2. 而且首位不能為0
3. 而且必須全部是數字
*/
public class Test1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入您的qq号碼:");
String qq = sc.nextLine();
System.out.println(checkQQ2(qq));
}
private static boolean checkQQ(String qq) {
// 1. QQ号碼必須是5--15位長度
if (qq.length() < 5 || qq.length() > 15) {
return false;
}
// 2 . 而且首位不能為0
if (qq.charAt(0) == '0') {
return false;
}
// 2. 而且必須全部是數字
for (int i = 0; i < qq.length(); i++) {
char ch = qq.charAt(i);
if (ch < '0' || ch > '9') {
return false;
}
}
return true;
}
// 正規表達式改進
private static boolean checkQQ2(String qq) {
return qq.matches("[1-9][0-9]{4,14}");
}
}
6.3 正規表達式的文法
- boolean matches(正規表達式) :如果比對正規表達式就傳回true,否則傳回false
- boolean matches(正規表達式) :如果比對正規表達式就傳回true,否則傳回false
- 字元類
- [abc] :代表a或者b,或者c字元中的一個。
- [^abc]:代表除a,b,c以外的任何字元。
- [a-z] :代表a-z的所有小寫字元中的一個。
- [A-Z] :代表A-Z的所有大寫字元中的一個。
- [0-9] :代表0-9之間的某一個數字字元。
- [a-zA-Z0-9]:代表a-z或者A-Z或者0-9之間的任意一個字元。
- [a-dm-p]:a 到 d 或 m 到 p之間的任意一個字元
package com.itheima.regex_demo;
/*
字元類 : 方括号被用于指定字元
[abc] :代表a或者b,或者c字元中的一個。
[^abc]:代表除a,b,c以外的任何字元。
[a-z] :代表a-z的所有小寫字元中的一個。
[A-Z] :代表A-Z的所有大寫字元中的一個。
[0-9] :代表0-9之間的某一個數字字元。
[a-zA-Z0-9]:代表a-z或者A-Z或者0-9之間的任意一個字元。
[a-dm-p]:a 到 d 或 m 到 p之間的任意一個字元
需求 :
1 驗證str是否以h開頭,以d結尾,中間是a,e,i,o,u中某個字元
2 驗證str是否以h開頭,以d結尾,中間不是a,e,i,o,u中的某個字元
3 驗證str是否a-z的任何一個小寫字元開頭,後跟ad
4 驗證str是否以a-d或者m-p之間某個字元開頭,後跟ad
注意: boolean matches(正規表達式) :如果比對正規表達式就傳回true,否則傳回false
*/
public class RegexDemo {
public static void main(String[] args) {
// 1 驗證str是否以h開頭,以d結尾,中間是a,e,i,o,u中某個字元
System.out.println("had".matches("h[aeiou]d"));
// 2 驗證str是否以h開頭,以d結尾,中間不是a,e,i,o,u中的某個字元
System.out.println("hwd".matches("h[^aeiou]d"));
// 3 驗證str是否a-z的任何一個小寫字元開頭,後跟ad
System.out.println("aad".matches("[a-z]ad"));
// 4 驗證str是否以a-d或者m-p之間某個字元開頭,後跟ad
System.out.println("bad".matches("[a-dm-p]ad"));
}
}
- 邏輯運算符
- && :并且
- | :或者
package com.itheima.regex_demo;
/*
邏輯運算符 :
1 && : 并且
2 | : 或者
需求 :
1 要求字元串是除a、e、i、o、u外的其它小寫字元開頭,後跟ad
2 要求字元串是aeiou中的某個字元開頭,後跟ad
*/
public class RegexDemo2 {
public static void main(String[] args) {
// 1 要求字元串是除a、e、i、o、u外的其它小寫字元開頭,後跟ad
System.out.println("vad".matches("[a-z&&[^aeiou]]ad"));
// 2 要求字元串是aeiou中的某個字元開頭,後跟ad
System.out.println("aad".matches("[a|e|i|o|u]ad"));
}
}
- 預定義字元類
- "." : 比對任何字元。
- "\d":任何數字[0-9]的簡寫;
- "\D":任何非數字0-9的簡寫;
- "\s" : 空白字元:[ \t\n\x0B\f\r] 的簡寫
- "\S" : 非空白字元:\s 的簡寫
- "\w" :單詞字元:[a-zA-Z_0-9]的簡寫
- "\W":非單詞字元:\w
package com.itheima.regex_demo;
/*
預定義字元 : 台灣字元類的書寫
"." :比對任何字元。
"\d" :任何數字[0-9]的簡寫
"\D" :任何非數字[^0-9]的簡寫
"\s" :空白字元:[\t\n\x0B\f\r] 的簡寫
"\S" :非空白字元:[^\s] 的簡寫
"\w" :單詞字元:[a-zA-Z_0-9]的簡寫
"\W" :非單詞字元:[^\w]
需求 :
1 驗證str是否3位數字
2 驗證手機号:1開頭,第二位:3/5/8,剩下9位都是0-9的數字
3 驗證字元串是否以h開頭,以d結尾,中間是任何字元
*/
public class RegexDemo3 {
public static void main(String[] args) {
// 1 驗證str是否3位數字
System.out.println("123".matches("\\d\\d\\d"));
// 2 驗證手機号:1開頭,第二位:3/5/8,剩下9位都是0-9的數字 )
System.out.println("15188888888".matches("1[358]\\d\\d\\d\\d\\d\\d\\d\\d\\d"));
// 3 驗證字元串是否以h開頭,以d結尾,中間是任何字元
System.out.println("had".matches("h.d"));
}
}
- 數量詞
- X? : 0次或1次
- X* : 0次到多次
- X+ : 1次或多次
- X{n} : 恰好n次
- X{n,} : 至少n次
- X{n,m}: n到m次(n和m都是包含的)
package com.itheima.regex_demo;
/*
數量詞 :
- X? : 0次或1次
- X* : 0次到多次
- X+ : 1次或多次
- X{n} : 恰好n次
- X{n,} : 至少n次
- X{n,m}: n到m次(n和m都是包含的)
需求 :
1 驗證str是否3位數字
2 驗證str是否是多位(大于等于1次)數字
3 驗證str是否是手機号 ( 1開頭,第二位:3/5/8,剩下9位都是0-9的數字)
4 驗證qq号碼:1).5--15位;2).全部是數字;3).第一位不是0
*/
public class RegexDemo4 {
public static void main(String[] args) {
// 1 驗證str是否3位數字
System.out.println("123".matches("\\d{3}"));
// 2 驗證str是否是多位(大于等于1次)數字
System.out.println("123456".matches("\\d+"));
// 3 驗證str是否是手機号 ( 1開頭,第二位:3/5/8,剩下9位都是0-9的數字)
System.out.println("15188888888".matches("1[358]\\d{9}"));
// 4 驗證qq号碼:1).5--15位;2).全部是數字;3).第一位不是0
System.out.println("122103987".matches("[1-9]\\d{4,14}"));
}
}
- 分組括号 :
- 将要重複使用的正則用小括号括起來,當做一個小組看待
package com.itheima.regex_demo;
/*
分組括号 : 将要重複使用的正則用小括号括起來,當做一個小組看待
需求 : window秘鑰 , 分為5組,每組之間使用 - 隔開 , 每組由5位A-Z或者0-9的字元組成 , 最後一組沒有 -
舉例 :
xxxxx-xxxxx-xxxxx-xxxxx-xxxxx
DG8FV-B9TKY-FRT9J-99899-XPQ4G
分析:
前四組其一 :DG8FV- 正則:[A-Z0-9]{5}-
最後一組 :XPQ4G 正則:[A-Z0-9]{5}
結果 : ([A-Z0-9]{5}-){4}[A-Z0-9]{5}
*/
public class RegexDemo5 {
public static void main(String[] args) {
System.out.println("DG8FV-B9TKY-FRT9J-99899-XPQ4G".matches("([A-Z0-9]{5}-){4}[A-Z0-9]{5}"));
}
}
- 字元串中常用含有正規表達式的方法
- public String[] split ( String regex ) 可以将目前字元串中比對regex正規表達式的符号作為"分隔符"來切割字元串。
- public String replaceAll ( String regex , String newStr ) 可以将目前字元串中比對regex正規表達式的字元串替換為newStr。
package com.itheima.regex_demo;
import java.util.Arrays;
/*
1 字元串中常用含有正規表達式的方法
public String[] split ( String regex ) 可以将目前字元串中比對regex正規表達式的符号作為"分隔符"來切割字元串。
public String replaceAll ( String regex , String newStr ) 可以将目前字元串中比對regex正規表達式的字元串替換為newStr。
需求:
1 将以下字元串按照數字進行切割
String str1 = "aa123bb234cc909dd";
2 将下面字元串中的"數字"替換為"*“a
String str2 = "我卡裡有100000元,我告訴你卡的密碼是123456,要保密哦";
*/
public class RegexDemo6 {
public static void main(String[] args) {
// 1 将以下字元串按照數字進行切割
String str1 = "aa123bb234cc909dd";
String[] strs = str1.split("\\d+");
System.out.println(Arrays.toString(strs));
// 2 将下面字元串中的"數字"替換為"*“a
String str2 = "我卡裡有100000元,我告訴你卡的密碼是123456,要保密哦";
System.out.println(str2.replaceAll("\\d+" , "*"));
}
}
7 引用資料類型使用
7.1 使用方式
- 基本資料類型可以當做方法的參數,傳回值及成員變量使用,傳遞或者儲存的是資料值。
- 引用資料類型也可以當做方法的參數,傳回值及字段使用,傳遞或者儲存的是對象的引用(位址)。
- 特别要注意,如果是抽象類或者接口那麼傳遞或者儲存的就是子類對象的位址引用了
package com.itheima.datatype_demo;
public interface Player {
public abstract void play();
}
abstract class Mp3Player implements Player {
}
class IPod extends Mp3Player {
@Override
public void play() {
System.out.println("IPod播放音樂...");
}
}
package com.itheima.datatype_demo;
/*
1 分别定義含有Play,Mp3Player , Ipod參數的方法,并調用傳入實參進行測試
2 定義一個學生類,裡面定義含有Player, Mp3Player,Ipod類型的成員變量
建立學生對象并給成員變量指派
*/
public class PlayerTest {
public static void main(String[] args) {
show1(new IPod());
show2(new IPod());
show2(new IPod());
Student s = new Student();
s.setPlayer(new IPod());
s.setMp3Player(new IPod());
s.setiPod(new IPod());
}
public static void show1(Player player) {// 方法的參數是一個接口 , 接收實作類對象
player.play();
}
public static void show2(Mp3Player player) {// 方法的參數是一個抽象類 , 接收子類對象
player.play();
}
public static void show3(Mp3Player player) {// 方法的參數是具體的類 , 接收此類對象或者子類對象
player.play();
}
}
/*
定義一個學生類,裡面定義含有Player, Mp3Player,Ipod類型的成員變量
建立學生對象并給成員變量指派
*/
class Student {
private Player player; // 接口的類型的成員變量 , 儲存的是實作類對象位址
private Mp3Player mp3Player; // 實作類的類型的成員變量 , 儲存的是子類對象位址
private IPod iPod; // 具體類類型的成員變量 , 儲存的是此類對象或者此類的子類對象位址
public Player getPlayer() {
return player;
}
public void setPlayer(Player player) {
this.player = player;
}
public Mp3Player getMp3Player() {
return mp3Player;
}
public void setMp3Player(Mp3Player mp3Player) {
this.mp3Player = mp3Player;
}
public IPod getiPod() {
return iPod;
}
public void setiPod(IPod iPod) {
this.iPod = iPod;
}
}
8 Collection集合
8.1 集合和數組的差別
- 長度差別
- 數組 : 長度固定
- 集合 : 長度可變
- 存儲資料類型
- 數組 : 可以存儲基本資料類型, 也可以存儲引用資料類型
- 集合 : 隻能存儲引用資料類型 , 要想存儲基本資料類型 , 需要存儲對應的包裝類類型
8.2 集合的體系
8.3 Collection集合常用的方法
- 在學習集合體系,一般先學習頂層接口。學習了頂層接口的方法,子類型繼承而來的方法,就可以不用重複學習。
- public boolean add(E e): 把給定的對象添加到目前集合中 。
- public void clear() :清空集合中所有的元素。
- public boolean remove(E e): 把給定的對象在目前集合中删除。
- public boolean contains(Object obj): 判斷目前集合中是否包含給定的對象。
- public boolean isEmpty(): 判斷目前集合是否為空。
- public int size(): 傳回集合中元素的個數。
- public Object[] toArray(): 把集合中的元素,存儲到數組中
8.4 疊代器
- 概述 :
- 疊代器就是對Iterator的稱呼 , 專門用來對Collection集合進行周遊使用的。學習疊代器的目的就是為了周遊集合
- 注意 :
- 隻有繼承/實作Iterable接口才具有疊代的能力
- Collection接口繼承了Iterable接口是以 , Collection及其子類都具備疊代的能力
- 擷取疊代器對象的方式
- 通過Collection集合對象調用Iterable接口中的iterator方法 , 就可以擷取疊代器對象
- Iterator(疊代器)中的方法booleanhasNext()如果疊代具有更多元素,則傳回 true 。Enext()傳回疊代中的下一個元素。default voidremove()從底層集合中删除此疊代器傳回的最後一個元素(可選操作)。
- 疊代器的注意事項
- 當疊代器疊代元素完成後,不能繼續next擷取元素,否則會報:NoSuchElementException
- 當疊代器在使用的過程中,不能使用集合對象直接增删元素。會導緻報錯ConcurrentModificationException。如果要删除可以使用疊代器來删除
8.5 增強for循環
- 增強for循環(foreach),專門用來周遊集合或者數組,底層實作使用疊代器
- 定義格式 :
-
for(元素的類型 變量名 : 數組/單列集合 ){
變量代表的就是集合或者數組的元素
}