[英]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.