需要消除阵列列表中的冗余对象

时间:2019-07-10 04:20:46

标签: java arraylist

菜单驱动的Java程序,用于输入和显示员工的详细信息。当用户想要添加员工时,他选择选项1,而他选择选项5以显示他输入的详细信息。

我尝试了list.contains(Object o)方法,但是它一直将重复的对象添加到列表中。现在,我已经删除了那部分代码。

class Employee {

    Scanner sc = new Scanner(System.in);

    int empid, num;

    String empname, empdesignation, empdept, empproject;
    //static ArrayList al = new ArrayList();


    public void setEmpNo(int n) {
        this.num = n;
    }

    public int getEmpNo() {
        return num;
    }

    public int getID() {
        return empid;
    }

    public void setID(int id) {
        this.empid = id;
    }

    public String getName() {
        return empname;
    }

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

    public String getDept() {
        return empdept;
    }

    public void setDept(String dept) {
        this.empdept = dept;
    }

    public String getDesig() {
        return empdesignation;
    }

    public void setDesig(String desig) {
        this.empdesignation = desig;
    }

    public String getProject() {
        return empproject;
    }

    public void setProject(String proj) {
        this.empproject = proj;
    }

    public void displayemp(int id, String name, String dept, String designation, String project) {
        System.out.print("\n============================================================\n");
        System.out.println("ID: " + id);
        System.out.println("Name: " + name);
        System.out.println("Department: " + dept);
        System.out.println("Designation: " + designation);
        System.out.println("Project: " + project);
        System.out.print("\n============================================================\n");

    }


}
public class trying {

    public static void main(String args[]) {

        Scanner sc = new Scanner(System.in);

        int ch = 0, id, n, empID, pid, projectID;
        // final int maxEmp=1000;

        String name, designation, dept, pname, mgrname, project;

        Employee emp10 = new Employee();
        Project proj10 = new Project();
        List<Employee> list = new ArrayList<Employee>();
        List<Project> list1 = new ArrayList<Project>();
        // String[] p2=new String[100];
        do {

            System.out.println("MENU");

            System.out.println(
                    " 1.Add Employee\n 2.Add Project \n 3.Edit Employee\n 4.Edit Project\n 5.Show Employee Deatils \n 6.Show Project Details \n 7.Exit");

            // System.out.println("Enter the maximum number of employees: ");

            // Employee[] emp=new Employee[maxEmp];

            System.out.print("Enter choice: ");
            try {

                ch = sc.nextInt();

                sc.nextLine();

            } catch (Exception e) {
                System.out.println("Enter an Integer value");
            }
            switch (ch) {

            case 1:
                // Employee emp6 = new Employee(1,"qwe","asd","zxc","qazwsxedc");
                // Employee emp7 = new Employee(2,"q","d","z","q");
                System.out.println("Enter the number of employees to be entered: ");
                try {
                    n = sc.nextInt();
                    emp10.setEmpNo(n);
                    if (n < 0) {
                        System.out.println("Only Positive Numbers & no Letters Please!");
                        continue;
                    }

                    for (int i = 0; i < n; i++) {
                        Employee emp5 = new Employee();

                        System.out.print("\n============================================================\n");

                        System.out.print("Enter ID: ");



                            id = sc.nextInt();
                            try {
                                if (id < 0) {
                                System.out.println("Enter an Integer value");
                                continue;
                            }

                            emp5.setID(id);

                        } catch (InputMismatchException e) {
                            System.out.println("Enter an Integer value");
                            sc.next();
                            System.exit(0);
                        }

                        sc.nextLine();
                        System.out.print("Enter Name: ");

                        name = sc.nextLine();

                        emp5.setName(name);

                        System.out.print("Enter Department: ");

                        dept = sc.nextLine();

                        emp5.setDept(dept);

                        System.out.print("Enter Designation: ");

                        designation = sc.nextLine();

                        emp5.setDesig(designation);

                        System.out.print("Enter Project: ");

                        project = sc.nextLine();

                        emp5.setProject(project);

                        System.out.print("\n============================================================\n");
                        // emp5.displayemp(id, name, dept, designation, project);

                        list.add(emp5);
                        // list.add(emp7);

                    }
                }

                catch (InputMismatchException e) {
                    System.out.println("Enter an Integer value");
                    sc.next(); // this consumes the invalid token
                }
case 5:
                System.out.println("Showing Employee Details:");
                for (Employee e : list) {
                    System.out.print("\n============================================================\n");
                    System.out.println("Id: " + e.empid + "\nName: " + e.empname + "\nDept: " + e.empdept + "\nDesig: "
                            + e.empdesignation + "\nProject: " + e.empproject);
                    System.out.print("\n============================================================\n");

                }
                break;

我希望此代码删除用户输入的相同条目,即没有多余的条目。

电流输出

Id:1 姓名:q 部门: 设计:z 项目:qaz

================================================ ============

================================================ ============

Id:1 姓名:q 部门: 设计:z 项目:qaz

================================================ ============

预期输出

Id:1 姓名:q 部门: 设计:z 项目:qaz

================================================ ============

1 个答案:

答案 0 :(得分:0)

list.contains(Object o)需要一些额外的帮助,才能自己找到对象。根据{{​​3}},ArrayList实现了List接口,该接口使用equals()比较两个对象。使用contains()时,必须在类中定义一个equals()方法。

也许是这样的:

class Employee {
 //...
  @Override
  public boolean equals(Object o) {
    boolean same = true;

    if(o != null && o instanceof Employee) {
      //check all member variables - remember to use .equals() for string comparison 
      if(
        (this.empId != o.empId) ||
        (this.num != o.num) ||
        (!(this.empname.equals(o.empname)) ||
        (!(this.emdesignation.equals(o.empdesignation)) ||
        (!(this.empdept.equals(o.empdept)) ||
        (!(this.empproject.equals(o.empproject))
      ) {
        same = false;
      }
    }
    else {
      //can't be equal if it's null or not an employee
      same = false;
    }

    return same;
  }
}

定义了equals函数后,您就可以使用ArrayList来检查员工是否已经在list.contains(emp5)中。