API(Application programming Interface 应用程序编程接口)

  • Java 写好的程序(功能),咱们可以直接调用
  • Oracle 也为 Java 提供了这些代码功能的相应 API文档(官网有)

1.String类概述

  • java.lang.String 类代表字符串,String 类定义的变量可以用于指向字符串对象,然后操作该字符串。
  • Java 程序中的所有字符串文字(例如 "abc")都为此类的对象
  • 【String 其实常被称为不可变字符串类型,它的对象在创建后不能被更改】

1.1、字符串对象存在哪里?

以 " " 方式给出的字符串,在字符串常量池中储存。

2.String类的常用方法-字符串内容比较

【字符串的内容比较不适合用 “==” 比较】

(因为字符串为对象,== 比较的为对象地址)

  • 字符串的内容比较

3.String类的常用方法

4.String类案例实战

  • String 类开发验证码功能

【需求】:随机产生一个 5 位的验证码,每位可能是数字、大写字母、小写字母

public class Test {
    public static void main(String[] args) {
        String data = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        Random random = new Random();
        String code = "";
        for (int i = 0; i < 5; i++) {
            code += data.charAt(random.nextInt(data.length()));
        }
        System.out.println(code);
    }
}
  • 模拟用户登录功能

【需求】:模拟用户登录功能,最多三次机会并提示机会次数

public class LoginDemo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        String setUser = "B22043226";
        String setPassword = "010220";
        for (int i = 2; i >= 0; i--) {
            System.out.println("请输入账号:");
            String user = input.next();
            System.out.println("请输入密码:");
            String password = input.next();
            if(!(user.equals(setUser) && password.equals(setPassword))){
                System.out.println("账号或密码错误,请重新登录:(您还有"+i+"次机会)");
                continue;
            }
            System.out.println("登录成功!");
            break;
        }
    }
}
  • 手机号码屏蔽

【需求】:键盘录入手机号,将中间四位号码屏蔽,效果为:156**9567

public class IgnorePhoneDemo {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        System.out.println("请输入手机号:");
        String phone = input.next();
        String phoneFore = phone.substring(0,3);
        String phoneEndFore = phone.substring(phone.length()-4);
        System.out.println("屏蔽后手机号显示为:"+phoneFore+"****"+phoneEndFore);
    }
}

5.String类创建对象的两种方式

  • 方式一:直接使用 " " 定义

    【String name = "马浩楠"】

  • 方式二:通过 String 类的构造器创建对象

  • 区别面试常考
    • 以 " " 方式给出的字符串对象,在字符串常量池中存储,而且相同内容只会在其中存储一份。
    • 通过构造器 new 对象,每 new 一次都会产生一个新对象,放在堆内存中

6.String类笔试题

  • 下列代码的运行结果是?

【Java 存在编译优化机制,程序在编译时:"a" + "b" + "c" 会转为 "abc"】

7.集合概述

【集合是与数组类似,也是一种容器,用于装数据的】

  • 数组定义完成并启动后,类型确定、长度固定
  • 问题:在个数不能确定,且要进行增删数据操作的时候,数组是不太适合的

【集合的特点】

  • 集合的大小不固定,启动后可以动态变化,类型也可以选择不固定
  • 集合非常适合做元素不确定,且要进行增删操作的业务场景
  • 集合还提供了许多丰富、好用的功能,而数组的功能很单一

8.ArrayList集合快速入门

  • ArrayList 是集合中的一种,它支持索引

  • ArrayList 集合的添加元素方法

9.ArrayList对泛型的支持

  • 泛型概述

    ArrayList:其实就是一个泛型类,可以在编译阶段约束集合对象只能操作某种数据类型

【例如】:

​ ArrayList:此集合只能操作字符串类型的元素

​ ArrayList:此集合只能操作整数类型的元素

【注意】:泛型只能支持引用数据类型,不支持基本数据类型

10.ArrayList 常用方法、遍历

11.ArrayList集合案例

11.1、遍历并删除元素值

【需求】:

  • 某个班级的考试在系统上进行,成绩大致位:98,77,66,89,79,50,100
  • 现在需要先吧成绩低于80分以下的数据去掉
public class Demo1 {
    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);
        ArrayList<Integer> scores = new ArrayList<>();
        scores.add(98);
        scores.add(77);
        scores.add(66);
        scores.add(89);
        scores.add(79);
        scores.add(50);
        scores.add(100);
        for (int i = 0; i < scores.size(); i++) {
            if(scores.get(i)<80){
                scores.remove(i);
                i--;
            }
        }
        System.out.println(scores);
    }
}

11.2、存储自定义类型的对象

【需求】:

  • 某影院系统需要在后台存储上述三部电影,然后依次展示出来

电影类:

public class Demo2_Film {
    private String name;
    private double score;
    private String actor;

    public Demo2_Film(String name, double score, String actor) {
        this.name = name;
        this.score = score;
        this.actor = actor;
    }

    public Demo2_Film() {
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }

    public String getActor() {
        return actor;
    }

    public void setActor(String actor) {
        this.actor = actor;
    }

    @Override
    public String toString() {
        return "Demo2_Film{" +
                "name='" + name + '\'' +
                ", score=" + score +
                ", actor='" + actor + '\'' +
                '}';
    }
}

测试类:

public class Demo2 {
    public static void main(String[] args) {
        ArrayList<Demo2_Film> films = new ArrayList<>();
        Demo2_Film fileOne = new Demo2_Film("肖申克的救赎",9.7,"罗宾斯");
        Demo2_Film fileTwo = new Demo2_Film("红海行动",8.5,"嗯哈");
        Demo2_Film fileThree = new Demo2_Film("巨齿鲨",7.9,"嗯");
        films.add(fileOne);
        films.add(fileTwo);
        films.add(fileThree);
        for (int i = 0; i < films.size(); i++) {
            System.out.println(films.get(i).toString());
        }
    }
}

results matching ""

    No results matching ""