有問題顯示我在數組列表中搜索的內容。當我搜索數組時,它會查找信息,但它會將所有列表中的所有對象都命名爲fisherman。它將搜索該項目,如果找到將打印我想要的項目,但它會打印所有漁夫的實例。但是搜索很笨重,並且在某些情況下不起作用。所以,我需要搜索功能的幫助才能對搜索進行正確的搜索和輸出。我不知道如何將對象名稱從「漁夫」改爲每個漁夫的SSN,然後這將有助於搜索參數。但我無法想出一種方法來改變每個漁民的對象名稱。感謝您提供任何幫助。函數數組列表搜索
public static void fishermanMenu() {
// declare variables and array
int selection;
String fName;
String lName;
String ssn;
String number;
String email;
String searchSSN;
String menuFisherman[] = {"Register New Fisherman", "Search Fisherman", "Return to Main Menu"};
System.out.println("\nFisherman Menu");
// for loop to print menu from array
for(int i = 0, k = 1; i < menuFisherman.length; i++, k++) {
System.out.println("("+ k + ") " + (menuFisherman[i]));
}
System.out.print("Selection: ");
selection = input.nextInt();
input.nextLine();
if (selection == 1) {
$Fisherman fisherman = new $Fisherman();
fishermen.add(fisherman);
System.out.print("Enter First Name: ");
fName = input.next();
fisherman.setFirstName(fName);
System.out.print("Enter Last Name: ");
lName = input.next();
fisherman.setLastName(lName);
System.out.print("Enter Phone Number...ie 904-560-7896: ");
number = input.next();
fisherman.setPhoneNumber(number);
System.out.print("Enter SSN...ie 123-45-6789: ");
ssn = input.next();
fisherman.setSSN(ssn);
System.out.print("Enter email address: ");
email = input.next();
fisherman.setEmailAddress(email);
System.out.print(fishermen.toString());
fishermanMenu();
}
else if (selection == 2) {
System.out.println("Enter Social Security Number of fisherman to search for below.");
System.out.print("SSN: ");
searchSSN = input.nextLine();
for ($Fisherman fisherman : fishermen) {
if (fisherman.getSSN().equals(searchSSN)) {
System.out.println(fishermen.toString());
fishermanMenu();
}
else {
System.out.println("No match for that fisherman");
fishermanMenu();
}
}
}
}
這裏是輸出。正如你所看到的,當我搜索「123」時,它返回了整個列表,當我搜索「456」時,它說「沒有匹配那個漁夫」。
Main Menu
(1) Fisherman Menu
(2) Tournament Menu
(3) Fish Menu
(4) Close Tournament
(5) Quit Program
Selection: 1
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 1
Enter First Name: Matt
Enter Last Name: P
Enter Phone Number...ie 904-560-7896: 123
Enter SSN...ie 123-45-6789: 123
Enter email address: 123
[fisherman{SSN=123, firstName='Matt', lastName='P', phoneNumber='123', emailAddress='123'}]
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 1
Enter First Name: Joe
Enter Last Name: K
Enter Phone Number...ie 904-560-7896: 456
Enter SSN...ie 123-45-6789: 456
Enter email address: 456
[fisherman{SSN=123, firstName='Matt', lastName='P', phoneNumber='123', emailAddress='123'}, fisherman{SSN=456, firstName='Joe', lastName='K', phoneNumber='456', emailAddress='456'}]
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 2
Enter Social Security Number of fisherman to search for below.
SSN: 123
[fisherman{SSN=123, firstName='Matt', lastName='P', phoneNumber='123', emailAddress='123'}, fisherman{SSN=456, firstName='Joe', lastName='K', phoneNumber='456', emailAddress='456'}]
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection: 2
Enter Social Security Number of fisherman to search for below.
SSN: 456
No match for that fisherman
Fisherman Menu
(1) Register New Fisherman
(2) Search Fisherman
(3) Return to Main Menu
Selection:
小建議在這裏:Java約定不鼓勵在標識符中使用「$」,除了遺留或機器生成的代碼。閱讀Java語言規範[這裏](https://docs.oracle.com/javase/specs/jls/se8/html/jls-3.html#jls-3.8) –