目錄
列:生成6個1~33之間的隨機整數,添加到集合,並遍歷集合。
列:定義以指定格式打印集合的方法(ArrayList類型作爲參數),使用{}擴起集合,使用@分隔每個元素。格式參照 {元素@元素@元素}。
Scanner類
什麼是Scanner類
一個可以解析基本類型和字符串的簡單文本掃描器(鍵盤輸入數據)
Scanner實現步驟
導包
import 包名.類名;
創建對象
數據類型 變量名 = new 數據類型(參數列表);
列:
Scanner sc = new Scanner(System.in);
調用方法
變量名.方法名();
int i = sc.nextInt(); // 接收一個鍵盤錄入的整數
列:從鍵盤輸入整數和字符串
public static void main(String[] args) {
// 2. 創建
// 備註:System.in代表從鍵盤進行輸入
Scanner sc = new Scanner(System.in);
// 3. 獲取鍵盤輸入的int數字
int num = sc.nextInt();
System.out.println("輸入的int數字是:" + num);
// 4. 獲取鍵盤輸入的字符串
String str = sc.next();
System.out.println("輸入的字符串是:" + str);
}
列:從鍵盤輸入三個數字比較大小
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("請輸入第一個數字:");
int a = sc.nextInt();
System.out.println("請輸入第二個數字:");
int b = sc.nextInt();
System.out.println("請輸入第三個數字:");
int c = sc.nextInt();
// 首先得到前兩個數字當中的最大值
int temp = a > b ? a : b;
int max = temp > c ? temp : c;
System.out.println("最大值是:" + max);
}
列:使用多種方式實現從鍵盤輸入數字
方法1
public static void main(String[] args) {
// 普通使用方式
Scanner sc = new Scanner(System.in);
int num = sc.nextInt();
}
匿名對象的方式
int num = new Scanner(System.in).nextInt();
System.out.println("輸入的是:" + num);
使用一般寫法傳入參數
public static void main(String[] args) {
methodParam(new Scanner(System.in));
}
public static void methodParam(Scanner sc) {
int num = sc.nextInt();
System.out.println("輸入的是:" + num);
}
使用匿名對象來進行傳參
public static void main(String[] args) {
Scanner sc = methodReturn();
int num = sc.nextInt();
System.out.println("輸入的是:" + num);
}
public static Scanner methodReturn() {
return new Scanner(System.in);
}
匿名對象
創建對象時,只有創建對象的語句,卻沒有把對象地址值賦值給某個變量。雖然是創建對象的簡化寫法,但是應用
注意事項:匿名對象只能使用唯一的一次,下次再用不得不再創建一個新對象。
列:匿名對象練習
Person
public class Person {
String name;
public void showName() {
System.out.println("我叫:" + name);
}
}
Demo01Anonymous
public static void main(String[] args) {
// 左邊的one就是對象的名字
Person one = new Person();
one.name = "高圓圓";
one.showName(); // 我叫高圓圓
System.out.println("===============");
// 匿名對象
new Person().name = "趙又廷";
new Person().showName(); // 我叫:null
}
結果:
Random類
此類的實例用於生成僞隨機數
Random API步驟
使用方法
獲取一個隨機的int數字(範圍是int所有範圍,有正負兩種):int num = r.nextInt()
獲取一個隨機的int數字(參數代表了範圍,左閉右開區間):int num = r.nextInt(3) 實際上代表的含義是:[0,3),也就是0~2
列:生成10個隨機數輸出到屏幕
public static void main(String[] args) {
Random r = new Random();
for (int i = 0; i < 100; i++) {
int num = r.nextInt(10); // 範圍實際上是0~9
System.out.println(num);
}
}
列:隨機生成一個數字,玩家有10次機會輸入數字猜數
public static void main(String[] args) {
Random r = new Random();
int randomNum = r.nextInt(100) + 1; // [1,100]
Scanner sc = new Scanner(System.in);
int i=0;
while (true) {
i++;
System.out.println("請輸入你猜測的數字:");
int guessNum = sc.nextInt(); // 鍵盤輸入猜測的數字
if (guessNum > randomNum) {
System.out.println("太大了,請重試。");
} else if (guessNum < randomNum) {
System.out.println("太小了,請重試。");
} else if (i==0) {
System.out.println("你已經輸入10次了");
break;
} else {
System.out.println("恭喜你,猜中啦!");
break; // 如果猜中,不再重試
}
}
System.out.println("遊戲結束。");
}
ArrayList類
java.util.ArrayList 是大小可變的數組的實現,存儲在內的數據稱爲元素。此類提供一些方法來操作內部存儲的元素。 ArrayList 中可不斷添加元素,其大小也自動增長。
對於ArrayList來說,有一個尖括號<E>代表泛型。 泛型:也就是裝在集合當中的所有元素,全都是統一的什麼類型。
注意:泛型只能是引用類型,不能是基本類型。
列:ArrayList添加數據
public static void main(String[] args) {
// 創建了一個ArrayList集合,集合的名稱是list,裏面裝的全都是String字符串類型的數據
// 備註:從JDK 1.7+開始,右側的尖括號內部可以不寫內容,但是<>本身還是要寫的。
ArrayList<String> list = new ArrayList<>();
System.out.println(list); // []
// 向集合當中添加一些數據,需要用到add方法。
list.add("趙麗穎");
System.out.println(list); // [趙麗穎]
list.add("迪麗熱巴");
list.add("古力娜扎");
list.add("瑪爾扎哈");
System.out.println(list); // [趙麗穎, 迪麗熱巴, 古力娜扎, 瑪爾扎哈]
// list.add(100); // 錯誤寫法!因爲創建的時候尖括號泛型已經說了是字符串,添加進去的元素就必須都是字符串才行
}
ArrayList常用方法
public boolean add(E e):向集合當中添加元素,參數的類型和泛型一致。返回值代表添加是否成功。
備註:對於ArrayList集合來說,add添加動作一定是成功的,所以返回值可用可不用。
但是對於其他集合(今後學習)來說,add添加動作不一定成功。
public E get(int index):從集合當中獲取元素,參數是索引編號,返回值就是對應位置的元素。
public E remove(int index):從集合當中刪除元素,參數是索引編號,返回值就是被刪除掉的元素。
public int size():獲取集合的尺寸長度,返回值是集合中包含的元素個數。
列:ArrayList常用方法
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
System.out.println(list); // []
// 向集合中添加元素:add
boolean success = list.add("柳巖");
System.out.println(list); // [柳巖]
System.out.println("添加的動作是否成功:" + success); // true
list.add("高圓圓");
list.add("趙又廷");
list.add("李小璐");
list.add("賈乃亮");
System.out.println(list); // [柳巖, 高圓圓, 趙又廷, 李小璐, 賈乃亮]
// 從集合中獲取元素:get。索引值從0開始
String name = list.get(2);
System.out.println("第2號索引位置:" + name); // 趙又廷
// 從集合中刪除元素:remove。索引值從0開始。
String whoRemoved = list.remove(3);
System.out.println("被刪除的人是:" + whoRemoved); // 李小璐
System.out.println(list); // [柳巖, 高圓圓, 趙又廷, 賈乃亮]
// 獲取集合的長度尺寸,也就是其中元素的個數
int size = list.size();
System.out.println("集合的長度是:" + size);
}
ArrayList 遍歷集合
ArrayList<String> list = new ArrayList<>();
list.add("迪麗熱巴");
list.add("古力娜扎");
list.add("瑪爾扎哈");
// 遍歷集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
ArrayList注意:
如果希望向集合ArrayList當中存儲基本類型數據,必須使用基本類型對應的“包裝類”。
基本類型 包裝類(引用類型,包裝類都位於java.lang包下) byte Byte short Short int Integer 【特殊】 long Long float Float double Double char Character 【特殊】 boolean Boolean
public static void main(String[] args) {
ArrayList<String> listA = new ArrayList<>();
// 錯誤寫法!泛型只能是引用類型,不能是基本類型
// ArrayList<int> listB = new ArrayList<>();
ArrayList<Integer> listC = new ArrayList<>();
listC.add(100);
listC.add(200);
System.out.println(listC); // [100, 200]
int num = listC.get(1);
System.out.println("第1號元素是:" + num);
}
列:生成6個1~33之間的隨機整數,添加到集合,並遍歷集合。
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Random r = new Random();
for (int i = 0; i < 6; i++) {
int num = r.nextInt(33) + 1;
list.add(num);
}
// 遍歷集合
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
列:自定義4個學生對象,添加到集合,並遍歷。
public class Student {
private String name;
private int age;
public Student() {
}
public Student(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
public static void main(String[] args) {
ArrayList<Student> list = new ArrayList<>();
Student one = new Student("洪七公", 20);
Student two = new Student("歐陽鋒", 21);
Student three = new Student("黃藥師", 22);
Student four = new Student("段智興", 23);
list.add(one);
list.add(two);
list.add(three);
list.add(four);
// 遍歷集合
for (int i = 0; i < list.size(); i++) {
Student stu = list.get(i);
System.out.println("姓名:" + stu.getName() + ",年齡" + stu.getAge());
}
}
列:定義以指定格式打印集合的方法(ArrayList類型作爲參數),使用{}擴起集合,使用@分隔每個元素。格式參照 {元素@元素@元素}。
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("張三丰");
list.add("宋遠橋");
list.add("張無忌");
list.add("張翠山");
System.out.println(list); // [張三丰, 宋遠橋, 張無忌, 張翠山]
printArrayList(list);
}
public static void printArrayList(ArrayList<String> list) {
// {10@20@30}
System.out.print("{");
for (int i = 0; i < list.size(); i++) {
String name = list.get(i);
if (i == list.size() - 1) {//最後一個數的下標
System.out.println(name + "}");
} else {
System.out.print(name + "@");
}
}
}