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

我正在尝试通过我的输入(扫描仪)从我的 ArrayList(memberList) 中删除 MemberPlayer(object)


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


    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


繁星淼淼
浏览 110回答 3
3回答

杨魅力

我建议Iterator为此使用 an ,因为在迭代时更改对象的状态时使用List.remove(Object o)can throw 。ConcurrentModificationException所以Iterator.remove()将是一个安全的赌注。来自Java SE 1.8文档:迭代器允许调用者在迭代期间以定义明确的语义从底层集合中移除元素。所以直接从Listusing 中移除一个对象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(); ){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;m = itr.next(); //The current element of the List&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;if(m.getFirstName().contains(fName) && m.getLastName().contains(lName)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("This MemberPlayer exist:");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println(fName + " " + lName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.print("Do you want to remove this MemberPlayer?&nbsp; [yes/no]");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; input.nextLine().toUpperCase();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (input.equals("Yes")) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;itr.remove(); //Removes the current element if the condition is satisfied.&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println("This MemberPlayer doesn't exist");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;break;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;}

慕标5832272

经过反复试验,这最终为我工作。public void removeMember()throws FileNotFoundException {&nbsp; &nbsp; System.out.println("Which MemberPlayer are you looking to remove?:");&nbsp; &nbsp; System.out.print("Input first name: ");&nbsp; &nbsp; String fName1 = input.nextLine().toUpperCase();&nbsp; &nbsp; System.out.print("Input last name: ");&nbsp; &nbsp; String lName2 = input.nextLine().toUpperCase();&nbsp; &nbsp; for (MemberPlayer m : memberlist){&nbsp; &nbsp; &nbsp; &nbsp; if (m.getFirstName().equals(fName1) & m.getLastName().equals(lName2)) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; memberlist.remove(m);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("You removed: "+m.getFirstName()+" "+m.getLastName());&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; saveMember();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println("This MemberPlayer doesn't exist");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; System.out.println();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}

肥皂起泡泡

您需要使用文档中的List#remove():布尔删除(对象 o)如果指定元素存在,则从该列表中删除第一次出现的指定元素(可选操作)。如果此列表不包含该元素,则它不变。更正式地说,删除具有最低索引 i 的元素,使得 (o==null ? get(i)==null : o.equals(get(i))) (如果存在这样的元素)。如果此列表包含指定的元素(或等效地,如果此列表因调用而更改),则返回 true。另外,这里不需要for-loop。您的方法可以简化为更面向对象的方法:public void removeMember() {&nbsp; &nbsp; System.out.println("Which MemberPlayer are you looking for?:");&nbsp; &nbsp; System.out.print("Input first name: ");&nbsp; &nbsp; String fName = input.nextLine().toUpperCase();&nbsp; &nbsp; System.out.print("Input last name: ");&nbsp; &nbsp; String lName = input.nextLine().toUpperCase();&nbsp; &nbsp; // create an object with input received&nbsp; &nbsp; MemberPlayer m = new MemberPlayer(fName, lName);&nbsp; &nbsp; // use contains of List&nbsp; &nbsp; if (memberlist.contains(m)) {&nbsp; &nbsp; &nbsp; &nbsp; memberlist.remove(m);&nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println("This MemberPlayer doesn't exist");&nbsp; &nbsp; }}确保覆盖. .equals()_.hashcode()MemberPlayer
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java