目录
Scanner类、Random类、ArrayList类
一、API概述
API(Application Programming Interface)
,应用程序编程接口。Java API
是一本程序员的字典 ,是JDK
中提供给我们使用的类的说明文档。这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。所以我们可以通过查询API
的方式,来学习Java
提供的类,并得知如何使用它们。
二、Scanner概述
- **Scanner类的功能:**可以实现键盘输入数据到程序中。
- 引用类型的一般使用步骤:
- 导包:
import 包路径.类名称;
如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写。只有java.lang
包下的内容不需要导包,其他的包都需要import
语句。 - 创建:
类名称 对象名 = new 类名称();
- 使用:
对象名.成员方法名();
- 导包:
三、Scanner的使用步骤
//1.导包
import java.util.Scanner;
public class DemoScanner {
public static void main(String[] args) {
//2.创建
//备注:System.in代表从键盘输入
Scanner sc = new Scanner(System.in);
//3.使用
//从键盘接收一个byte数字
byte num1 = sc.nextByte();
System.out.println("从键盘接收的byte数字是:" + num1);
//从键盘接收一个short数字
short num2 = sc.nextShort();
System.out.println("从键盘接收的short数字是:" + num2);
//从键盘接收一个int数字
int num3 = sc.nextInt();
System.out.println("从键盘接收的int数字是:" + num3);
//从键盘接收一个long数字
long num4 = sc.nextLong();
System.out.println("从键盘接收的long数字是:" + num4);
//从键盘接收一个float数字
float num5 = sc.nextFloat();
System.out.println("从键盘接收的float数字是:" + num5);
//从键盘接收一个double数字
double num6 = sc.nextDouble();
System.out.println("从键盘接收的double数字是:" + num6);
//从键盘接收一个String字符串
String str = sc.next();
System.out.println("从键盘接收的String字符串是:" + str);
//从键盘接收一个boolean值
boolean bool = sc.nextBoolean();
System.out.println("从键盘接收的boolean值是:" + bool);
}
}
四、Scanner练习
//1.键盘输入两个数字求和
import java.util.Scanner;
public class Sum {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("请输入第一个数字:");
int num1 = sc.nextInt();
System.out.println("请输入第一个数字:");
int num2 = sc.nextInt();
System.out.println("两个数字之和是:" + sumTwo(num1, num2));
}
public static int sumTwo(int a, int b){
return a + b;
}
}
//2.求输入的三个数字中的最大值
import java.util.Scanner;
public class Max {
public static void main(String[] args) {
int[] array = new int[3];
Scanner sc = new Scanner(System.in);
for (int i = 0; i < array.length; i++) {
System.out.println("请输入第" + (i + 1) +"个数字:");
array[i] = sc.nextInt();
}
System.out.println("最大值为:" + max(array));
}
public static int max(int[] array){
int result = array[0];
for (int i = 1; i < array.length; i++) {
if (result < array[i]){
result = array[i];
}
}
return result;
}
}
五、匿名对象
1. 匿名对象的说明
- 创建对象的标准格式:
类名称 对象名 = new 类名称();
- 匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
- 格式:
new 类名称();
- 注意事项:匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象。
- 使用建议:如果确定有一个对象只需要使用唯一的一次,就可以使用匿名对象。
- 格式:
2. 匿名对象作为方法的参数和返回值
import java.util.Scanner;
public class DemoAnonymous {
public static void main(String[] args) {
//正常创建对象
/*Scanner sc = new Scanner(System.in);
System.out.println("请输入一个数字:");
int num = sc.nextInt();
System.out.println("输入的数字是:" + num);*/
//使用匿名对象
/*System.out.println("请输入一个数字:");
int num = new Scanner(System.in).nextInt();
System.out.println("输入的数字是:" + num);*/
//匿名对象作为方法参数
//method_1(new Scanner(System.in));
//匿名对象作为方法返回值
Scanner sc = method_2();
System.out.println("请输入一个数字:");
int num = sc.nextInt();
System.out.println("输入的数字是:" + num);
}
public static void method_1(Scanner sc){
System.out.println("请输入一个数字:");
int num = sc.nextInt();
System.out.println("输入的数字是:" + num);
}
public static Scanner method_2(){
return new Scanner(System.in);
}
}
六、Random
1. 概念与使用
-
Random
类用来生成随机数字。 -
使用步骤:
- 导包:
import java.util.Random;
- 创建:
Random rd = new Random();
- 使用:
- 获取一个随机的
int
数字(范围是int
所有范围,由正负两种):int num = rd.nextInt();
- 获取一个随机的
int
数字(参数代表了范围,左闭右开区间):int num = rd.nextInt(3);
实际代表的含义是:[0,3)
。
- 获取一个随机的
import java.util.Random; public class DemoRandom { public static void main(String[] args) { /* Random rd = new Random(); int num = rd.nextInt(); System.out.println("生成的随机数是:" + num);*/ Random rd = new Random(); for (int i = 0; i < 100; i++) { int num = rd.nextInt(10); System.out.println(num); } } }
- 导包:
2. 练习
//1.生成1~n之间的随机数
import java.util.Random;
public class RandomPrac {
public static void main(String[] args) {
int n = 5;
Random rd = new Random();
for (int i = 0; i < 100; i++) {
int sum = rd.nextInt(n) + 1;
System.out.println(sum);
}
}
}
//2. 猜数字小游戏
import java.util.Random;
import java.util.Scanner;
public class RandomGame {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
Random rd = new Random();
int guessNum = rd.nextInt(101);
while (true){
System.out.println("请输入你的数字:");
int num = sc.nextInt();
if (num == guessNum){
System.out.println("恭喜你!猜对了!");
break;
}else if (num > guessNum) {
System.out.println("对不起!猜大了!");
}else {
System.out.println("对不起!猜小了!");
}
}
}
}
七、对象数组
- 定义一个数组,用来存放对象。
- 缺点:数组一旦创建,程序运行期间长度不可以发生改变。
public class Person {
private String name;
private int age;
public Person() {
}
public Person(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 class UsePerson {
public static void main(String[] args) {
Person per_1 = new Person("朱古力", 18);
Person per_2 = new Person("猪猪侠", 20);
Person per_3 = new Person("猪猪猪", 22);
Person[] per = new Person[3];
per[0] = per_1;
per[1] = per_2;
per[2] = per_3;
System.out.println("姓名:" + per[0].getName() + " 年龄:" + per[0].getAge());
System.out.println("姓名:" + per[1].getName() + " 年龄:" + per[1].getAge());
System.out.println("姓名:" + per[2].getName() + " 年龄:" + per[2].getAge());
}
}
八、ArrayList集合
1. ArrayList集合概述和基本使用
- 数组的长度不可以发生改变,但是
ArrayList
集合的长度是可以随意变化的。 - 对于
ArrayList
来说,有一个**<E>
代表泛型**。 - **泛型:**就是装在集合中的所有元素全都是统一的什么类型。
- **注意:**泛型只能是引用类型,不能是基本类型。
- 对于
ArrayList
集合来说,直接打印得到的不是地址值,而是内容。如果内容是空,得到的是空的中括号:[ ]。
//导包
import java.util.ArrayList;
public class DemoArrayList {
public static void main(String[] args) {
//创建
//创建了一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
//备注:从JDK 1.7+ 开始,右侧的尖括号内部可以不写内容,但是<>本身还是要写的
ArrayList<String> list = new ArrayList<>();
//使用
//
System.out.println(list); // []
//向集合添加元素用到add方法
list.add("朱古力");
list.add("猪猪侠");
list.add("猪猪猪");
System.out.println(list); // [朱古力, 猪猪侠, 猪猪猪]
}
}
2. ArrayList集合的常用方法和遍历
- 常用方法:
public boolean add(E e)
向集合当中添加元素,参数的类型和泛型一致。public E get(int index)
从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素。public E remove(int index)
从集合当中删除元素,参数是索引编号,返回值就是被删除的元素。public int size()
获取集合的尺寸长度,返回值是集合中包含的元素个数。
//导包
import java.util.ArrayList;
public class ArrayListMethod {
public static void main(String[] args) {
//创建
ArrayList<String> list = new ArrayList<>();
System.out.println(list); // []
//添加元素
list.add("zhuguli");
System.out.println(list); // [zhuguli]
list.add("zhuzhuxia");
list.add("zhuzhuzhu");
System.out.println(list); // [zhuguli, zhuzhuxia, zhuzhuzhu]
//得到元素
System.out.println(list.get(0)); // zhuguli
System.out.println(list.get(1)); // zhuzhuxia
System.out.println(list.get(2)); // zhuzhuzhu
//删除元素
String whoDelete = list.remove(2);
System.out.println("被删除的是:" + whoDelete); // 被删除的是:zhuzhuzhu
System.out.println(list); // [zhuguli, zhuzhuxia]
//得到数组长度
System.out.println("数组长度为:" + list.size()); // 数组长度为:2
}
}
- 遍历:
import java.util.ArrayList;
public class ArrayListMethod {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("zhuguli");
list.add("zhuzhuxia");
list.add("zhuzhuzhu");
for (int i = 0; i < list.size(); i++){
System.out.println(list.get(i));
}
}
}
3. ArrayList存储基本数据类型
-
如果希望向集合
ArrayList
当中存储基本类型数据,就必须使用基本类型对应的“包装类”。基本类型 包装类(引用类型,包装类都位于 java.lang
包下)byte Byte short Short int Integer long Long float Float double Double char Character boolean Boolean -
从
JDK1.5+
开始,支持自动装箱、自动拆箱- **自动装箱:**基本类型---->包装类型
- **自动拆箱:**包装类型---->基本类型
import java.util.ArrayList;
public class ArrayListPrac {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
System.out.println(list); // [1, 2]
int num = list.get(0);
System.out.println(num); // 1
}
}
- 一些练习
//1.生成6个1~33之间的随机整数,添加到集合,并遍历集合
import java.util.ArrayList;
import java.util.Random;
public class SaveRandomNum {
public static void main(String[] args) {
Random rd = new Random();
ArrayList<Integer> list = new ArrayList<>();
for (int i = 0; i < 6; i++) {
int num = rd.nextInt(33) + 1 ;
list.add(num);
}
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
}
}
//2.自定义四个学生对象,添加到集合并遍历
import java.util.ArrayList;
public class StudentArrayList {
public static void main(String[] args) {
ArrayList<Student> studentList = new ArrayList<>();
studentList.add(new Student("朱古力", 18));
studentList.add(new Student("猪猪侠", 20));
studentList.add(new Student("猪猪猪", 22));
for (int i = 0; i < studentList.size(); i++) {
Student temp = studentList.get(i);
System.out.println("姓名:" + temp.getName() + " 年龄:" + temp.getAge());
}
}
}
//3.定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}括起集合,使用@分隔每个字符。参照格式{元素@元素@元素}
import java.util.ArrayList;
public class PrintArrayList {
public static void main(String[] args) {
ArrayList<String> list = new ArrayList<>();
list.add("朱古力");
list.add("猪猪侠");
list.add("猪猪猪");
printArrayList(list);
}
public static void printArrayList(ArrayList<String> 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("}");
}
}
//4.用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中。 要求:使用自定义的方法来实现
import java.util.ArrayList;
import java.util.Random;
public class ArrayListReturn {
public static void main(String[] args) {
ArrayList<Integer> list = new ArrayList<>();
Random rd = new Random();
for (int i = 0; i < 20; i++) {
list.add(rd.nextInt(20) + 1);
}
System.out.println(list);
list = selectedArrayList(list);
System.out.println("筛选后的结果为:" + list);
}
public static ArrayList<Integer> selectedArrayList(ArrayList<Integer> list) {
for (int i = 0; i < list.size(); i++) {
if (list.get(i) % 2 == 0) {
System.out.println("将要删除:" + list.remove(i));
}
}
return list;
}
}