第七天:Scanner类、Random类、ArrayList类
Scanner类、Random类、ArrayList类
1.Scanner类
- Scanner类的功能,可以实现键盘输入
- 引用类型的一般使用步骤
- 导包
- import 包路径.类名称
- 如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写
- 只有 java.lang 包下的内容不需要导包,其他的包都需要import语句
- 创建
- 类名称 对象名 = new 类名称()
- 使用
- 对象名.成员方法名()
- 导包
- 获取键盘输入一个int数字,sc.nextInt();
- 获取键盘输入一个字符串,String str = sc.next();
实例
import java.util.Scanner; // 1、导包
public class Demo01Scanner {
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);
}
/*
结果:
1
输入的int数字是:1
1
输的字符串是:1
* */
}
- 键盘输入两个int数字,并且求出和值
import java.util.Scanner;
public class Demo02ScannerSum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个int数字:");
int a = sc.nextInt();
System.out.println("请输入第二个int数字:");
int b = sc.nextInt();
System.out.println("输入的数字和为:" + (a + b));
}
/*
请输入第一个int数字:
1
请输入第二个int数字:
1
输入的数字和为:2
* */
}
- 键盘输入三个int数字,然后求出其中的最大值
import java.util.Scanner;
public class Demo03ScannerMax {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个int数字:");
int a = sc.nextInt();
System.out.println("请输入第二个int数字:");
int b = sc.nextInt();
System.out.println("请输入第三个int数字:");
int c = sc.nextInt();
int max = a > b ? a : b;
max = max > c ? max : c;
// if (a > b) {
// max = a;
// } else {
// max = b;
// }
// if (max < c) {
// max = c;
// }
System.out.println("最大值为:" + max);
}
/*
结果:
请输入第一个int数字:
2
请输入第二个int数字:
1
请输入第三个int数字:
4
最大值为:4
* */
}
- 创建对象的标准格式
- 类名称 对象名 = new 类名称();
- 匿名对象是只有右边对象,没有左边的名字和赋值运算符
- new 类名称();
注意事项
- 匿名对象只能使用唯一一次,下次再用不得不再创建一个对象
- 如果确定一个对象只需要使用一次,就可以使用匿名对象
实例
public class Person {
String name;
public void showName() {
System.out.println("我叫:" + name);
}
}
public class 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
* */
}
- 实例2
import java.util.Scanner;
public class Demo02Anonymous {
public static void main(String[] args) {
// 普通使用方式
// Scanner sc = new Scanner(System.in);
// int num1 = sc.nextInt();
// 匿名对象的方式
// int num2 = new Scanner(System.in).nextInt();
// System.out.println("输入的是:" + num2);
// 使用一般写法传入参数
// Scanner sc = new Scanner(System.in);
// methodParam(sc);
// 使用匿名对象来进行传入参数
// methodParam(new Scanner(System.in));
Scanner sc = methodReturn();
int num = sc.nextInt();
System.out.println("输入的是:" + num);
}
public static void methodParam(Scanner sc){
int num = sc.nextInt();
System.out.println("输入的是:" + num);
}
public static Scanner methodReturn(){
// Scanner sc = new Scanner(System.in);
// return sc;
return new Scanner(System.in);
}
}
2.Random类
- 导包
- import java.util.Random
- 创建
- Random r = new Ranom(); // 小括号留空即可
- 使用
- 获取一个随机的int数字(范围是int所有数字,有正负两种)
- int num = r.nextInt()
- 获取一个随机的int数字(参数代表范围,左闭右开区间)
- int num = r.nextInt(3)
- 实际代表的含义:[0.3),也就是0~2
实例
import java.util.Random;
public class Demo01Random {
public static void main(String[] args) {
Random r = new Random();
int num = r.nextInt();
System.out.println("随机数是:" + num); // 随机数是:-246064806
}
}
- 根据int变量的值,来获取随机数字,范围是[1,n],可以去到1也可以去到n
import java.util.Random;
/*
* 思路
* 1、定义一个int变量num,随机赋值
* 2、要使用Random,三个步骤:导包,创建,使用
* 3、如果写10,那么就是0~9,而想要1~10,可以发现,整体+1即可
* 4、打印随机数字
* */
public class Demo03Random {
public static void main(String[] args) {
Random random = new Random();
for (int i = 0; i < 10; i++) {
// 本来范围是[0,n),整体+1后变成了[1,n+1),也就是[1,n]
int num = random.nextInt(10) + 1;
System.out.println("随机数是:" + num);
}
}
}
- 要求用代码模拟猜数字游戏
import java.util.Random;
import java.util.Scanner;
/*
* 思路:
* 1、首先需要产生一个随机数字,并且一旦产生不在变化。用Random的nextInt方法
* 2、需要键盘输入,所以用到了Scanner
* 3、获取键盘输入的数字,用Scanner当中的nextInt方法
* 4、已经得到两个数字,判断(if)一下
* 如果太大了,提示太大了,并且重试
* 如果太小了,提示太小了,并且重试
* 如果猜中了,游戏结束
* 5、重试就是在来一次,循环次数不确定,用while(true)
* */
public class Demo04RandomGame {
public static void main(String[] args) {
Random random = new Random();
int randomNum = random.nextInt(100) + 1;
// boolean flag = true;
int i = 0;
while (true) {
System.out.println("请输入一个1到100之间的数字:");
Scanner scanner = new Scanner(System.in);
int guessNum = scanner.nextInt();
if (guessNum > randomNum) {
System.out.println("太大了");
i++;
} else if (guessNum < randomNum) {
System.out.println("太小了");
i++;
} else {
i++;
System.out.println("猜对了,一共猜了" + i + "次");
break; // 如果猜中,不在尝试
}
}
System.out.println("游戏结束!");
}
}
3.ArrayList类
- 数组有一个缺点,一旦创建,程序运行期间长度不可发生改变
- 但是ArrayList集合的长度是可以发生改变的
- 对于ArrayList来说,有一个尖括号
代表泛型 - 泛型:也就是装在集合当中的所有元素,全部是统一的什么类型
- 注意:泛型只能是引用类型,不能是基本类型
注意事项
- 对于ArrayList集合来说,直接打印得到的不是地址值,而是内容
- 如果内容为空,得到的的:{}
实例
public class Demo02ArrayList {
public static void main(String[] args) {
// 创建一个ArrayList集合,集合的名称是List,里面全都是String字符串类型的数据
// 备注:从JDK1.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(); 获取集合的尺寸长度,返回值是集合中包含的元素个数
实例
import java.util.ArrayList;
public class Demo03ArrayListMethod {
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); // [柳岩, 高圆圆, 赵又廷, 李小璐, 贾乃亮]
System.out.println("================");
// 从集合中获取元素,get,索引值从0开始
String name = list.get(2);
System.out.println("第二号索引位置:" + name); // 赵又廷
System.out.println("================");
// 从集合中删除元素,remove,索引值从0开始
String whoRemove = list.remove(3); // 李小璐
System.out.println("被删除的人是" + whoRemove);
System.out.println(list);
System.out.println("================");
// 获取集合长度尺寸,也就是其中元素的个数
int size = list.size();
System.out.println("集合长度是:" + size); // 4
}
/*
集合:
[]
[柳岩]
添加的动作是否成功:true
[柳岩, 高圆圆, 赵又廷, 李小璐, 贾乃亮]
================
第二号索引位置:赵又廷
================
被删除的人是李小璐
[柳岩, 高圆圆, 赵又廷, 贾乃亮]
================
集合长度是:4
* */
}
- 如果希望向集合ArrayList当中存储基本类型的数据,必须使用基本类型对应的“包装类”
* 基本类型 包装类(引用类型,包装类位于java.lang包下面)
* byte Byte
* short Short
* int Integer 【特殊】
* long Long
* float Float
* double Double
* char Character 【特殊】
* boolean Boolean
- 从JDK1.5+开始,支持自动装箱,自动拆箱
- 自动装箱:基本类型 --> 包装类型
- 自动拆箱:包装类型 --> 基本类型
实例
public class Demo05ArrayListBasic {
public static void main(String[] args) {
ArrayList<String> listA = new ArrayList<>();
// 错误写法!泛型只能是引用类型,不能是基本类型
// ArrayList<int> ListB = new ArrayList<int>();
ArrayList<Integer> listc = new ArrayList<>();
listc.add(100);
listc.add(200);
System.out.println(listc); // [100, 200]
Integer num = listc.get(1);
System.out.println("第一号元素是:"+ num); // 第一号元素是:200
}
}
- 题目:生成6个1~33之间的随机整数,添加到集合,并遍历
import java.util.ArrayList;
import java.util.Random;
/*
* 思路;
* 1、需要储存6个数字,创建一个集合,<Integer>
* 2、产生随机数,需要用到Random
* 3、用循环6次,来产生6个随机数字,for循环
* 4、循环内调用random.nextInt(int n),参数是33,0~32,整体加1才是1~33
* 5、把数字添加到集合中:add
* 6、遍历集合:for,size,get
* */
public class Demo01ArrayListRandom {
public static void main(String[] args) {
Random random = new Random();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 6; i++) {
int randomNum = random.nextInt(33) + 1;
list.add(randomNum);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
/*
结果:
16
31
32
25
24
15
* */
}
- 题目:自定义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;
}
}
import java.util.ArrayList;
/*
* 思路:
* 1、自定义Student学生类,四个部分
* 2、创建一个集合,用来存储学生对象,四个部分
* 3、根据类,创建4个学生对象
* 4、将4个学生对象添加到集合中:add
* 5、遍历集合:for、size、get
* */
public class Demo02ArrayListStudent {
public static void main(String[] args) {
Student stu1 = new Student("李华", 18);
Student stu2 = new Student("王五", 20);
Student stu3 = new Student("赵六", 24);
Student stu4 = new Student("李四", 8);
ArrayList<Student> list = new ArrayList<>();
list.add(stu1);
list.add(stu2);
list.add(stu3);
list.add(stu4);
System.out.println(list);
for (int i = 0; i < list.size(); i++) {
System.out.println("姓名:" + list.get(i).getName() + ",年龄" + list.get(i).getAge());
}
}
/*
集合:
姓名:李华,年龄18
姓名:王五,年龄20
姓名:赵六,年龄24
姓名:李四,年龄8
* */
}
- 题目:定义指定格式打印集合的方法(ArrayList类型作为参数),使用{}括起集合,使用@分隔每个参数;格式参照
import java.util.ArrayList;
public class Demo03ArrayListPrint {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(10);
list.add(20);
list.add(30);
System.out.println(list); // [10, 20, 30]
printArrayList(list); // {10@20@30}
}
/*
* 定义方法的三要素
* 返回值类型《只是进行打印而已,没有运算,没有结果
* 方法名称:printArrayList
* 参数列表 ArrayList
* */
public static void printArrayList(ArrayList<Integer> list) {
System.out.print("{");
for (int i = 0; i < list.size(); i++) {
if (i == list.size() - 1) {
System.out.print(list.get(i));
} else {
System.out.print(list.get(i) + "@");
}
}
System.out.println("}");
}
}
- 题目:用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中
- 要求使用自定义的方法来实现筛选筛选
import java.util.ArrayList;
import java.util.Random;
/*
* 分析
* 1、需要创建一个大集合,用来存储int数字,<Integer>
* 2、随机数字就用Random nextInt
* 3、循环20次,把随机数字放入大集合,for循环,add循环
* 4、定义一个方法,用来进行筛选
* 筛选,根据大集合,筛选符合要求的元素,得到小集合
* 三要素
* 返回值类型:ArrayList小集合(里面元素个数补确定)
* 方法名称:getSmallList
* 参数列表:ArrayList大集合(装20个随机数字)
* 5、判断(if)是偶数:num % 2 == 0
* 6、如果是偶数,就放到小集合当中,否则不放
* */
public class Demo04ArrayListReturn {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Random random = new Random();
for (int i = 0; i < 20; i++) {
int randomNum = random.nextInt(100) + 1;
list.add(randomNum);
}
ArrayList<Integer> samllList = getSamllList(list);
System.out.println("偶数个数为:" + samllList.size()); // 偶数个数为:10
System.out.println(samllList); // [58, 20, 42, 98, 78, 34, 70, 30, 56, 22]
}
public static ArrayList<Integer> getSamllList(ArrayList<Integer> list) {
ArrayList<Integer> list1 = new ArrayList<>();
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 == 0) {
list1.add(list.get(i));
}
}
return list1;
}
}

浙公网安备 33010602011771号