Java中常用 API(第一部分)

1.API

  1. API 概述:
    • API(Application Programming Interface),应用程序编程接口。Java API是一本程序员的 字典 ,是JDK中提供给我们使用的类的说明文档。这些类将底层的代码实现封装了起来,我们不需要关心这些类是如何实现的,只需要学习这些类如何使用即可。所以我们可以通过查询API的方式,来学习Java提供的类,并得知如何使用它们。
    • 所谓的API :就是里面有好多类,好多方法.为我们量身定制的字典.
  2. API 的使用步骤:
    1. 打开帮助文档。
    2. 点击显示,找到索引,看到输入框。
    3. 你要找谁?在输入框里输入,然后回车。
    4. 看包。java.lang下的类不需要导包,其他需要。
    5. 看类的解释和说明。
    6. 学习构造方法
    7. 使用成员方法

2.Scanner类

  1. Scanner类的概述:

    Scanner类的功能:可以实现键盘输入数据,到程序当中。

  2. 引用类型的使用步骤

    1. 导包
      import 包路径.类名称;
      如果需要使用的目标类,和当前类位于同一个包下,则可以省略导包语句不写
      只有java.lang包下的内容不需要导包,其他的包都需要import语句。

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

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

  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);
        }
    
    }
    
    
  4. 练习

    1. 键盘输入三个int数字,然后求出其中的最大值。

      分析:

      1. 既然是键盘输入,肯定需要用到Scanner

      2. Scanner三个步骤:导包、创建、使用nextInt()方法

      3. 既然是三个数字,那么调用三次nextInt()方法,得到三个int变量

      4. 无法同时判断三个数字谁最大,应该转换成为两个步骤:
        4.1 首先判断前两个当中谁最大,拿到前两个的最大值
        4.2 拿着前两个中的最大值,再和第三个数字比较,得到三个数字当中的最大值

      5. 打印最终结果

      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 = temp > c ? temp : c;
              System.out.println("最大值是:" + max);
          }
      
      }
      
      
    2. 键盘输入两个int数字,并且求出和值。

      分析 :

      1. 既然需要键盘输入,那么就用Scanner

      2. Scanner的三个步骤:导包、创建、使用

      3. 需要的是两个数字,所以要调用两次nextInt方法

      4. 得到了两个数字,就需要加在一起。

      5. 将结果打印输出。

      public class Demo02ScannerSum {
      
          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();
      
              int result = a + b;
              System.out.println("结果是:" + result);
          }
      
      }
      
      
  5. 补充 . 匿名对象

    1. 标准创建对象格式

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

      匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
      格式: new 类名称();
      
    3. 注意事项:

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

    4. 代码 :

      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
          }
      
      }
      
      
    5. 匿名对象可以作为方法的参数和返回值

      1. 作为方法的参数 \ 作为返回值

        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() {
        //        Scanner sc = new Scanner(System.in);
        //        return sc;
                return new Scanner(System.in);
            }
        
        }
        
        

3.Random类

  1. Random类的概述:

    Random类用来生成随机数字.

  2. Random 类的使用步骤

    1. 导包
      import java.util.Random;
    2. 创建
      Random r = new Random(); // 小括号当中留空即可
    3. 使用
      获取一个随机的int数字(范围是int所有范围,有正负两种):int num = r.nextInt()
      获取一个随机的int数字(参数代表了范围,左闭右开区间):int num = r.nextInt(3)
      实际上代表的含义是:[0,3),也就是0~2
    public class Demo01Random {
    
        public static void main(String[] args) {
            Random r = new Random();
    		//获取一个随机的int数字(范围是int所有范围,有正负两种):int num = r.nextInt()
            int num = r.nextInt();
            System.out.println("随机数是:" + num);
        }
    
    }
    
  3. 生成指定范围的随机数

    获取一个随机的int数字(参数代表了范围,左闭右开区间):int num = r.nextInt(3)   范围 就是 [0,2)
    调用 r.nextInt(给定参数);方法
    
  4. 练习

    1. 根据给定 int类型的值 n , 生成从[1 , x) 的随机数

      public class DemoRandom {
          public static void main(String[] args) {
              generationRanom(100);
          }
      
          private static void generationRanom(int x) {
              for (int i = 0; i < 100; i++) {
                  int nextInt = new Random().nextInt(x) + 1;  // 生成 : [1 , x) 的随机数
                  System.out.println(nextInt);
              }
          }
      
      
      
    2. 用代码模拟猜数字的小游戏。数字范围 [1 ,100)

      思路:

      1. 首先需要产生一个随机数字,并且一旦产生不再变化。用Random的nextInt方法

      2. 需要键盘输入,所以用到了Scanner

      3. 获取键盘输入的数字,用Scanner当中的nextInt方法

      4. 已经得到了两个数字,判断(if)一下:
        如果太大了,提示太大,并且重试;
        如果太小了,提示太小,并且重试;
        如果猜中了,游戏结束。

      5. 重试就是再来一次,循环次数不确定,用while(true)。

      public class GuessGame {
              public static void main(String[] args) {
                  Random r = new Random();
                  int randomNum = r.nextInt(100) + 1; // [1,100]
                  Scanner sc = new Scanner(System.in);
      
                  while (true) {
                      System.out.println("请输入你猜测的数字:");
                      int guessNum = sc.nextInt(); // 键盘输入猜测的数字
      
                      if (guessNum > randomNum) {
                          System.out.println("太大了,请重试。");
                      } else if (guessNum < randomNum) {
                          System.out.println("太小了,请重试。");
                      } else {
                          System.out.println("恭喜你,猜中啦!");
                          break; // 如果猜中,不再重试
                      }
                  }
      
                  System.out.println("游戏结束。");
              }
      
          }
      
    3. 给定验证码的长度 ,随机生成一个 x 长度的验证码 ;

      public class GenerateRandomCode {
          public static void main(String[] args) {
      
              System.out.println(GenerateRandomCode.GenerateVerificationCode(8));
          }
          //产生验证码的逻辑 生成数字 或者 生成 字母
          public static String GenerateVerificationCode(int n){
      		//定义一个接收生成的字符串
              String val = "";
      
              Random random = new Random();
              for (int i = 0; i < n; i++) {
              	//随机产生一个 0 | 1 的数字 ,如果是 0 则产生 数字 ,如果是 1 则产生 字符 .
                  String s = random.nextInt(2) % 2 == 0 ? "number" : "char";
                  // 产生数字和字母交替的目的,产生字母的情况
                  if("char".equalsIgnoreCase(s)){
                      //生成字母,
                      int i1 = random.nextInt(2) % 2 == 0 ? 65 : 97;
                      val += (char) (i1 + random.nextInt(26));
                  }else if ("number".equalsIgnoreCase(s)){
                      // 产生数字  数字转String  ---> String.valueOf(...)
                      val += String.valueOf(random.nextInt(10));
                  }
              }
      
              return val;
      
          }
      
      
      

4.ArrayList类

  1. 知识回顾 :

    • 定义一个数组,用来存储3个Person对象。
    public class Demo01Array {
    
        public static void main(String[] args) {
            // 首先创建一个长度为3的数组,里面用来存放Person类型的对象
            Person[] array = new Person[3];
    
            Person one = new Person("迪丽热巴", 18);
            Person two = new Person("古力娜扎", 28);
            Person three = new Person("玛尔扎哈", 38);
    
            // 将one当中的地址值赋值到数组的0号元素位置
            array[0] = one;
            array[1] = two;
            array[2] = three;
    
            System.out.println(array[0]); // 地址值
            System.out.println(array[1]); // 地址值
            System.out.println(array[2]); // 地址值
    
            System.out.println(array[1].getName()); // 古力娜扎
        }
    
    }
    
  2. ArrayList集合

    几点声明:

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

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

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

    public class Demo02ArrayList {
    
        public static void main(String[] args) {
            // 创建了一个ArrayList集合,集合的名称是list,里面装的全都是String字符串类型的数据
            // 备注:从JDK 1.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); // 错误写法!因为创建的时候尖括号泛型已经说了是字符串,添加进去的元素就必须都是字符串才行
        }
    
    }
    
    
  3. 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); // []
    
            // 向集合中添加元素: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); // [柳岩, 高圆圆, 赵又廷, 李小璐, 贾乃亮]
    
            // 从集合中获取元素:get。索引值从0开始
            String name = list.get(2);
            System.out.println("第2号索引位置:" + 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. 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));
            }
        }
    
    }
    
    
  5. ArrayList 集合当中存放基本数据类型的解决办法

    1. 如果希望向集合ArrayList当中存储基本类型数据,必须使用基本类型对应的“包装类”。

    2. 自动装箱:基本类型 --> 包装类型

    3. 自动拆箱:包装类型 --> 基本类型 [从JDK 1.5+开始,支持自动装箱、自动拆箱。]

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

    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);
          }
      
      }
      
      
  6. 练习

    1. 随机生成 6 个 1 - 33 的整数 ,添加到集合,并遍历集合.

      public class RandomlyGenerated {
          public static void main(String[] args) {
              //定义一个 存放 Integer的集合,用来存放产生的随机数
              ArrayList<Integer> list = new ArrayList<>();
              Random r = new Random();
              for (int i = 0; i < 6; i++) {
                  //生成 6 个 随机数
                  int nextInt = r.nextInt(33) + 1;
                  //将产生的随机数放到集合内
                  list.add(nextInt);
              }
              //查看一下集合内的内容
              System.out.println(list);
              //集合遍历
              for (int i = 0; i < list.size(); i++) {
                  Integer integer = list.get(i);
                  System.out.println(integer);
              }
          }
      }
      
    2. ArrayList 集合存放自定义对象,并遍历集合.

      1. 创建一个学生类 :

         public class Student {
            private String naem ;
            private int age ;
            private boolean isGirl;
        
            public Student() {
            }
        
            public Student(String naem, int age, boolean isGirl) {
                this.naem = naem;
                this.age = age;
                this.isGirl = isGirl;
            }
        
            public String getNaem() {
                return naem;
            }
        
            public void setNaem(String naem) {
                this.naem = naem;
            }
        
            public int getAge() {
                return age;
            }
        
            public void setAge(int age) {
                this.age = age;
            }
        
            public boolean isGirl() {
                return isGirl;
            }
        
            public void setGirl(boolean girl) {
                isGirl = girl;
            }
        
        
      2. 创建一个测试类

        public class StudentTest {
            public static void main(String[] args) {
            //创建对象
                Student s1 = new Student("周杰伦", 30, false);
                Student s2 = new Student("石原里美", 19, true);
                Student s3 = new Student("雪梨", 20, true);
                Student s4 = new Student("小仓柚子", 17, true);
        	//创建容器
                ArrayList<Student> list = new ArrayList<>();
                //存对象
                list.add(s1);
                list.add(s2);s
                list.add(s3);
                list.add(s4);
                System.out.println(list.size());
                //遍历集合
                for (int i = 0; i < list.size(); i++) {
                    Student stu = list.get(i);
                    System.out.println("姓名 : " + stu.getNaem() + ",年龄  :"  + stu.getAge() + ", 是不适合暖床丫 :" + stu.isGirl());
                }
            }
        }
        
    3. 定义以指定格式打印集合的方法(ArrayList类型作为参数),使用{}扩起集合,使用@分隔每个元素。格式参照 {元素 @元素@元素}

      1. 分析:需要以集合作为传入的参数,得到集合,遍历元素,拼接成指定格式;

      2. ==== > 考察的是集合可以作为传入参数

        public class SpecifyFormatOutput {
            public static void main(String[] args) {
                ArrayList<String> list = new ArrayList<>();
                list.add("张三丰");
                list.add("张无忌");
                list.add("周芷若");
                list.add("谢逊");
                list.add("张三丰");
        
                letFormate(list);
            }
            public static void letFormate(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 + "}");
                    }
                }
            }
        }
        
    4. 题目描述 : 用一个大集合存入20个随机数字,然后筛选其中的偶数元素,放到小集合当中。
      要求使用自定义的方法来实现筛选。要求使用自定义的方法来实现筛选。

      1. 分析:

        1. 需要创建一个大集合,用来存储int数字:
        2. 随机数字就用Random nextInt
        3. 循环20次,把随机数字放入大集合:for循环、add方法
        4. 定义一个方法,用来进行筛选。
          筛选:根据大集合,筛选符合要求的元素,得到小集合。

        三要素
        返回值类型:ArrayList小集合(里面元素个数不确定)
        方法名称:selectOld
        参数列表:ArrayList大集合(装着30个随机数字)

        1. 判断(if)是偶数:num % 2 == 0
        2. 如果是偶数,就放到小集合当中,否则不放。
        public class SelectNum {
            public static void main(String[] args) {
                //产生30个随机数,放到大集合里
                Random r = new Random();
                ArrayList<Integer> big = new ArrayList<>();
                for (int i = 0; i < 20; i++) {
                    int nextInt = r.nextInt(100) + 1; // 1 - 100 之间的整数
                    big.add(nextInt);
                }
                //查看大集合里存放的随机数
                System.out.println(big);
        
                //调用筛选方法,返回的小集合
                System.out.println(selectOld(big));
        
            }
        
            public  static ArrayList<Integer>  selectOld(ArrayList<Integer> big) {
                //创建一个小集合,用来存放筛选过后的数字
                ArrayList<Integer> small = new ArrayList<>();
                //遍历大集合,筛选条件, 添加元素
                for (int i = 0; i < big.size(); i++) {
                    Integer sums = big.get(i);
                    if(sums % 2  == 0){
                        // 像小集合中存放
                        small.add(sums);
                    }
                }
                return small;
            }
        
        }
        
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值