• Java基础语法Day_07(1-3 常用API第一部分)


     常用API第一部分

    第1节 Scanner类
            day07_01_API概述和使用步骤(使用最基本的三个步骤 搜索 构造方法  方法)

      day07_02_Scanner概述及其API文档的使用

      day07_03_Scanner的使用步骤

      day07_04_Scanner练习一_键盘输入两个数字求和
      day07_05_Scanner练习二_键盘输入三个数字求最大值

    第2节 匿名对象
            day07_06_匿名对象的说明

      day07_07_匿名对象作为方法的参数和

    第3节 Random类
            day07_08_Random概述和基本使用

      day07_09_Random生成指定范围的随机数

      day07_10_Random练习一_生成1-n之间的随机数

      day07_11_Random练习二_猜数字小游戏

      课后练习

    第4节 ArrayList集合

      day07_12_对象数组

      day07_13_ArrayList集合概述和基本使用

      day07_14_ArrayList集合的常用方法和遍历

      day07_15_ArrayList集合存储基本数据类型

      day07_16_ArrayList练习一_存储随机数字

      day07_17_ArrayList练习二_存储自定义对象

      day07_18_ArrayList练习三_按指定格式遍历集合字符串

      day07_19_ArrayList练习四_筛选集合中的随机数

    小节测试

    课后练习

     1 package cn.itcast.day07.demo01;
     2 
     3 import java.util.Scanner; // 1. 导包
     4 
     5 /*
     6 Scanner类的功能:可以实现键盘输入数据,到程序当中。
     7 
     8 引用类型的一般使用步骤:
     9 
    10 1. 导包
    11 import 包路径.类名称;
    12 如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写。
    13 只有java.lang包下的内容不需要导包,其他的包都需要import语句。
    14 
    15 2. 创建
    16 类名称 对象名 = new 类名称();
    17 
    18 3. 使用
    19 对象名.成员方法名()
    20 
    21 获取键盘输入的一个int数字:int num = sc.nextInt();
    22 获取键盘输入的一个字符串:String str = sc.next();
    23  */
    24 public class Demo01Scanner {
    25 
    26     public static void main(String[] args) {
    27         // 2. 创建
    28         // 备注:System.in代表从键盘进行输入
    29         Scanner sc = new Scanner(System.in);
    30 
    31         // 3. 获取键盘输入的int数字
    32         int num = sc.nextInt();
    33         System.out.println("输入的int数字是:" + num);
    34 
    35         // 4. 获取键盘输入的字符串
    36         String str = sc.next();
    37         System.out.println("输入的字符串是:" + str);
    38     }
    39 
    40 }
     1 package cn.itcast.day07.demo01;
     2 
     3 import java.util.Scanner;
     4 
     5 /*
     6 题目:
     7 键盘输入两个int数字,并且求出和值。
     8 
     9 思路:
    10 1. 既然需要键盘输入,那么就用Scanner
    11 2. Scanner的三个步骤:导包、创建、使用
    12 3. 需要的是两个数字,所以要调用两次nextInt方法
    13 4. 得到了两个数字,就需要加在一起。
    14 5. 将结果打印输出。
    15  */
    16 public class Demo02ScannerSum {
    17 
    18     public static void main(String[] args) {
    19         Scanner sc = new Scanner(System.in);
    20 
    21         System.out.println("请输入第一个数字:");
    22         int a = sc.nextInt();
    23         System.out.println("请输入第二个数字:");
    24         int b = sc.nextInt();
    25 
    26         int result = a + b;
    27         System.out.println("结果是:" + result);
    28     }
    29 
    30 }
     1 package cn.itcast.day07.demo01;
     2 
     3 import java.util.Scanner;
     4 
     5 /*
     6 题目:
     7 键盘输入三个int数字,然后求出其中的最大值。
     8 
     9 思路:
    10 1. 既然是键盘输入,肯定需要用到Scanner
    11 2. Scanner三个步骤:导包、创建、使用nextInt()方法
    12 3. 既然是三个数字,那么调用三次nextInt()方法,得到三个int变量
    13 4. 无法同时判断三个数字谁最大,应该转换成为两个步骤:
    14     4.1 首先判断前两个当中谁最大,拿到前两个的最大值
    15     4.2 拿着前两个中的最大值,再和第三个数字比较,得到三个数字当中的最大值
    16 5. 打印最终结果
    17  */
    18 public class Demo03ScannerMax {
    19 
    20     public static void main(String[] args) {
    21         Scanner sc = new Scanner(System.in);
    22 
    23         System.out.println("请输入第一个数字:");
    24         int a = sc.nextInt();
    25         System.out.println("请输入第二个数字:");
    26         int b = sc.nextInt();
    27         System.out.println("请输入第三个数字:");
    28         int c = sc.nextInt();
    29 
    30         // 首先得到前两个数字当中的最大值
    31         int temp = a > b ? a : b;
    32         int max = temp > c ? temp : c;
    33         System.out.println("最大值是:" + max);
    34     }
    35 
    36 }
     1 package cn.itcast.day07.demo02;
     2 
     3 /*
     4 创建对象的标准格式:
     5 类名称 对象名 = new 类名称();
     6 
     7 匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
     8 new 类名称();
     9 
    10 注意事项:匿名对象只能使用唯一的一次,下次再用不得不再创建一个新对象。
    11 使用建议:如果确定有一个对象只需要使用唯一的一次,就可以用匿名对象。
    12  */
    13 public class Demo01Anonymous {
    14 
    15     public static void main(String[] args) {
    16         // 左边的one就是对象的名字
    17         Person one = new Person();
    18         one.name = "高圆圆";
    19         one.showName(); // 我叫高圆圆
    20         System.out.println("===============");
    21 
    22         // 匿名对象
    23         new Person().name = "赵又廷";
    24         new Person().showName(); // 我叫:null
    25     }
    26 
    27 }
     1 package cn.itcast.day07.demo02;
     2 
     3 public class Person {
     4 
     5     String name;
     6 
     7     public void showName() {
     8         System.out.println("我叫:" + name);
     9     }
    10 
    11 }
     1 package cn.itcast.day07.demo02;
     2 
     3 import java.util.Scanner;
     4 
     5 public class Demo02Anonymous {
     6 
     7     public static void main(String[] args) {
     8         // 普通使用方式
     9 //        Scanner sc = new Scanner(System.in);
    10 //        int num = sc.nextInt();
    11 
    12         // 匿名对象的方式
    13 //        int num = new Scanner(System.in).nextInt();
    14 //        System.out.println("输入的是:" + num);
    15 
    16         // 使用一般写法传入参数
    17 //        Scanner sc = new Scanner(System.in);
    18 //        methodParam(sc);
    19 
    20         // 使用匿名对象来进行传参
    21 //        methodParam(new Scanner(System.in));
    22 
    23         Scanner sc = methodReturn();
    24         int num = sc.nextInt();
    25         System.out.println("输入的是:" + num);
    26     }
    27 
    28     public static void methodParam(Scanner sc) {
    29         int num = sc.nextInt();
    30         System.out.println("输入的是:" + num);
    31     }
    32 
    33     public static Scanner methodReturn() {
    34 //        Scanner sc = new Scanner(System.in);
    35 //        return sc;
    36         return new Scanner(System.in);
    37     }
    38 
    39 }
     1 package cn.itcast.day07.demo03;
     2 
     3 import java.util.Random;
     4 
     5 /*
     6 Random类用来生成随机数字。使用起来也是三个步骤:
     7 
     8 1. 导包
     9 import java.util.Random;
    10 
    11 2. 创建
    12 Random r = new Random(); // 小括号当中留空即可
    13 
    14 3. 使用
    15 获取一个随机的int数字(范围是int所有范围,有正负两种):int num = r.nextInt()
    16 获取一个随机的int数字(参数代表了范围,左闭右开区间):int num = r.nextInt(3)
    17 实际上代表的含义是:[0,3),也就是0~2
    18  */
    19 public class Demo01Random {
    20 
    21     public static void main(String[] args) {
    22         Random r = new Random();
    23 
    24         int num = r.nextInt();
    25         System.out.println("随机数是:" + num);
    26     }
    27 
    28 }
     1 package cn.itcast.day07.demo03;
     2 
     3 import java.util.Random;
     4 
     5 public class Demo02Random {
     6 
     7     public static void main(String[] args) {
     8         Random r = new Random();
     9 
    10         for (int i = 0; i < 100; i++) {
    11             int num = r.nextInt(10); // 范围实际上是0~9
    12             System.out.println(num);
    13         }
    14     }
    15 
    16 }
     1 package cn.itcast.day07.demo03;
     2 
     3 import java.util.Random;
     4 
     5 /*
     6 题目要求:
     7 根据int变量n的值,来获取随机数字,范围是[1,n],可以取到1也可以取到n。
     8 
     9 思路:
    10 1. 定义一个int变量n,随意赋值
    11 2. 要使用Random:三个步骤,导包、创建、使用
    12 3. 如果写10,那么就是0~9,然而想要的是1~10,可以发现:整体+1即可。
    13 4. 打印随机数字
    14  */
    15 public class Demo03Random {
    16 
    17     public static void main(String[] args) {
    18         int n = 5;
    19         Random r = new Random();
    20 
    21         for (int i = 0; i < 100; i++) {
    22             // 本来范围是[0,n),整体+1之后变成了[1,n+1),也就是[1,n]
    23             int result = r.nextInt(n) + 1;
    24             System.out.println(result);
    25         }
    26 
    27     }
    28 
    29 }
     1 package cn.itcast.day07.demo03;
     2 
     3 import java.util.Random;
     4 import java.util.Scanner;
     5 
     6 /*
     7 题目:
     8 用代码模拟猜数字的小游戏。
     9 
    10 思路:
    11 1. 首先需要产生一个随机数字,并且一旦产生不再变化。用Random的nextInt方法
    12 2. 需要键盘输入,所以用到了Scanner
    13 3. 获取键盘输入的数字,用Scanner当中的nextInt方法
    14 4. 已经得到了两个数字,判断(if)一下:
    15     如果太大了,提示太大,并且重试;
    16     如果太小了,提示太小,并且重试;
    17     如果猜中了,游戏结束。
    18 5. 重试就是再来一次,循环次数不确定,用while(true)。
    19  */
    20 public class Demo04RandomGame {
    21 
    22     public static void main(String[] args) {
    23         Random r = new Random();
    24         int randomNum = r.nextInt(100) + 1; // [1,100]
    25         Scanner sc = new Scanner(System.in);
    26 
    27         while (true) {
    28             System.out.println("请输入你猜测的数字:");
    29             int guessNum = sc.nextInt(); // 键盘输入猜测的数字
    30 
    31             if (guessNum > randomNum) {
    32                 System.out.println("太大了,请重试。");
    33             } else if (guessNum < randomNum) {
    34                 System.out.println("太小了,请重试。");
    35             } else {
    36                 System.out.println("恭喜你,猜中啦!");
    37                 break; // 如果猜中,不再重试
    38             }
    39         }
    40 
    41         System.out.println("游戏结束。");
    42     }
    43 
    44 }

    ArrayList集合

     1 package cn.itcast.day07.demo04;
     2 
     3 /*
     4 题目:
     5 定义一个数组,用来存储3个Person对象。
     6 
     7 数组有一个缺点:一旦创建,程序运行期间长度不可以发生改变。
     8  */
     9 public class Demo01Array {
    10 
    11     public static void main(String[] args) {
    12         // 首先创建一个长度为3的数组,里面用来存放Person类型的对象
    13         Person[] array = new Person[3];
    14 
    15         Person one = new Person("迪丽热巴", 18);
    16         Person two = new Person("古力娜扎", 28);
    17         Person three = new Person("玛尔扎哈", 38);
    18 
    19         // 将one当中的地址值赋值到数组的0号元素位置
    20         array[0] = one;
    21         array[1] = two;
    22         array[2] = three;
    23 
    24         System.out.println(array[0]); // 地址值
    25         System.out.println(array[1]); // 地址值
    26         System.out.println(array[2]); // 地址值
    27 
    28         System.out.println(array[1].getName()); // 古力娜扎
    29     }
    30 
    31 }
     1 package cn.itcast.day07.demo04;
     2 
     3 public class Person {
     4 
     5     private String name;
     6     private int age;
     7 
     8     public Person() {
     9     }
    10 
    11     public Person(String name, int age) {
    12         this.name = name;
    13         this.age = age;
    14     }
    15 
    16     public String getName() {
    17         return name;
    18     }
    19 
    20     public void setName(String name) {
    21         this.name = name;
    22     }
    23 
    24     public int getAge() {
    25         return age;
    26     }
    27 
    28     public void setAge(int age) {
    29         this.age = age;
    30     }
    31 }
     1 package cn.itcast.day07.demo04;
     2 
     3 import java.util.ArrayList;
     4 
     5 /*
     6 数组的长度不可以发生改变。
     7 但是ArrayList集合的长度是可以随意变化的。
     8 
     9 对于ArrayList来说,有一个尖括号<E>代表泛型。
    10 泛型:也就是装在集合当中的所有元素,全都是统一的什么类型。
    11 注意:泛型只能是引用类型,不能是基本类型。
    12 
    13 注意事项:
    14 对于ArrayList集合来说,直接打印得到的不是地址值,而是内容。
    15 如果内容是空,得到的是空的中括号:[]
    16  */
    17 public class Demo02ArrayList {
    18 
    19     public static void main(String[] args) {
    20         // 创建了一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
    21         // 备注:从JDK 1.7+开始,右侧的尖括号内部可以不写内容,但是<>本身还是要写的。
    22         ArrayList<String> list = new ArrayList<>();
    23         System.out.println(list); // []
    24 
    25         // 向集合当中添加一些数据,需要用到add方法。
    26         list.add("赵丽颖");
    27         System.out.println(list); // [赵丽颖]
    28 
    29         list.add("迪丽热巴");
    30         list.add("古力娜扎");
    31         list.add("玛尔扎哈");
    32         System.out.println(list); // [赵丽颖, 迪丽热巴, 古力娜扎, 玛尔扎哈]
    33 
    34 //        list.add(100); // 错误写法!因为创建的时候尖括号泛型已经说了是字符串,添加进去的元素就必须都是字符串才行
    35     }
    36 
    37 }
     1 package cn.itcast.day07.demo04;
     2 
     3 import java.util.ArrayList;
     4 
     5 /*
     6 ArrayList当中的常用方法有:
     7 
     8 public boolean add(E e):向集合当中添加元素,参数的类型和泛型一致。返回值代表添加是否成功。
     9 备注:对于ArrayList集合来说,add添加动作一定是成功的,所以返回值可用可不用。
    10 但是对于其他集合(今后学习)来说,add添加动作不一定成功。
    11 
    12 public E get(int index):从集合当中获取元素,参数是索引编号,返回值就是对应位置的元素。
    13 
    14 public E remove(int index):从集合当中删除元素,参数是索引编号,返回值就是被删除掉的元素。
    15 
    16 public int size():获取集合的尺寸长度,返回值是集合中包含的元素个数。
    17  */
    18 public class Demo03ArrayListMethod {
    19 
    20     public static void main(String[] args) {
    21         ArrayList<String> list = new ArrayList<>();
    22         System.out.println(list); // []
    23 
    24         // 向集合中添加元素:add
    25         boolean success = list.add("柳岩");
    26         System.out.println(list); // [柳岩]
    27         System.out.println("添加的动作是否成功:" + success); // true
    28 
    29         list.add("高圆圆");
    30         list.add("赵又廷");
    31         list.add("李小璐");
    32         list.add("贾乃亮");
    33         System.out.println(list); // [柳岩, 高圆圆, 赵又廷, 李小璐, 贾乃亮]
    34 
    35         // 从集合中获取元素:get。索引值从0开始
    36         String name = list.get(2);
    37         System.out.println("第2号索引位置:" + name); // 赵又廷
    38 
    39         // 从集合中删除元素:remove。索引值从0开始。
    40         String whoRemoved = list.remove(3);
    41         System.out.println("被删除的人是:" + whoRemoved); // 李小璐
    42         System.out.println(list); // [柳岩, 高圆圆, 赵又廷, 贾乃亮]
    43 
    44         // 获取集合的长度尺寸,也就是其中元素的个数
    45         int size = list.size();
    46         System.out.println("集合的长度是:" + size);
    47     }
    48 
    49 }
    package cn.itcast.day07.demo04;
    
    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));
            }
        }
    
    }
    package cn.itcast.day07.demo04;
    
    import java.util.ArrayList;
    
    /*
    如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”。
    
    基本类型    包装类(引用类型,包装类都位于java.lang包下)
    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<>();
    
            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);
        }
    
    }
     1 package cn.itcast.day07.demo05;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Random;
     5 
     6 /*
     7 题目:
     8 生成6个1~33之间的随机整数,添加到集合,并遍历集合。
     9 
    10 思路:
    11 1. 需要存储6个数字,创建一个集合,<Integer>
    12 2. 产生随机数,需要用到Random
    13 3. 用循环6次,来产生6个随机数字:for循环
    14 4. 循环内调用r.nextInt(int n),参数是33,0~32,整体+1才是1~33
    15 5. 把数字添加到集合中:add
    16 6. 遍历集合:for、size、get
    17  */
    18 public class Demo01ArrayListRandom {
    19 
    20     public static void main(String[] args) {
    21         ArrayList<Integer> list = new ArrayList<>();
    22         Random r = new Random();
    23         for (int i = 0; i < 6; i++) {
    24             int num = r.nextInt(33) + 1;
    25             list.add(num);
    26         }
    27         // 遍历集合
    28         for (int i = 0; i < list.size(); i++) {
    29             System.out.println(list.get(i));
    30         }
    31     }
    32 
    33 }
    package cn.itcast.day07.demo05;
    
    import java.util.ArrayList;
    
    /*
    题目:
    自定义4个学生对象,添加到集合,并遍历。
    
    思路:
    1. 自定义Student学生类,四个部分。
    2. 创建一个集合,用来存储学生对象。泛型:<Student>
    3. 根据类,创建4个学生对象。
    4. 将4个学生对象添加到集合中:add
    5. 遍历集合:for、size、get
     */
    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());
            }
        }
    
    }
    package cn.itcast.day07.demo05;
    
    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;
        }
    }
     1 package cn.itcast.day07.demo05;
     2 
     3 import java.util.ArrayList;
     4 
     5 /*
     6 题目:
     7 定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起集合,使用@分隔每个元素。
     8 格式参照 {元素@元素@元素}。
     9 
    10 System.out.println(list);       [10, 20, 30]
    11 printArrayList(list);           {10@20@30}
    12  */
    13 public class Demo03ArrayListPrint {
    14 
    15     public static void main(String[] args) {
    16         ArrayList<String> list = new ArrayList<>();
    17         list.add("张三丰");
    18         list.add("宋远桥");
    19         list.add("张无忌");
    20         list.add("张翠山");
    21         System.out.println(list); // [张三丰, 宋远桥, 张无忌, 张翠山]
    22 
    23         printArrayList(list);
    24     }
    25 
    26     /*
    27     定义方法的三要素
    28     返回值类型:只是进行打印而已,没有运算,没有结果;所以用void
    29     方法名称:printArrayList
    30     参数列表:ArrayList
    31      */
    32     public static void printArrayList(ArrayList<String> list) {
    33         // {10@20@30}
    34         System.out.print("{");
    35         for (int i = 0; i < list.size(); i++) {
    36             String name = list.get(i);
    37             if (i == list.size() - 1) {
    38                 System.out.println(name + "}");
    39             } else {
    40                 System.out.print(name + "@");
    41             }
    42         }
    43     }
    44 
    45 }
     1 package cn.itcast.day07.demo05;
     2 
     3 import java.util.ArrayList;
     4 import java.util.Random;
     5 
     6 /*
     7 题目:
     8 用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中。
     9 要求使用自定义的方法来实现筛选。
    10 
    11 分析:
    12 1. 需要创建一个大集合,用来存储int数字:<Integer>
    13 2. 随机数字就用Random nextInt
    14 3. 循环20次,把随机数字放入大集合:for循环、add方法
    15 4. 定义一个方法,用来进行筛选。
    16 筛选:根据大集合,筛选符合要求的元素,得到小集合。
    17 三要素
    18 返回值类型:ArrayList小集合(里面元素个数不确定)
    19 方法名称:getSmallList
    20 参数列表:ArrayList大集合(装着20个随机数字)
    21 5. 判断(if)是偶数:num % 2 == 0
    22 6. 如果是偶数,就放到小集合当中,否则不放。
    23  */
    24 public class Demo04ArrayListReturn {
    25 
    26     public static void main(String[] args) {
    27         ArrayList<Integer> bigList = new ArrayList<>();
    28         Random r = new Random();
    29         for (int i = 0; i < 20; i++) {
    30             int num = r.nextInt(100) + 1; // 1~100
    31             bigList.add(num);
    32         }
    33 
    34         ArrayList<Integer> smallList = getSmallList(bigList);
    35 
    36         System.out.println("偶数总共有多少个:" + smallList.size());
    37         for (int i = 0; i < smallList.size(); i++) {
    38             System.out.println(smallList.get(i));
    39         }
    40     }
    41 
    42     // 这个方法,接收大集合参数,返回小集合结果
    43     public static ArrayList<Integer> getSmallList(ArrayList<Integer> bigList) {
    44         // 创建一个小集合,用来装偶数结果
    45         ArrayList<Integer> smallList = new ArrayList<>();
    46         for (int i = 0; i < bigList.size(); i++) {
    47             int num = bigList.get(i);
    48             if (num % 2 == 0) {
    49                 smallList.add(num);
    50             }
    51         }
    52         return smallList;
    53     }
    54 
    55 }
  • 相关阅读:
    循环队列
    UVa10000_Longest Paths(最短路SPFA)
    最新jhost免费jsp云空间会员邀请码
    Vertica: 基于DBMS架构的列存储数据仓库
    java中接口的定义与实现
    【C++知识汇总】运营商 &amp; 运算符重载
    SVN与eclipse整合和利用、SVN与Apache综合
    Java单链逆转
    hdu1115(重力算法的多边形中心)
    高效C++规划
  • 原文地址:https://www.cnblogs.com/yaozhenhua/p/10324857.html
Copyright © 2020-2023  润新知