Java Day07

一、API

概述:API,应用程序编程接口。Java API是一本程序员的字典,是JDK中提供给我们使用的类的说明文档,这些类将底层的实现封装了起来,我们不需要关心这些类是如何实现,只需要学习这些类如何使用即可。

二、Scanner类

2.1 什么是Scanner类

  • 一个可以解析基本类型和字符串的简单文本扫描器。
package cn.itcast.day07.demo01;

/*
* Scanner类的功能,可以实现键盘输入数据,到程序当中。
*
* 引用类型的一般使用步骤:
*   1、导包
*       import 包路径.类名称
*   如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写
*   只有java.lang包下的内容不需要导包,其他的包都需要import语句
*
*   2、创建
*       类名称 对象名 = new 类名称();
*   3、使用
*       对象名.成员方法名()
*       获取键盘输入的一个int数字,int num = sc.nextInt();
*       获取键盘输入的一个字符串:String str = sc.next();
*
* */

import java.util.Scanner;  // 1、导包

public class Demo01Scanner {

    public static void main(String[] args) {
        // 创建
        // 备注:System.in表示从键盘进行输入
        Scanner sc = new Scanner(System.in);

        // 获取键盘输入的int数字
        int num = sc.nextInt();
        System.out.println("输入的int数字是:" + num);

        // 获取键盘输入的字符串
        String str = sc.next();
        System.out.println("输入的字符串是:" + str);

    }

}

2.2 Scanner使用步骤

  • 查看类:java.util.Scanner 该类需要导入后使用。
  • 查看构造方法:public Scanner(InputStream source) 构造一个新的Scanner,它生成的值是从指定的输入流扫描的。
  • 查看成员方法:public int nextInt 将输入信息的下一个标记扫描为一个int值。

2.3 练习

package cn.itcast.day07.demo01;

/*
* 题目:键盘输入两个int数字,并且求出合值
* */

import java.util.Scanner;

public class Demo02ScannerSum {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入第一个数字:");
        int x = sc.nextInt();
        System.out.println("请输入第二个数字:");
        int y = sc.nextInt();
        System.out.println("输入的两个数字的和为:" + (x + y));
    }
}
package cn.itcast.day07.demo01;

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


import java.util.Scanner;

public class Demo03ScannerMax {

    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 = c > temp ? c : temp;
        System.out.println("最大值是:" + max);
    }

}

2.4 匿名对象

  • 概念:创建对象时,只有创建对象的语句,却没有把对象地址赋值给某个变量。
  • 格式:
new 类名(参数列表);
  • 举例:
new Scanner(System.in);
  • 应用场景

    1、创建匿名对象直接调用方法,没有变量名。

new Scanner(System.in).nextInt();

    2、一旦调用两次方法,就是创建了两个对象,造成浪费。

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

package cn.itcast.day07.demo02;

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

    public static void methodParam(Scanner sc) {
        int num = sc.nextInt();
        System.out.println("输入的是:" + num);
    }

    public static Scanner methodReturn() {
        return new Scanner(System.in);
    }
}

三、Random类

3.1 Random类的使用步骤

  • 查看类:需要倒入后使用
  • 查看成员方法
package cn.itcast.day07.demo03;

/*
* Random类用来生成随机数字,使用起来也是三个步骤:导包、创建、使用
* */

import java.util.Random;

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

        int num = r.nextInt();  // 不带参数,范围是int所有范围,有正负两种
        System.out.println("随机数是:" + num);

    }

}

3.2 练习

package cn.itcast.day07.demo03;

/*
* 题目:
*   用代码模拟猜数字的小游戏。
* 思路:
*   1、首先需要产生一个随机数字,并且一旦产生不再变化,用Random的nextInt方法
*   2、需要键盘输入,使用Scanner中的nextInt方法
*   3、获取数字并比较
*   
* */

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(1000);
        System.out.println(num);
        while (true) {
            System.out.println("请输入一个数字");
            int n = new Scanner(System.in).nextInt();
            if (n > num) {
                System.out.println("你猜大了!!!");
            } else if (n < num) {
                System.out.println("你猜小了!!!");
            } else if (n == num) {
                System.out.println("恭喜你答对了!!!");
                break;
            }
        }
    }
}

四、ArrayList类

4.1 引入——对象数组

package cn.itcast.day07.demo04;

/*
* 定义一个数组用于存储Person对象
*
* 数组的缺点:一旦创建,程序运行期间长度不可以发生改变
* */

public class Demo01Array {
    public static void main(String[] args) {
        // 首先创建一个长度为3的数组里面存放Person对象
        Person[] people = new Person[3];
        Person one = new Person("tiger", 18);
        Person two = new Person("trony", 20);
        Person three = new Person("shi", 23);

        // 将one当中的地址值复制到数组的0号元素位置
        people[0] = one;

        people[1] = two;
        people[2] = three;
        System.out.println(people[0]);  // 地址值
        System.out.println(people[1]);  // 地址值
        System.out.println(people[2]);  // 地址值

        Person person = people[1];
        System.out.println(person.getName());

    }
}

4.2 什么是ArrayList类

  • ArrayList是大小可变的数组的实现,存储在里面的数据称为元素,此类提供一些方法来操作内部存储的元素,ArrayList中可以不断的添加元素,大小也是可变的。

4.3 ArrayList使用步骤

  • 查看类:该类需要导入后使用。ArrayList<E>,<E>表示一种指定的数据结构,叫做泛型,E,取自Element首字母,在出现E的地方,我们使用一种引用数据类型将其替换即可,表示我们将存储引用类型的元素。
ArrayList<String> 
  • 查看构造方法:构造一个内容为空的集合。
ArrayList<String> list = new ArrayList<>();
  • 查看成员方法
package cn.itcast.day07.demo04;

/*
* 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 Demo03ArrayListMethod {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        System.out.println(list);

        // 向集合中添加元素:add
        boolean success = list.add("tiger");
        System.out.println(list);
        System.out.println("添加的动作是否成功:" + success);

        list.add("trony");
        list.add("shi");
        list.add("hhhhh");
        System.out.println(list);

        // 从集合中获取元素:get,索引值从0开始
        String name = list.get(3);
        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.4 如何存储基本数据类型

ArrayList对象不能存储基本类型,只能存储引用类型的数据,类似<int>不能写,但是存储基本数据类型对应的包装类型是可以的,所以想要存储基本类型数据。<>中的数据类型,必须转换后才能编写。

package cn.itcast.day07.demo04;

import java.util.ArrayList;

/*
* 如果希望向集合ArrayList当中存储基本类型,必须使用基本类型对应的包装类
*
* 基本类型  包装类
* byte     Byte
* short    Short
* int      Integer
* long     Long
* float    Float
* double   Double
* char     Character
* boolean  Boolean
*
* 从JDK 1.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);

        int num = listC.get(1);
        System.out.println("第一号元素是:" + num);
    }
}

4.5 练习

package cn.itcast.day07.Demo05;

import java.util.ArrayList;
import java.util.Random;

public class Demo01ArrayListRandom {
    public static void main(String[] args) {
        Random random = new Random();
        ArrayList<Integer> nums = new ArrayList<>();
        for (int i = 0; i < 6; i++) {
            int num = random.nextInt(33) + 1;
            nums.add(num);
        }
        System.out.println(nums);
        for (int i = 0; i < nums.size(); i++) {
            System.out.println(nums.get(i));
        }



    }
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值