2016-09-12 7 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()が単一のオブジェクトクラスレベルで動作し、操作を実行したいときにそのセッターでその属性を変更するだけのパラメータを送信する

もあなたは、単一のオブジェクトで動作するように、例えば1 *

​​

で動作することができるかどうかなど、多くのスキャナオブジェクトを宣言するには、何か

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のアドバイスに従うことができます –

関連する問題