java study(常用的API第一部分)

这篇博客详细介绍了Java中Scanner类的API使用,包括创建和使用Scanner对象来读取系统输入。接着讲解了匿名对象的概念和作为方法参数的用法。在Random类部分,博主阐述了生成随机数的方法。ArrayList集合的介绍涵盖了其基本使用、添加和删除元素,以及存储基本数据类型的技巧。此外,还讨论了字符串的特点、构造方法和常用方法,如比较、获取、截取和转换。最后,介绍了静态static关键字的作用及其在成员变量和方法中的应用,以及Arrays和Math类的一些实用方法。
摘要由CSDN通过智能技术生成

文章目录

第一节 Scanner类

1、API

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

2、API使用步骤

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

3、Scanne类

3.1什么是Scanner类

      一个可以解析基本类型和字符串的简单文本扫描器。

import java.util.Scanner;

public class Scanner1 {
   
    public static void main(String[] args) {
   
        Scanner sc = new Scanner(System.in);
        int i = sc.nextInt();
        System.out.println(i);
    }
}

System.in 系统输入指的是通过键盘录入数据。

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

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

    import java.util.Scanner;

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

    Scanner sc = new Scanner(System.in);

  • 3.使用
    对象名.成员方法()
    int i = sc.nextInt();//获得整数
    String str = sc.next();//获得字符串

  • 注意:键盘输入的都是字符串,只是相应的next方法将其转为对应的数据类型

import java.util.Scanner;

public class MAX {
   
    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 tem = a > b ? a : b;
        int max = tem > c ? tem : c;
        System.out.println(max);
    }
}

第二节 匿名对象

1、匿名对象的创建

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

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

2、匿名对象作为方法的参数

import java.util.Scanner;

public class Anonmous {
   
    public static void main(String[] args) {
   
        //普通使用方式
        Scanner sc = new Scanner(System.in);
        int num = sc.nextInt();

        //匿名对象使用方式
        int a = new Scanner(System.in).nextInt();

        //使用一般方法传入参数
        Scanner sc1 = new Scanner(System.in);
        methonParam(sc1);
        //使用匿名对象来进行传参
        methonParam(new Scanner(System.in));
    }
    public static void methonParam(Scanner sc){
   
        int nem = sc.nextInt();
    }

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

}

第三节 Random概述和基本使用

1、Random使用

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

生成的是整个int范围的随机数

public class RandomInt {
   
    public static void main(String[] args) {
   
        Random r = new Random();
        System.out.println(r.nextInt());
    }
}

2、生成指定范围内的随机数

       r.nextInt(n) n:代表了范围,左闭右开 从0开始
      r.nextInt(3) 表示[0,3)

3、生成1-n之间的随机数

    思路:

  • 1.定义一个int变量n,随意赋值
  • 2.要使用Random:三个步骤,导包、创建、使用
  • 3.如果要写10,那么就是0~ 9,然而想要的是1~10,可以发现:整体加1即可
  • 4.打印随机数
import java.util.Random;
public class RandomInt {
   
    public static void main(String[] args) {
   
        Random r = new Random();
        System.out.println(r.nextInt());
        int n = 5;
        for (int i = 0; i < 10; i++) {
   
            System.out.println(r.nextInt(n)+1);
        }

    }
}

第四节 ArrayLIst集合

1、定义一个数组,用来存储三个Person对象。

    数组有一个缺点:一旦创建,程序运行期间长度不可以发生改变。

    

public class Demo01Array {
   
    public static void main(String[] args) {
   
        Person[] array = new Person[3];
        Person one = new Person("alex1",16);
        Person two = new Person("alex2",17);
        Person thress = new Person("alex3",18);
        //将one当中的地址值赋值到数组的0号元素位置
        array[0]=one;
        array[1]=two;
        array[2]=thress;
        System.out.println(array[0]);
        System.out.println(array[1]);
        System.out.println(array[2]);

        System.out.println(array[0].getName());
    }
}
public class Person {
   
 
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值