如何在ArrayList中搜索特定的自定义对象属性,然后删除

时间:2019-04-28 14:53:00

标签: java arraylist equals

我有一个电话簿程序,该程序用文本文件中的数据Entry个对象填充ArrayList。

使用构造函数创建数据后,数据将被加载到ArrayList中,然后按姓氏字母顺序进行排序。我需要做的是输入名称或电话分机号,然后将其从电话簿中删除。我还需要能够通过输入名称来搜索分机号码

到目前为止,我已经尝试过修改equals方法并在Entry类中创建了deleteByName方法,但是在运行程序时会导致很多错误(ConcurrentModificationException在几行不同)

public class Entry implements Comparable<Entry> {

    private String firstName = null;
    private String surname = null;
    private String extension = null;

    public Entry(String firstName, String surname, String extension) {
        super();
        this.firstName = firstName;
        this.surname = surname;
        this.extension = extension;
    }

    public String getFirstName() {
        return firstName;
    }

    public String getSurname() {
        return surname;
    }

    public String getExtension() {
        return extension;
    }

    @Override
    public String toString() {
        return "\nFirst Name: " + getFirstName() + "\nSurname: " + getSurname() + "\nExtension: " + getExtension();
    }

    @Override
    public int compareTo(Entry e) {
        return surname.compareTo(e.getSurname());
    }

    @Override
    public boolean equals(Object arg0) {
        return ((Entry) arg0).firstName.equalsIgnoreCase(this.firstName);
    }

}

我有一个名为ArrayDirectory的类,它具有ArrayList和所有操作方法。

public class ArrayDirectory implements Directory {

    // Instantiate variables for program
    private final static int MAX_ENTRIES = 20;
    private static Scanner input = new Scanner(System.in);
    DirectoryFile file = new DirectoryFile("C:\\Users\\John\\Documents\\Phonebook.txt");
    private ArrayList<Entry> phonebook = new ArrayList<>(MAX_ENTRIES);

    public ArrayDirectory() throws IOException {
        loadEntries(file.getFile());
        runMenu();
    }

    public void runMenu() {
        Scanner sc = new Scanner(System.in);
        int choice;
        do {
            System.out.println("\nStaff Phonebook Program");
            System.out.println("Select an option: (1-6)");
            System.out.println("1) Add new record");
            System.out.println("2) Delete record");
            System.out.println("3) Search for record");
            System.out.println("4) Change a number");
            System.out.println("5) Display all records");
            System.out.println("6) Exit program");

            choice = sc.nextInt();

            switch (choice) {
            case 1:
                System.out.println("\nEnter first name: ");
                String fname = input.nextLine();
                System.out.println("Enter surname: ");
                String sname = input.nextLine();
                System.out.println("Enter telephone extension: ");
                String telephone = input.nextLine();
                addEntry(fname, sname, telephone);
                break;
            case 2:
                printDirectory();
                System.out.println("Enter name or number: ");
                String nameOrNum = input.nextLine();
                System.out.println("Removed staff member: " + deleteByName(nameOrNum, phonebook));

                break;
            case 3:

                break;
            case 4:

                break;
            case 5:
                printDirectory();
                break;
            case 6:
                System.out.println("Program closed");
                break;
            default:

                break;
            }
        } while (choice != 6);
    }

    // This method will allow the user to add a new entry to the phone book. The
    // phone book will be
    // alphabetically sorted.
    public void addEntry(String firstName, String surname, String extension) {
        Entry entry = new Entry(firstName, surname, extension);
        phonebook.add(entry);
        System.out.println("\nAdded staff member: " + firstName + "\t" + surname + "\t" + extension);

    } // End of addEntry()

    public ArrayList<Entry> loadEntries(File file) throws IOException {
        BufferedReader br = new BufferedReader(new FileReader(file));
        try {
            String line = br.readLine();

            while (line != null) {
                String arr[] = line.split("\\s");
                // Entry(firstName, surname, extension)
                Entry entry = new Entry(arr[0], arr[1], arr[2]);
                phonebook.add(entry);
                line = br.readLine();
                if (line.isEmpty()) {
                    break;
                }

            }
        } finally {
            br.close();
        }
        sortBySurname(phonebook);
        return phonebook;
    } // End of loadEntries()

    // This method will allow the user to delete a specific entry
    // From the directory either by name or number
    public void deleteEntry(String fullName, String phoneNumber) {
        // TODO
    }

    public void printDirectory() {
        System.out.println("Surname \tFirst Name \tExtension Number");
        for (Entry e : phonebook) {
            System.out.println(e.getFirstName() + "\t" + e.getSurname() + "\t" + e.getExtension());
        }
    }

    public ArrayList<Entry> sortBySurname(ArrayList<Entry> entries) {
        Collections.sort(entries);
        return entries;
    }

    public boolean deleteByName(String name, ArrayList<Entry> entries) {
        for (Entry e : entries) {
            if (e.getFirstName().equals(name)) {
                entries.remove(e);
            }
        }
        return false;
    }
}

如果可能的话,我想正确地覆盖equals方法,以便可以将其用于其他操作。我认为我正在努力完全理解equals方法的工作原理。这将是解决该问题的最佳方法,并且还可以用于帮助我执行其他操作吗?

0 个答案:

没有答案