gpt4 book ai didi

java - 如何在Arraylist java中通过ID号搜索并显示对象?

转载 作者:行者123 更新时间:2023-11-30 05:19:11 25 4
gpt4 key购买 nike

我的程序确实需要帮助。我是 Arraylist 新手,不知道如何通过 ID 号搜索和显示对象。

所以程序有4个类:Main、Person(父类)Student和Employee(两者都是Person的子类)

现在我的主类有一个菜单,可以执行以下操作:

  1. 添加学生(每个学生的 ID 必须是唯一的)
  2. 添加员工(每个员工的 ID 必须是唯一的)
  3. 搜索学生(按学生 ID 然后显示)
  4. 搜索员工(按员工编号然后显示)
  5. 显示全部(显示所有螺柱和员工)
  6. 退出

我之前已经通过数组搜索和显示完成了此操作,但现在我们必须使用 Arraylist,它更好,但我对它很陌生。

非常感谢任何帮助和建议。谢谢!

这是我的代码:

主类:

import java.util.ArrayList;
import java.util.Scanner;

public class Main {

static Scanner sc = new Scanner(System.in);

public static void main(String[] args){
ArrayList<Student> stud = new ArrayList<Student>();
ArrayList<Employee> emp = new ArrayList<Employee>();
while (true) {
int select;
System.out.println("Menu");
System.out.println("1. Add Student");
System.out.println("2. Add Employee");
System.out.println("3. Search Student");
System.out.println("4. Search Employee");
System.out.println("5. Display All");
System.out.println("6. Quit");
select = sc.nextInt();

switch (select) {
case 1:
addStud(stud);
break;
case 2:
addEmp(emp);
break;
case 3:
srchStud();
break;
case 4:
srchEmp();
case 5:
displayAll(stud, emp);
break;
case 6:
return;
default:
System.out.println("Invalid Option");
}
}
}

public static void addStud(ArrayList<Student> stud) {

String name, address, course;
int age, cNum, studNum, year;

int addMore;
System.out.println("ADD STUDENT");
do {
System.out.println("Student No: ");
studNum = sc.nextInt();
sc.nextLine();
for(int x = 0; x < stud.size(); x++){ // Please help fix, this accepts another ID if already existing to prevent duplicate
if(studNum == stud[x].getStudNum()) { // this code works with array of object but not on arraylist,
System.out.println("The Student ID: " +studNum+ " already exist.\nEnter New Student ID: ");
studNum = sc.nextInt();
sc.nextLine();
x = -1;
}
}
System.out.println("Name: ");
name = sc.nextLine();
System.out.println("Age: ");
age = sc.nextInt();
sc.nextLine();
System.out.println("Address: ");
address = sc.nextLine();
System.out.println("Contact No: ");
cNum = sc.nextInt();
sc.nextLine();
System.out.println("Course: ");
course = sc.nextLine();
System.out.println("Year: ");
year = sc.nextInt();
sc.nextLine();

stud.add(new Student(name, address, age, cNum, studNum, year, course));

System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);

}

public static void addEmp(ArrayList<Employee> emp) {

String name, address, position;
int age, cNum, empNum;
double salary;

int addMore;
System.out.println("ADD Employee");
do {
System.out.println("Employee No: ");
empNum = sc.nextInt();
sc.nextLine();
for(int x = 0; x < emp.size(); x++){
if(empNum == emp[x].getEmpNum()) {
System.out.println("The Employee ID: " +empNum+ " already exist.\nEnter New Student ID: ");
empNum = sc.nextInt();
sc.nextLine();
x = -1;
}
}
System.out.println("Name: ");
name = sc.nextLine();
System.out.println("Age: ");
age = sc.nextInt();
sc.nextLine();
System.out.println("Address: ");
address = sc.nextLine();
System.out.println("Contact No: ");
cNum = sc.nextInt();
sc.nextLine();
System.out.println("Position: ");
position = sc.nextLine();
System.out.println("Salary: ");
salary = sc.nextInt();
sc.nextLine();

emp.add(new Employee(name, address, age, cNum, empNum, salary, position));

System.out.println("To add another Student Record Press 1 [any] number to stop");
addMore = sc.nextInt();
sc.nextLine();
} while (addMore == 1);

}

public static void displayAll(ArrayList<Student> stud, ArrayList<Employee> emp){ // Definitely not working with Arraylist
System.out.println("Student ID\tStudent Name\tStudent Course\tStudent Year");
for (int x = 0; x < stud.size(); x++) {

System.out.println(stud[x].getStudNum() + "\t\t\t\t" + stud[x].getName() + "\t\t\t\t" + stud[x].getCourse() + "\t\t\t\t" + stud[x].getYear());

}
}
}

人员类别:

public class Person {

private String name, address;
private int age, cNum;

public Person() {
}

public Person(String name, String address, int age, int cNum) {
this.name = name;
this.address = address;
this.age = age;
this.cNum = cNum;
}

public String getName() {
return name;
}

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

public String getAddress() {
return address;
}

public void setAddress(String address) {
this.address = address;
}

public int getAge() {
return age;
}

public void setAge(int age) {
this.age = age;
}

public int getcNum() {
return cNum;
}

public void setcNum(int cNum) {
this.cNum = cNum;
}
}

学生类(class):

public class Student extends Person{

private int studNum, year;
private String course;

public Student(String name, String address, int age, int cNum, int studNum, int year, String course) {
super(name, address, age, cNum);
this.studNum = studNum;
this.year = year;
this.course = course;
}

public int getStudNum() {
return studNum;
}

public void setStudNum(int studNum) {
this.studNum = studNum;
}

public int getYear() {
return year;
}

public void setYear(int year) {
this.year = year;
}

public String getCourse() {
return course;
}

public void setCourse(String course) {
this.course = course;
}
}

员工类别:

public class Employee extends Person {

private int empNum;
private double salary;
private String position;

public Employee(String name, String address, int age, int cNum, int empNum, double salary, String position) {
super(name, address, age, cNum);
this.empNum = empNum;
this.salary = salary;
this.position = position;
}

public int getEmpNum() {
return empNum;
}

public void setEmpNum(int empNum) {
this.empNum = empNum;
}

public double getSalary() {
return salary;
}

public void setSalary(double salary) {
this.salary = salary;
}

public String getPosition() {
return position;
}

public void setPosition(String position) {
this.position = position;
}
}

最佳答案

如果不限于使用ArrayList,使用Map将会获得更好的性能。

Map<Integer, Employee>  employeeIndex = new HashMap<>();
//put
employeeIndex.put(employeeId, empployee);
//get
employeeIndex.get(employeeId);

如果需要使用ArrayList,可以使用filter:

List<Student> students = new ArrayList<Student>();

List<Student> filteredStudent = students.stream()
.filter(student -> student.getStudNum() == student_id)
.collect(Collectors.toList());

关于java - 如何在Arraylist java中通过ID号搜索并显示对象?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59827395/

25 4 0