Java:检索具有唯一ID的ArrayList的信息仅打印出添加到其中的最后一个元素的信息

时间:2017-10-09 17:27:09

标签: java arraylist

首先我会告诉你我的问题是什么,下面是代码:

当我运行Class1时,我可以注册第一个对象,该对象会获得唯一ID。之后,我注册了第二个对象,该对象也有唯一的ID。然后我尝试通过询问第一个对象的ID来检索我的第一个对象信息。现在的问题是,当我要求添加到ArrayList的第一个对象时,将始终打印出以下内容:“ID错误。您无法检索对象信息。” 然而,当我尝试检索我的第二个对象(最后添加到ArrayList的对象)时,它会打印出就像我想要的那样。< / p>

我的问题是,为什么我只能访问添加到ArrayList的最后一个对象,如何使用其唯一ID打印第一个对象信息?

这是Class1

import java.util.Scanner;
import java.util.*;

public class Class1 {

   public static void main(String[] args){
       Class1 class1 = new Class1();
       class1.presentoptions();
       class1.createObject();
   }

   Scanner sc = new Scanner(System.in);
   String name;
   String ID;
   double salary;
   Class2 class2;
   final String END_LINE = System.lineSeparator();

   public void presentoptions(){
       class2 = new Class2();
       while (true){
           System.out.println("=== Welcome === ");
           System.out.println("Choose an option below: ");
           System.out.println(" ");
           System.out.println("1. Register an object. ");
           System.out.println("2. Retrieve an objects information. ");
           System.out.println("3. Quit this program. ");
           int option = sc.nextInt();

           switch (option) {
               case 1:
                   System.out.println("What type of object? " + END_LINE
                        + " - Worker. " + END_LINE);
                        // other objects
                   String type = sc.nextLine();
                   createObject(); // creating the specified employee
                   break;

               case 2:
                   class2.retrieveObject();
                   break;
               case 3:
                   System.out.println("You've quitted the program.");
                   System.exit(0);

               default:
                   System.out.println("Error. Please try again.");
                   break;
           }
       }
}


   public void createObject(){
       class2 = new Class2();
       System.out.println("Write the name of the object (worker): ");
       String typeofobject = sc.nextLine();

       UUID uniqueID = UUID.randomUUID();
       String x = "" + uniqueID;
       System.out.println("The new ID of the " + typeofobject + " is: " + uniqueID + ".");
       System.out.println();

       System.out.println("What's the name of the new " + typeofobject + "?");
       name = sc.nextLine();
       System.out.println("What's the salary of the new " + typeofobject + "?");
       salary = sc.nextDouble();
       Employee employee = new Employee(x, name, salary);

       switch (typeofobject) {
           case "Worker":
               class2.registerObject(employee);
               break;

           default:
               System.out.println("You missspelled the type of object. Run the program again.");
               break;
       }
   }
}

这是Class2

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

class Class2 extends Class1{

   Scanner input = new Scanner(System.in);
   ArrayList<Employee> employees = new ArrayList<Employee>();
   final String END_OF_LINE = System.lineSeparator();

   public void registerObject(Employee employee){
       employees.add(employee);
   }

   public void retrieveObject() {

       System.out.println("Which objects information do you want to retrieve? Type in his/her ID.");
       String inputNewID = input.nextLine();
       for(Employee employee: employees){
           if(inputNewID.equals(employee.getID())){
               System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
           } else {
               System.out.println("The ID is wrong. You can't retrieve the objects information.");
           }
       }
   }
}

最后,Employee类:

import java.util.*;

public class Employee {

   protected String ID;
   protected String name;
   protected double grossSalary;
   final String END_OF_LINE = System.lineSeparator();


   public String getID() {
       return ID;
   }

   public Employee (String ID, String name, double grossSalary){
       this.ID = ID;
       this.name = name;
       this.grossSalary = grossSalary;
   }
}

感谢。

3 个答案:

答案 0 :(得分:1)

你有一行

OK
1
Time taken: 0.912 seconds, Fetched: 1 row(s)

在您的class2 = new Class2(); 方法中。但createObject()是您拥有员工ArrayList的地方。因此,每当您致电class2时,您只是在吹走旧的ArrayList。

我认为你可以摆脱那条线。

答案 1 :(得分:0)

注意for循环:

for(Employee employee: employees){
       if(inputNewID.equals(employee.getID())){
           System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
       } else {
           System.out.println("The ID is wrong. You can't retrieve the objects information.");
       }
   }

它将在员工列表中打印FOR EACH对象,无论它是否与inputNewID相等,我认为其意图是在ONCE中打印。

你可以使用布尔标志获得想要的行为:

boolean isEmployeeFound = false;
for(Employee employee: employees){
       if(inputNewID.equals(employee.getID())){
           isEmployeeFound = true;
           System.out.println("ID: " + employee.ID + END_OF_LINE + "Name:"+employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
       } 
   }
 if(!isEmployeeFound){System.out.println("The ID is wrong. You can't retrieve the objects information.");}

另外正如其他提到的那样摆脱了createObject函数中的class2 = new Class2()行​​。

答案 2 :(得分:0)

问题在于Class2的for循环中的if / else,或者更具体地说是else。在循环的第一个元素上,将触发else以给出消息。处理这个问题的一种方法是添加一个布尔值来检查是否找到了ID,并在循环之后将else块更改为if检查:

   boolean idFound = false;
   for(Employee employee: employees){
       if(inputNewID.equals(employee.getID())){
           idFound = true;
           System.out.println("ID: " + employee.ID + END_OF_LINE + "Name: " + employee.name + END_OF_LINE + "Salary: " + employee.grossSalary);
       } 
   }
   if(! idFound) {
       System.out.println("The ID: " + inputNewID + " is not found. You can't retrieve the objects information.");
   }