簡體   English   中英

java - 如何在Arraylist java中按ID號搜索和顯示對象?

[英]How to search and display an object by ID number in an Arraylist java?

我真的需要幫助我的程序。 我是 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;
    }
}

學生班級:

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,您可以使用過濾器:

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

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

就這樣,如果您正在搜索學生,則 id 為 1 2

ArrayList<Integer> students;
students.forEach(stu -> {if ( stu.getStudNum() == 2) System.out.println(stu.getName());});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM