常用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; // 查無此學號!
}
}