常用API第一部分

1.1 API概述

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

1.2 API使用步骤

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

1.2 Scanner类

Scanner类的功能,可以输入数据到程序当中
步骤:

  • 1.导包:import 包路径.类名称;只有java.lang包下的类容不需要导包
  • 2.创建:类名称 对象名 = new 类名称();
  • 3.使用:对象名.成员方法名()
    -获取键盘输入的一个int数字:int sum = sc.nextInt();
    -获取键盘输入的一个字符串:String str = sc.next();
/**
*练习	从三个值里选择一个最大值
*/
public class Text2 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int a = sc.nextInt();
        int b = sc.nextInt();
        int c = sc.nextInt();

        int temp = a > b ? a :b;
        int max = temp > c ? temp : c;
        System.out.println("最大值为" + max);
    }
}

1.3 匿名对象

匿名对象就是只有右边的对象,没有左边的名字和赋值运算符。
注意事项:匿名对象只能使用唯一一次,下次在想使用不得不在创建一个对象。
使用建议:如果确定有一个对象只需要使用唯一一次,就可以使用匿名对象。

public class Anonymous {
    public static void main(String[] args) {
        Person per = new Person();
        per.name = "高圆圆";
        per.shouName();//我叫高圆圆

        //匿名对象
        new Person().name = "王权";
        new Person().shouName();//我叫null
    }
}

class Person{
    String name;

    public void shouName(){
        System.out.println("我叫" + name);
    }
}

1.4 Random

  • 1.导包:import java.util.Random;
  • 2.创建:Random r = new Random;
  • 3.使用:获取一个随机的数字(范围是int的所有范围包括负数)int num = r.nextInt(),如果括号里有值则代表取[0~该值),例如5的话则范围为,0,1,2,3,4,不包括5。

猜字小游戏:

public class RandomGame {
    public static void main(String[] args) {
        Random r = new Random();
        int randomNum = r.nextInt(1000000) + 1;
        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;
            }
        }
    }
}

1.5ArrayList集合

*********java.util.ArrayList是大小可变的数组的实现,存储在内的数据称为元素。此类提供一些方法来操作内部存储的元素。ArrayList中可以不断添加元素,其大小也自动增长。

public class DemoArrayList {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<String>();
        System.out.println(list);

        list.add("赵丽颖");
        list.add("迪丽热巴");
        list.add("古力娜扎");
        list.add("马儿扎哈");
        System.out.println(list);
    }
}

1.6ArrayList集合当中的常用方法:

E代表泛型,泛型只能是引用类型,不能是基本类型(int之类的)。

  • public boolean add(E e):向集合当中添加元素,参数的类型和泛型保持一致。返回值代表添加是否成功(以后学习其他集合时用得着这个返回值)
  • public E get(int index):从集合中获取元素,参数是索引编号,返回值就是对应位置的元素。
  • public E remove(int index):从集合中删除元素,参数时索引编号,返回值就是被删掉的元素。
  • public int size():获取集合的尺寸长度,返回值是集合中包含的元素个数。
public class DemoArrayListTest {
    public static void main(String[] args) {
        ArrayList<String> list = new ArrayList<>();
        System.out.println(list);

        //向集合中添加元素
        boolean success = list.add("柳岩");
        System.out.println("添加是否成功" + success);
        list.add("柳岩");
        list.add("李小璐");
        list.add("刘亦菲");
        list.add("小龙女");
        System.out.println("现在集合里有这几个人" + list);

        //向集合中获取元素:get
        String name = list.get(1);
        System.out.println("第一个人是:" + list.get(1));

        //从集合中删除元素,remove
        String whoRemoved = list.remove(1);
        System.out.println("被删除的人是" + list.get(1));

        //获取集合的长度
        int size = list.size();
        System.out.println("现在集合里有" + size + "个人");

    }
}

遍历的集合:代码如下

public class ArrayListEach {
    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));
        }
    }
}

1.7 集合存储基本数据类型

  • 如果需要在集合中存储基本数据类型,必须使用基本数据类型对应的"包装类"。
    基本类型–>包装类(引用类型、包装类都位于java.lang包下)
byte Byte
short Short
int Integer
long Long
float Float
double Double
char Character
boolean Boolean
public class DemoArrayListBasic {
    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(123);
        list.add(365);
        list.add(10);
        list.add(98);
        System.out.println(list);
        System.out.println(list.get(2));
    }
}

1.8 String

字符串的特点:

  1. 字符串的内容永不可变。
  2. 正因为字符串不可改变,所以字符串是可以共享使用的。
  3. 字符串效果上相当于是char[]字符数组,但是底层原理是byte[]字节数组
    创建字符串的3+1种常见方式
    三种构造方法
    1.public String();
    2.public String(char[] array);根据字符数组的内容,来创建相应的字符串
    3.public String(byte[] array);根据字节数组的内容,来创建相应的字符串
public class StringText {
    public static void main(String[] args) {
        String str1 = new String();
        System.out.println("第一个字符串" + str1);

        char[] charArray = {'A','B','C'};
        String str2 = new String(charArray);
        System.out.println("第二个字符串" + str2);

        byte[] byteArray = {97,98,99};
        String  str3 = new String(byteArray);
        System.out.println("第三个字符串" + str3);
    }
}
字符串常量池:程序当中直接写上双引号字符串,就在字符串常量池中,在堆当中
对于基本类型来说, ==是进行数值的比较
对于引用类型来说, ==是进行地址值的比较

如果一定要对引用类型进行数值或者说内容的比较这时需要用到:

  • equals(区分大小写)
  • equalsIgnoreCase(不区分大小写)
    格式:A.equals(B);结果为true或false;
    例如System.out.println(str1.equals(str4));
    如果比较双方一个常量一个变量,推荐把常量字符串写在前面。把引用类型写后面
    例如:推荐**“abc".equals(str)**,不推荐str.equals(“abc”)

猜你喜欢

转载自blog.csdn.net/qq_28168643/article/details/88078072