2022/06/06 day07:Scanner类、Random类、ArrayList 类

1. API

1.1 概述

API(Application Programming Interface),应用程序编程接口。Java API是一本程序员的 字典 ,是JDK中提供给
我们使用的类的说明文档。
这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。所以我们可以通过查询API的方式,来学习Java提供的类,并得知如何使用它们。

1.2 API使用步骤

  1. 打开帮助文档。
  2. 点击显示,找到索引,看到输入框。
  3. 你要找谁?在输入框里输入,然后回车。
  4. 看包。java.lang下的类不需要导包,其他需要。
  5. 看类的解释和说明。
  6. 学习构造方法。
  7. 使用成员方法。

2. Scanner类

2.1 什么事Scanner类以及使用步骤

Scanner类的功能可以实现键盘输入数据,到程序到当中。
关键字都是小写的,所以String不是关键字,而是一个类。
引用类型:只要不是基本类型就是引用类型。

引用类型的一般使用步骤:

  1. 导包 (在什么位置?)
    import 包路径.类名称;
    如果需要使用的目标类,和当前类位于同一个包当下,则可以省略导包语句不写。
    只有java.long包下的内容不需要导包,其他的包都需要import语句。 其中String类就是在java.long包中

  2. 创建对象
    类名称 对象名 = new 类名称();

  3. 使用
    对象名.成员方法名();

Scanner是一个引用类型;
获取键盘输入的一个int数字,int num = 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);

    }
}

2.2 Scanner练习

求和:
题目:键盘输入两个int数字,并且求出和值。

思路:

  1. 既然需要键盘输入,那么就用Scanner
  2. Scanner的三个步骤:导包、创建、使用
  3. 需要的是两个数字,所以需要调用两次nextInt方法
  4. 得到两个数字,就需要加在一起。
  5. 将结果打印输出。
//1. 导包    快捷键:光标在红色,按下 alt + enter
import java.util.Scanner;

public class Demo02ScannerSum {
    public static void main(String[] args) {
        //2.创建对象
        Scanner sc = new Scanner(System.in);
        //3.获取数字
        System.out.println("请输入第一个数字:");
        int num1 = sc.nextInt();
        System.out.println("请输入第二个数字:");
        int num2 = sc.nextInt();
        //4.求和
        int sam = num1 + num2;
        //5.输出
        System.out.println("结果为:"+ sam);
    }
}

求最大值:
题目:键盘输入三个数字,然后求出其中最大值。

思路:

  1. 既然是键盘输入,肯定需要用到Scanner
  2. Scanner三步骤:导包、创建、使用。
  3. 既然是三个数字,那么调用三次nextInt()方法,得到三个int变量。
  4. 无法同时判断三个数字谁最大,应该转换成两个步骤:
    1. 首先判断前两个当中谁最大,拿到前两个的最大值;
    2. 那着前两个数字的最大值,再和第三个数字比较,得到三个数字当中的最大值。
  5. 打印最终结果。
//1. 导包
import java.util.Scanner;


public class Demo03ScannerMax {
    public static void main(String[] args) {
        //2. 创建对象
        Scanner sca = new Scanner(System.in);
        //3. 输入三个数字
        System.out.println("请输入第一个数字:");
        int num1 = sca.nextInt();
        System.out.println("请输入第二个数字:");
        int num2 = sca.nextInt();
        System.out.println("请输入第三个数字:");
        int num3 = sca.nextInt();
        //4. 比较大小
        int max = num1 > num2 ? num1 : num2;
        int max1 = max > num3 ? max : num3;
        System.out.println("最大值为" + max1);


    }
}

2.3 匿名对象

创建对象的标准格式:
类名称 对象名 = new 类名称();

匿名对象就是只有右边的对象,没有左边的名字和赋值运算符:
new 类名称();

注意事项:
匿名对象只能使用唯一的一次,下次再用不得不再创建新对象。
使用建议:如果确定有一个对象只需要使用唯一的一次,就可以使用匿名对象。

例子:

public class Demo01Anonymous {//Anonymous 匿名

    public static void main(String[] args) {

        //左边的one就是对象的名字
        Person one = new Person();
        one.name = "李思思";
        one.showName();//我叫李思思。

        System.out.println("----------------");

        //匿名对象
        new Person().name = "喜洋洋";//创建了第二个对象,但是这个地址谁都没有用上。
        new Person().showName();//第三个对象,成员变量String默认为null。
    }
}

匿名对象可以作为方法的参数和返回值:

用匿名对象作为方法的参数;

用匿名对象作为返回值;

例子:

import java.util.Scanner;
public class Demo02Anonymous {
    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);


        //使用一般写法传入参数;
//        Scanner sc = new Scanner(System.in);
//        methodParam(sc);

        //使用匿名对象来进行传参
        methodParam(new Scanner(System.in));//这里要的只是一个参数/名字/地址值/

        //使用匿名对象作为返回值
        Scanner sc = methodReturn();//使用了方法名,记录了上一个对象的地址值。
        int num = sc.nextInt();//输入键盘数字
        System.out.println("输入的是:" + num);//打印数字

        System.out.println("输入的是:" + methodReturn().nextInt());//可以直接打印
    }

    public static void methodParam(Scanner sc){//这个参数名获得了地址值
        int num = sc.nextInt();//拿出来。快捷键:alt + enter 自动创建赋值语句
        System.out.println("输入的是:" + num);
    }

    public static Scanner methodReturn()
    {
        return  new Scanner(System.in);//return一个地址
    }
}

3. Random类

3.1 Random以及使用步骤

random用来生成随机数字。
使用起来也是三个步骤:

  1. 导包
    import java.util.Random;
  2. 创建
    Random r = new Random();//小括号中留空即可
  3. 使用
    获取一个随机的int数字(范围int 所有,有正负):int num = r.nextInt();

例子:

import java.util.Random;
public class Demo01Random {//Random 随机

    public static void main(String[] args) {
        Random r = new Random();

        int num = r.nextInt();
        System.out.println("随机数是:" + num);
        
    }
}

获取一个随机的int数字(参数代表了范围,左右开闭区间):int num = r.nextInt(3)
实际上代表的含义是:[0,3),也即是0–2。
例子:

import java.util.Random;

public class Demo02Random {
    public static void main(String[] args) {
        Random r = new Random();
        for (int i = 0; i < 10; i++) {
            int num = r.nextInt(10);
            System.out.println("第" + ( i + 1 ) + "个随机数:" + num);
        }
    }
}

3.2 Random练习题

题目要求:
根据int变量的n的值,来获取随机数字,范围是[1,n],可以取到1.也可以取到n。

思路:

  1. 定义一个变量n 随意赋值
  2. 要使用一个Random:三个步骤:导包,创建,使用
  3. 如果写10,那么是09,然而想要的是110,可以发现整体+1即可。
  4. 打印随机数字
public class Demo03Random {
    public static void main(String[] args) {
        int n = 5;

        for (int i = 0; i < 100; i++) {
            Random r = new Random();
            int num = r.nextInt(n);//0--n-1
            System.out.println("随机数是:" + (num + 1));//整体加1之后变成了[1,n]
        }

    }
}

题目:用代码模拟猜数字小游戏。

思路:

  1. 首先需要产生一个随机数字,并且一旦产生不再变化。用Random的nextInt方法。
  2. 需要键盘输入,所以用到了Scanner。
  3. 获取键盘输入的数字,用Scanner当中的nextInt方法。
  4. 已经得到两个数字,判断(if)一下
    如果太大,提示太大,并且重试;
    如果太小,提示太小,并且重试;
    如果猜中了,游戏结束。
  5. 重试就是再来一次,循环次数不确定,用while(true)。
import java.util.Random;
import java.util.Scanner;
public class Demo04RandomGame {
    public static void main(String[] args) {
        Random r = new Random();
        int num = r.nextInt(101);//[0,100]

        Scanner sca = new Scanner(System.in);

        System.out.println("您只有10次机会~");
        for (int i = 0; i < 10; i++) {
            int a = 10;
            System.out.println("您还有" + (a - i) + "次机会~");
            System.out.println("请输入你的猜测:");
            int num1 = sca.nextInt();

            if(num1 > num){
                System.out.println("大了!");
            }else if(num1 < num) {
                System.out.println("小了");
            }else {
                System.out.println("恭喜通关!");
                break;
            }
            }
        }
    }

4. ArrayList集合

4.1 引入——对象数组

题目:定义一个数组,用来存储三个Person对象。

数组有一个缺点:一旦创建,程序运行期间,长度不可改变。太麻烦。
还有一个更好的容器,可以存储多个数据,而且比数组更好用,就是ArrayList集合。

例子:

public class Demo01Array {
    public static void main(String[] args) {
        //首先创建一个长度为三的数组,里面用来存放Person类型的对象。
        Person[] array = new Person[3];//引用元素默认值为null。
//        System.out.println(array[0]);

        Person one = new Person("迪丽热巴",20);
        Person two = new Person("古力娜扎",18);
        Person three = new Person("马尔扎哈",30);

        //将one当中的地址值赋值到数组0号元素位置。
        array[0] = one;
        array[1] = two;
        array[2] = three;
//      System.out.println(array[0]);//地址值cn.itcast.day07.demo04.Person@75412c2f

        System.out.println(array[0].getName() + array[0].getAge());

    }
}
public class Person {

    private String name;
    private int age;

    //剩下的代码不用手写了,用alt + insert

    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;
    }
}

4.2 ArrayList类以及使用步骤

数组的长度不可以发生改变;
但是ArrayList集合的长度是可以随机变化的。

对于ArrayList来说,有一个尖括号代表泛型。
泛型,也即是装在集合当中的所有元素,全都是统一的什么类型。
注意:泛型只能是引用类型,不能是基本类型。

注意事项:
对于ArrayList集合来说,直接打印得到的不是地址值,而是内容。
如果内容是空,得到的是空的中括号[ ]。

例子:

public class Demo02ArrayList {
    public static void main(String[] args) {

        //创建了一个ArrayList集合,集合的名称时list,里面装的全都是String字符串类型的数据。
       //备注:从JDK1.7开始,右侧的尖括号右侧可以不写内容,但是其本身还是要写上的。
        ArrayList<String> list = new ArrayList<String>();//创建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);//错误写法,创建时,泛型已经说了是字符串,那么添加进去的元素应该是字符串,否者报错。
    }
}

4.3 ArrayList常用的方法和遍历

ArrayList当中常用方法有:

public boolean add(E e);向集合当中添加元素,参数的类型和泛型一致。返回值代表添加是否成功。
备注:对于ArrayList集合来说。add添加动作一定是成功的,所以返回值可不用。
但对于其他集合(今后学习)来说,add添加动作不一定成功。
public E get(int index);从集合当中获取【读取】元素,参数是索引编号,返回值就是对应位置的元素。
public E remove(int index);从集合当中删除元素,参数是索引编号,返回值就是被删除掉的元素。
public int size();获取集合的尺寸长度,返回值是集合中包含的元素个数。

例子:

public class Demo03ArrayListMethod {
    public static void main(String[] args) {

        ArrayList<String> list = new ArrayList<>();
        System.out.println(list);

        //向集合中添加东西
        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("第二号索引位置:" + name);//第二号索引位置:赵又廷


        //从集合中删除元素:remove。索引值从0开始。
        String whoRemoved = list.remove(3);//用快捷键直接生成赋值信息。
        System.out.println("被删除的人:" + whoRemoved);//被删除的人:李小璐
        System.out.println(list);//[柳岩, 高圆圆, 赵又廷, 贾乃亮]


        //获取集合的长度尺寸,也即是其中的元素个数
        int size = list.size();
//        System.out.println("集合的长度是:" + size);//集合的长度是:4
    }
}

遍历集合

import java.util.ArrayList;

public class Demo04ArrayListEach {
    public static void main(String[] args) {

        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));
        }
    }
}

4.5 如何存储基本类型数据

如果希望向集合ArrayList当中存储基本类型,必须使用基本类型对应的‘包装类’。
包装类:给基本类型套上一个小盒子。

基本类型 包装类(引用类型,包装类都位于java.long包下,不用导包了。
byte Byte
short Short
int Integer 【特殊】
long Long
float Float
double Double
char Character 【特殊】
boolean Boolean

从JDK1.5+ 开始,支持自动装箱、自动拆箱。
自动装箱:基本类型自动变成引用类型【包装类型】 基本类型 --> 包装类型
自动拆箱:包装类型自动拆成一个基本类型 包装类型 – > 基本类型

例子:

import java.util.ArrayList;
public class Demo05ArrayListBasic {
    public static void main(String[] args) {
        ArrayList<String> listA = new ArrayList<>();
        //错误写法,泛型只能是引用类型,不能是基本类型,基本类型没有地址值
//        ArrayList<int> listA = new ArrayList<>();


        ArrayList<Integer> listC = new ArrayList<>();
        listC.add(100);
        listC.add(200);
        listC.add(300);
        listC.add(400);
        System.out.println(listC);
        int num = listC.get(1);
        System.out.println("第1号元素为" + num);
    }
}

4.6 ArrayList练习

数值添加到集合
题目:
生成6个1~33之间的随机整数,添加到集合,并遍历。

思路:

  1. 需要存储6个数字,创建一个集合,
  2. 产生随机数,需要用到Random
  3. 用循环6次来产生6个随机数字,for循环。
  4. 循环内调用r.nextInt(int n),参数是33,0-33,整体+1才是1-33.
  5. 把数字添加到集合中看,add‘
  6. 遍历集合:for、size、get
public class Demo01ArrayListRandom {
    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个学生对象,添加到集合,并遍历。

思路:

  1. 自定义学生类,四部分。
  2. 创建一个集合,用来创建学生对象。泛型:
  3. 根据类,创建4个学生对象。
  4. 将4个学生对象添加到集合中:add
  5. 遍历集合:for、size、get
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 class Demo02ArrayListStudent {
    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类型作为参数),使用{}扩起集合,使用@分隔每个元素。
格式参照 {元素 @元素@元素}。

System.out.println(list);// [10, 20, 30]
printArrayList(list); // {10@20@30}

public class Demo03ArrayListPrint {
    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);
        /*
        定义方法的三要素:
        返回值类型:只是进行打印而已,没有运算,没有结果,所以用void。
        方法名称:printArrayList
        参数名称:ArrayList
         */

    }

    public static void printArrayList(ArrayList<String> list){
        System.out.print("{");
        for (int i = 0; i < list.size(); i++) {
            String name = list.get(i);
            if(i == list.size() - 1)
            {
                System.out.print(name + "}");
            }else{
            System.out.print(name + "@");
        }
    }
}}

题目:用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放在小集合当中。
要求使用自定义的方法实现筛选。

分析:

  1. 需要创建一个集合,用来存储int数字。。
  2. 随机数字就用Random nextInt。
  3. 循环20次,把随机数字放入大集合:for循环、add方法
  4. 定义一个方法,用来进行筛选。
    筛选:根据大集合,筛选符合要求的元素,得到小集合
    三要素:
    返回值类型:ArrayList小集合(里面元素个数不确定)
    方法名称:getSmallList
    方法列表:ArrayList大集合(装着20个随机数字)
    5.判断(if)是偶数:num % 2 == 0.
  5. 如果是偶数,就放到小集合当中,否者不放。
public class Demo04ArrayListReturn {
    public static void main(String[] args) {
        ArrayList<Integer> bigList = new ArrayList<>();
        Random r = new Random();
        for (int i = 0; i < 20; i++) {
            int num = r.nextInt(100) + 1;//1--100
            bigList.add(num);
        }

        ArrayList<Integer> smallList = getSmallList(bigList);

        System.out.println("偶数总共有对少个:" + smallList.size());
        for (int i = 0; i < smallList.size(); i++) {
            System.out.println(smallList.get(i));
        }
    }
    //这个方法,接收大集合参数,返回小集合结果
    public static ArrayList<Integer> getSmallList(ArrayList<Integer> bigList){

        //创建一个小集合,用来装偶数结果
        ArrayList<Integer> smallList = new ArrayList<>();
        for (int i = 0; i < bigList.size(); i++) {
            int num = bigList.get(i);
            if(num % 2 == 0){
                smallList.add(num);
            }
        }
        return smallList;
    }
}


                                                                                 ——此文档为学习笔记!
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值