繁体   English   中英

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

[英]Java: Retrieving information of an ArrayList with unique ID only prints out info of last element added to it

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

当我运行Class1我可以注册我的第一个对象,该对象具有唯一的ID。 之后,我注册第二个对象,该对象也具有唯一的ID。 然后,我尝试通过询问第一个对象的ID来检索我的第一个对象信息 现在的问题是,当我请求添加到ArrayList的第一个对象时,以下内容将始终被打印出来:“ ID错误。您无法检索对象信息。” 但是 ,当我尝试检索第二个对象(该对象最后添加到ArrayList )时,它将按我的需要打印出信息。

我的问题是,为什么我只能访问添加到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;
   }
}

谢谢。

你有线

class2 = new Class2();

在您的createObject()方法中。 但是class2是您拥有员工ArrayList的地方。 因此,每次调用createObject()时,您就只是吹散了旧的ArrayList。

我认为您可以摆脱这一限制。

注意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,我认为目的是一次打印。

您可以使用布尔值标志获得所需的行为:

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()行​​。

问题出在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.");
   }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM