【问题标题】:Remove method not working correctly in ArrayList [duplicate]删除方法在 ArrayList 中无法正常工作 [重复]
【发布时间】:2016-09-12 01:18:58
【问题描述】:

由于某种原因,在我的伪数据库中,我的删除方法似乎完全无效并且无法正常工作。源代码如下:

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. 

我在这里做错了什么?

【问题讨论】:

  • 您需要重写equals 方法并比较字段。如果equals 没有被覆盖,它只会使用默认行为来比较引用,而不是值。
  • 您没有在 Person 类中定义 equals() 方法。
  • 顺便问一下如何写MCVE。您的问题包含大量冗余信息。
  • 我看不到将new Person 传递给函数的目的。要么在函数外构建人,然后将其传入或在内部构建

标签: java arraylist


【解决方案1】:

如果您想比较对象应该有这样的东西,请在此处完整回答here !

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

或者可以比较其对象的任何特定字段,例如

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

*无需发送参数 new Person() 可以与单个对象类级别一起使用,并且仅在您要执行某些操作时使用其设置器修改其属性

如果您可以使用一个对象,也不要声明尽可能多的 Scanner 对象 *

static Scanner userInput = new Scanner(System.in);

处理单个对象可能是什么

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

及其方法在请求数据输入时添加或删除设置对象属性创建和比较也基于该对象执行

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

【讨论】:

  • 我不太清楚你的意思是我不需要向一个新的 Person() 发送参数,你会怎么做?
  • @Linuxn00b - 你可以创建new Person() inside 方法并移除参数
  • 更新我的答案,或者你可以听从@Linuxn00b 的建议
猜你喜欢
  • 1970-01-01
  • 2011-06-14
  • 2012-07-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多