如何在java中使用扫描程序时删除ArrayList中的特定对象

问题描述 投票:1回答:4

我正试图通过我的输入(扫描仪)从我的ArrayList(成员列表)中删除一个成员播放器(对象)

我试过环顾谷歌和堆栈,但似乎无法找到任何使用扫描仪的东西。

    public void removeMember(){
    System.out.println("Which MemberPlayer are you looking for?:");
    System.out.print("Input first name: ");
    String fName = input.nextLine().toUpperCase();
    System.out.print("Input last name: ");
    String lName = input.nextLine().toUpperCase();

    for (MemberPlayer m: memberlist){
        if(m.getFirstName().contains(fName) && m.getLastName().contains(lName)) {
            System.out.println();
            System.out.println("This MemberPlayer exist:");
            System.out.println(fName + " " + lName);

            System.out.print("Do you want to remove this MemberPlayer?  [yes/no]");
            input.nextLine().toUpperCase();
            if (input.equals("Yes")) {
                memberlist.remove(); //I can't figure out how to write this line?
            }else{
                break;
            }
        }else {
            System.out.println();
            System.out.println("This MemberPlayer doesn't exist");
            System.out.println();
            break;
        }
    }
}

我从file.txt中读取了我的成员列表,其中包含以下信息:名字,姓氏,年龄和团队。

ANDERS
ANDERSEN 23 1

BERT BERSEN 16 2

HANS HANSEN 25 1

TIM TIMSEN 20 2
MORTEN MORTENSEN 34 1
java object arraylist java.util.scanner
4个回答
0
投票

您需要使用文档中的List#remove()

boolean remove(Object o)

从该列表中删除第一次出现的指定元素(如果存在)(可选操作)。如果此列表不包含该元素,则不会更改。更正式地,删除具有最低索引i的元素,使得(o == null?get(i)== null:o.equals(get(i)))(如果存在这样的元素)。如果此列表包含指定的元素,则返回true(或等效地,如果此列表因调用而更改)。


此外,你不需要这里的for-loop。您的方法可以简化为更多OO方法:

public void removeMember() {
    System.out.println("Which MemberPlayer are you looking for?:");
    System.out.print("Input first name: ");
    String fName = input.nextLine().toUpperCase();
    System.out.print("Input last name: ");
    String lName = input.nextLine().toUpperCase();

    // create an object with input received
    MemberPlayer m = new MemberPlayer(fName, lName);

    // use contains of List
    if (memberlist.contains(m)) {
        memberlist.remove(m);
    } else {
        System.out.println("This MemberPlayer doesn't exist");
    }
}

确保覆盖.equals()中的.hashcode()MemberPlayer方法。


0
投票

我建议使用Iterator,因为使用List.remove(Object o)可以抛出ConcurrentModificationException,因为你在迭代时改变了对象的状态。

所以Iterator.remove()将是一个安全的赌注。来自Java SE 1.8文档:

迭代器允许调用者在迭代期间使用定义良好的语义从底层集合中删除元素。

因此,使用List直接从List.remove()删除对象将导致不可预测的迭代并在迭代时抛出ConcurrentModificationException

如果你没有迭代,那么可以使用List.remove(Object o)List中删除对象。

//Initializes the iterator, checks if next element is present by calling Iterator.hasNext()
for(Iterator<MemberPlayer> itr = memberList.iterator(); itr.hasNext(); ){
         m = itr.next(); //The current element of the List
         if(m.getFirstName().contains(fName) && m.getLastName().contains(lName)) {
            System.out.println();
            System.out.println("This MemberPlayer exist:");
            System.out.println(fName + " " + lName);

            System.out.print("Do you want to remove this MemberPlayer?  [yes/no]");
            input.nextLine().toUpperCase();
            if (input.equals("Yes")) {
                 itr.remove(); //Removes the current element if the condition is satisfied.
            }else{
                 break;
            }
         }else {
             System.out.println();
             System.out.println("This MemberPlayer doesn't exist");
             System.out.println();
             break;
         }
 }

0
投票

请记住,在使用Collection<T>循环迭代时,无法删除for-each元素。在那种情况下,可能会抛出ConcurrentModificationException

您需要明确使用Interator<T>ListIterator<T>,具体取决于用例。 ListIterator还允许插入元素或设置元素。

for (final Iterator<MemberPlayer> iterator = memberList.iterator(); iterator.hasNext();) {
   final MemberPlayer m = iterator.next();

   if (m.getFirstName().contains(fName) && m.getLastName().contains(lName)) {
      ...

      iterator.remove();
   }
}

0
投票

这是经过一些试验和错误后最终为我工作的结果。

public void removeMember()throws FileNotFoundException {
    System.out.println("Which MemberPlayer are you looking to remove?:");
    System.out.print("Input first name: ");
    String fName1 = input.nextLine().toUpperCase();
    System.out.print("Input last name: ");
    String lName2 = input.nextLine().toUpperCase();

    for (MemberPlayer m : memberlist){

        if (m.getFirstName().equals(fName1) & m.getLastName().equals(lName2)) {
            System.out.println();
            memberlist.remove(m);
            System.out.println("You removed: "+m.getFirstName()+" "+m.getLastName());
            System.out.println();
            saveMember();
            break;
        } else {
            System.out.println();
            System.out.println("This MemberPlayer doesn't exist");
            System.out.println();
            break;
        }
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.