Java:集合,ArrayList,請(qǐng)選擇你要執(zhí)行的功能1.查詢2.添加3.修改4.刪除,增刪改查

實(shí)現(xiàn)有功能“1.查詢2.添加3.修改4.刪除”的和ArrayList有關(guān)的程序
package a;
import java.util.ArrayList;
import java.util.Scanner;
public class lizi {
public static void main(String[] args) {
ArrayList<student> arr = new ArrayList<student>();
arr.add(new student(1, "詩(shī)名", '男', 95));
arr.add(new student(2, "書名", '男', 92));
arr.add(new student(3, "畫名", '男', 90));
arr.add(new student(4, "唱名", '男', 111));
arr.add(new student(5, "帥名", '男', 134));
while (true) {
System.out.println("請(qǐng)選擇你要執(zhí)行的功能1.查詢2.添加3.修改4.刪除");
Scanner s = new Scanner(System.in);
int num = s.nextInt();
if (num == 1) {
// 查詢:
System.out.println("執(zhí)行查詢功能");
for (student i : arr) {
System.out.println(i);
}
} else if (num == 2) {
// 添加:
System.out.println("已經(jīng)執(zhí)行添加“3, “張三”, '男', 88”功能,不信可執(zhí)行查詢功能");
arr.add(new student(3, "張三", '男', 88));
} else if (num == 3) {
// 修改:
System.out.println("執(zhí)行修改功能");
System.out.println("請(qǐng)輸入你要修改的學(xué)生的編號(hào)");
int bianhao = s.nextInt();
for (int i = 0; i < arr.size(); i++) {
if (bianhao == arr.get(i).bianhao) {
System.out.println("你要修改的成績(jī)?yōu)槎嗌?");
double d = s.nextDouble();
arr.get(i).ChengJi = d;
}
}
} else if (num == 4) {
// 刪除:
System.out.println("執(zhí)行刪除功能");
System.out.println("請(qǐng)輸入你要?jiǎng)h除的學(xué)生的編號(hào)");
int bianhao = s.nextInt();
for (int i = 0; i < arr.size(); i++) {
if (bianhao == arr.get(i).bianhao) {
arr.remove(arr.get(i));
}
}
System.out.println("刪除完畢");
}
}
}
}
class student {
int bianhao;
double ChengJi;
String name;
char sex;
public student(int bianhao, String name, char sex, double ChengJi) {
this.bianhao = bianhao;
this.name = name;
this.sex = sex;
this.ChengJi = ChengJi;
}
@Override
public String toString() {
return "編號(hào):" + bianhao + ",\t名字:" + name + ",\t性別:" + sex + ",\t成績(jī):"
+ ChengJi;
}
}







