package Total_Demo.ArrayList.Big_test;
public class Student {
private String id;
private String name;
private int age;
private String home;
public Student() {
}
public Student(String id, String name, int age, String home) {
this.id = id;
this.name = name;
this.age = age;
this.home = home;
}
/**
* 获取
* @return id
*/
public String getId() {
return id;
}
/**
* 设置
* @param id
*/
public void setId(String id) {
this.id = id;
}
/**
* 获取
* @return name
*/
public String getName() {
return name;
}
/**
* 设置
* @param name
*/
public void setName(String name) {
this.name = name;
}
/**
* 获取
* @return age
*/
public int getAge() {
return age;
}
/**
* 设置
* @param age
*/
public void setAge(int age) {
this.age = age;
}
/**
* 获取
* @return home
*/
public String getHome() {
return home;
}
/**
* 设置
* @param home
*/
public void setHome(String home) {
this.home = home;
}
}
设置ArrayList来存储学生对象,通过方法来返回student对象索引,来进行增删改查
package Total_Demo.ArrayList.Big_test;
import java.util.ArrayList;
import java.util.Scanner;
public class StuTest {
public static void main(String[] args) {
ArrayList<Student> list=new ArrayList<>();
//loop是给break停止的循环
loop:while (true){
System.out.println("---------坤坤学院-------");
System.out.println("1.添加学生");
System.out.println("2.删除学生");
System.out.println("3.修改学生");
System.out.println("4.查询学生");
System.out.println("5.退出");
System.out.println("please into your choose number");
Scanner sc=new Scanner(System.in);
String choose = sc.next();
switch (choose){
case "1"-> addstudent(list);
case "2"->delstudent(list);
case "3"-> updatestudent(list);
case "4"-> querytudent(list);
case "5"-> {
System.out.println("5.退出");
break loop;
// System.exit(0); //停止虚拟机运行
}
default -> System.out.println("no number you have choose");
}
}
}
//添加学生
public static void addstudent(ArrayList<Student> list){
System.out.println("添加学生");
Scanner sc =new Scanner(System.in);
Student stu=new Student();
while (true){
System.out.println("id");
String id = sc.next();
boolean flag=content(list,id);
if (flag){
System.out.println("id存在,重新录入");
}else {
stu.setId(id);
System.out.println("添加成功");
break;
}
}
System.out.println("name");
String name=sc.next();
System.out.println("age");
int age = sc.nextInt();
System.out.println("home");
String home = sc.next();
stu.setName(name);
stu.setAge(age);
stu.setHome(home);
list.add(stu);
System.out.println("添加成功");
}
public static void delstudent(ArrayList<Student> list){
System.out.println("请写出要删除的id");
Scanner sc=new Scanner(System.in);
String id = sc.next();
//查询id在集合中的索引
int index = getIndex(list, id);
//对index进行判断
//大于等于0,表示存在,直接删除
//-1 直接表示不存在,结束方法,回到主菜单
if (index>=0){
list.remove(index);
System.out.println("id为"+id+"的学生删除成功");
}else {
System.out.println("id不存在");
}
}
public static void updatestudent(ArrayList<Student> list){
System.out.println("你要修改的id");
Scanner sc =new Scanner(System.in);
String id = sc.next();
//获得list student对象的索引
int sidindex=getIndex(list,id);
if (sidindex>=0){
System.out.println("你要修改后的name");
String name = sc.next();
list.get(sidindex).setName(name);
System.out.println("你要修改后的的age");
int age = sc.nextInt();
list.get(sidindex).setAge(age);
System.out.println("你要修改后的home");
String home=sc.next();
list.get(sidindex).setHome(home);
System.out.println("修改成功");
}else {
System.out.println("id 不存在");
}
}
public static void querytudent(ArrayList<Student> list){
System.out.println("查询学生");
if (list.size()==0){
System.out.println("无信息");
//结束方法
return;
}
//打印表头信息
System.out.println("id\t\t姓名\t年龄\t家庭地址");
for (int i = 0; i < list.size(); i++) {
Student stu=list.get(i);
System.out.println(stu.getId()+"\t\t"+stu.getName()+"\t"+stu.getAge()+"\t"+stu.getHome());
}
}
//判断id的唯一性
public static boolean content(ArrayList<Student> list,String id){
// for (int i = 0; i < list.size(); i++) {
// //id相同返回true
// if (list.get(i).getId().equals(id)){
// return true;
// }
// }
//
// return false;
return getIndex(list,id)>=0;
}
public static int getIndex(ArrayList<Student> list,String id){
//通过id获取索引的方法
for (int i = 0; i < list.size(); i++) {
//得到每一个学生对象
Student stu=list.get(i);
//得到其id
String sid=stu.getId();
//拿着集合中的学生id跟要删除的id进行比较
if (sid.equals(id)){
//如果一样返回索引
return i;
}
}
return -1;
}
}