2016-09-12 83 views
0

出於某種原因,在我的僞數據庫中,我的remove方法似乎完全無效並且無法工作。源代碼如下:刪除方法在ArrayList中無法正常工作

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

public class Lab2 { 
    static ArrayList<Person> peopleDirectory = new ArrayList<Person>(10); 

    public static void main(String[] args) { 
     // TODO Auto-generated method stub 
     int choice; 

     Scanner userInput = new Scanner(System.in); 

     do { 
      System.out.println("Welcome to the people directory please make a choice from the list below:"); 
      System.out.println("-------------------------------------------------------------------------"); 
      System.out.println("1. Add a person to the directory."); 
      System.out.println("2. Remove a Person from the directory."); 
      System.out.println("3. View the User Directory."); 
      System.out.println("4. Exit the directory."); 
      choice = userInput.nextInt(); 
      switch(choice) { 
      case 1: 
        addPerson(new Person()); 
        break; 
      case 2: removePerson(new Person()); 
        break; 
      case 3: displayPeople(); 
        break; 
      case 4: System.out.println("Thanks for using the people diretory!"); 
        System.exit(0); 
        break; 
      default: System.out.println("Invalid choice! Please select a valid choice!"); 
        break; 

      } 
     } while (choice != 4); 
    } 

    public static void addPerson(Person thePerson) { 
     String firstName; 
     String lastName; 
     String phoneNumber; 
     int age; 
     if (peopleDirectory.size() >= 10) { 
      System.out.println("Sorry the list can not be larger than 10 people"); 
     } else { 
      int i = 0; 
      Scanner input = new Scanner(System.in); 
      System.out.println("Enter the first name of the Person you would like to add: "); 
      firstName = input.nextLine(); 
      thePerson.setFirstName(firstName); 
      System.out.println("Enter the last name of the Person you would like to add: "); 
      lastName = input.nextLine(); 
      thePerson.setLastName(lastName); 
      System.out.println("Enter the phone number of the Person you would like to add: "); 
      phoneNumber = input.nextLine(); 
      thePerson.setPhoneNumber(phoneNumber); 
      System.out.println("Enter the age of the Person you would like to add: "); 
      age = input.nextInt(); 
      thePerson.setAge(age); 
      peopleDirectory.add(i, thePerson); 

      i++; 
     } 



    } 

    public static void removePerson(Person thePerson) { 
     if (peopleDirectory.size() < 1) { 
      System.out.println("There is absolutely nothing to remove from the Directory"); 
     } 


     else { 
      Scanner input = new Scanner(System.in); 
      System.out.println("Please enter the first name of the person you would like to delete: "); 
      String firstName = input.nextLine(); 
      thePerson.setFirstName(firstName); 
      System.out.println("Enter the last name of the Person you would like to remove: "); 
      String lastName = input.nextLine(); 
      thePerson.setLastName(lastName); 
      System.out.println("Enter the phone number of the Person you would like to remove: "); 
      String phoneNumber = input.nextLine(); 
      thePerson.setPhoneNumber(phoneNumber); 
      System.out.println("Enter the age of the Person you would like to remove: "); 
      int age = input.nextInt(); 
      thePerson.setAge(age); 
      for (int i = 0; i < peopleDirectory.size(); i++) { 
       if (peopleDirectory.get(i).equals(thePerson)) { 
        peopleDirectory.remove(thePerson); 
       } 
      } 

     } 
    } 

    public static void displayPeople() { 
     for (Person person : peopleDirectory) { 
      System.out.println("First Name: " + person.getFirstName() + " Last name: " + 
          person.getLastName() + " Phone number: " + person.getPhoneNumber() + 
          " Age: " + person.getAge()); 
     } 
    } 



} 

class Person { 
    private String firstName; 
    private String lastName; 
    private int age; 
    private String phoneNumber; 

    public Person (String firstName, String lastName, int personAge, String phoneNumber) { 
     this.firstName = firstName; 
     this.lastName = lastName; 
     this.age = personAge; 
     this.phoneNumber = phoneNumber; 
    } 

    public Person() { 
     this.firstName = ""; 
     this.lastName = ""; 
     this.age = 0; 
     this.phoneNumber = ""; 
    } 

    public int getAge() { 
     return this.age; 
    } 

    public String getFirstName() { 
     return this.firstName; 
    } 

    public String getLastName() { 
     return this.lastName; 
    } 

    public String getPhoneNumber() { 
     return this.phoneNumber; 
    } 


    public void setFirstName(String firstName) { 
     this.firstName = firstName; 
    } 

    public void setLastName(String lastName) { 
     this.lastName = lastName; 
    } 

    public void setAge(int age) { 
     this.age = age; 
    } 

    public void setPhoneNumber(String phoneNumber) { 
     this.phoneNumber = phoneNumber; 
    } 
} 

當我嘗試從ArrayList中移除一個元素時,它仍然保留在arrayList中。我不知道爲什麼,但我覺得我的刪除方法有點笨拙。

比如我添加的元素,並嘗試將其刪除(請參閱下面的輸出):

Welcome to the people directory please make a choice from the list below: 
------------------------------------------------------------------------- 
1. Add a person to the directory. 
2. Remove a Person from the directory. 
3. View the User Directory. 
4. Exit the directory. 
1 
Enter the first name of the Person you would like to add: 
Tom 
Enter the last name of the Person you would like to add: 
Jones 
Enter the phone number of the Person you would like to add: 
6073388152 
Enter the age of the Person you would like to add: 
24 
Welcome to the people directory please make a choice from the list below: 
------------------------------------------------------------------------- 
1. Add a person to the directory. 
2. Remove a Person from the directory. 
3. View the User Directory. 
4. Exit the directory. 
3 
First Name: Tom Last name: Jones Phone number: 6073388152 Age: 24 
Welcome to the people directory please make a choice from the list below: 
------------------------------------------------------------------------- 
1. Add a person to the directory. 
2. Remove a Person from the directory. 
3. View the User Directory. 
4. Exit the directory. 
2 
Please enter the first name of the person you would like to delete: 
Tom 
Enter the last name of the Person you would like to remove: 
Jones 
Enter the phone number of the Person you would like to remove: 
6073388152 
Enter the age of the Person you would like to remove: 
24 
Welcome to the people directory please make a choice from the list below: 
------------------------------------------------------------------------- 
1. Add a person to the directory. 
2. Remove a Person from the directory. 
3. View the User Directory. 
4. Exit the directory. 
3 
First Name: Tom Last name: Jones Phone number: 6073388152 Age: 24 
Welcome to the people directory please make a choice from the list below: 
------------------------------------------------------------------------- 
1. Add a person to the directory. 
2. Remove a Person from the directory. 
3. View the User Directory. 
4. Exit the directory. 

什麼可能我做錯了嗎?

+1

您需要重寫equals方法並比較字段。如果'equals'沒有被覆蓋,它只是使用默認行爲來比較引用,而不是值。 – resueman

+0

你沒有在你的Person類中定義一個'equals()'方法。 – ajb

+1

順便說一句,請看看如何編寫[MCVE](http://stackoverflow.com/help/mcve)。你的問題包含大量的冗餘信息。 – ajb

回答

0

如果要比較的對象應該有這樣的事情,在這裏完整的答案here !

public boolean equals(Object object2) { 
    return object2 instanceof MyClass && a.equals(((MyClass)object2).a); 
} 

或可以比較爲宗旨的任何特定領域比如

if(peopleDirectory.get(i).getFirstName().equals(thePerson.getFirstName())) 

*沒有必要發送一個參數一個新的Person()可以使用單個對象類的級別,並且只有在你想執行一些操作時用它的setter修改它們的屬性

也宣佈儘可能多的掃描對象,如果你可以用一個例如工作*

​​

與單個對象的工作可能是一些

static Person person = new Person();//declaration 

及其方法添加或請求數據錄入時刪除setteas創建對象屬性並根據該對象執行比較

System.out.println("Enter the first name of the Person you would like to add: "); 
     person.setFirstName(userInput.nextLine());//data entry and setteo 

if (peopleDirectory.get(i).equals(person)) // comparation 
+0

我不太確定您的意思,我不需要發送一個新的Person()參數,您會如何做到這一點? – Linuxn00b

+0

@ Linuxn00b - 您可以在**方法內創建'Person()** **並刪除參數 –

+0

更新我的答案,或者您可以按照@ Linuxn00b的建議 –