常用API(String、 ArrayList)
什么是API
API文档下载地址:
http://www.oracle.com/technetwork/java/javase/downloads/index.html
1.String简单介绍
【补充】:为什么java 数据类型 String 是大写?
1.1 String类概述
1.2 String类创建对象的2种方式
1.3 String类常见面试题
1.3.1 String类常见面试题(面试题一):
1.3.2 String类常见面试题(面试题二):
把编译后的文件拖进来之后,可以看到反编译出来的.class文件:即,Java将 "a" + "b" + "c"编译成"abc"给到底层处理器。
1.4 String类常用API-字符串内容比较
1.5 String类常用API-遍历、替换、截取、分割操作
1.6 String类案例实战
2.ArrayList简单介绍
2.1 集合概述
2.2 【ArrayList集合】快速入门
2.3 ArrayList对泛型的支持
2.4 ArrayList常用API、遍历
2.5 ArrayList集合案例:遍历并删除元素
2.6 ArrayList集合案例:存储自定义类型的对象
2.7 ArrayList集合案例:元素搜索
package day07_demo.demo_02;
public class Student {
private String studyId;
private String name;
private int age;
private String className;
public Student() {
}
public Student(String studyId, String name, int age, String className) {
this.studyId = studyId;
this.name = name;
this.age = age;
this.className = className;
}
public String getStudyId() {
return studyId;
}
public void setStudyId(String studyId) {
this.studyId = studyId;
}
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;
}
public String getClassName() {
return className;
}
public void setClassName(String className) {
this.className = className;
}
}
package day07_demo.demo_02;
import java.util.ArrayList;
import java.util.Scanner;
/**
案例:学生信息系统:展示数据,并按照学号完成搜索
学生类信息(学号,姓名,性别,班级)
测试数据:
"20180302","叶孤城",23,"护理一班"
"20180303","东方不败",23,"推拿二班"
"20180304","西门吹雪",26,"中药学四班"
"20180305","梅超风",26,"神经科2班"
*/
public class ArrayListTest6 {
public static void main(String[] args) {
// 1、定义一个学生类,后期用于创建对象封装学生数据
// 2、定义一个集合对象用于装学生对象
ArrayList<Student> students = new ArrayList<>();
students.add(new Student("20180302","叶孤城",23,"护理一班"));
students.add(new Student("20180303","东方不败",23,"推拿二班"));
students.add(new Student( "20180304","西门吹雪",26,"中药学四班"));
students.add(new Student( "20180305","梅超风",26,"神经科2班"));
System.out.println("学号\t\t名称\t年龄\t\t班级");
// 3、遍历集合中的每个学生对象并展示其数据
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
System.out.println(s.getStudyId() +"\t\t" + s.getName()+"\t\t"
+ s.getAge() +"\t\t" + s.getClassName());
}
// 4、让用户不断的输入学号,可以搜索出该学生对象信息并展示出来(独立成方法)
Scanner sc = new Scanner(System.in);
while (true) {
System.out.println("请您输入要查询的学生的学号:");
String id = sc.next();
Student s = getStudentByStudyId(students, id);
// 判断学号是否存在
if(s == null){
System.out.println("查无此人!");
}else {
// 找到了该学生对象了,信息如下
System.out.println(s.getStudyId() +"\t\t" + s.getName()+"\t\t"
+ s.getAge() +"\t\t" + s.getClassName());
}
}
}
/**
根据学号,去集合中找出学生对象并返回。
* @param students
* @param studyId
* @return
*/
public static Student getStudentByStudyId(ArrayList<Student> students, String studyId){
for (int i = 0; i < students.size(); i++) {
Student s = students.get(i);
if(s.getStudyId().equals(studyId)){
return s;
}
}
return null; // 查无此学号!
}
}
标签:day08,Java,String,students,ArrayList,studyId,Student,public
From: https://www.cnblogs.com/SuperLee017/p/18137332