从用户输入中删除arraylist元素而不使用iteratior

时间:2015-11-29 23:44:42

标签: java arraylist

我需要从用户输入中删除ArrayList的任何元素,而不使用java iterator:

(见(转换)案例2:)

当我选择选项2并继续输入名称时,例如James,它不会做任何事情,因为朋友列表是相同的。任何帮助将不胜感激!

import java.util.Scanner;
import java.util.ArrayList;

public class FriendsTest
{
   public static void main(String[] args)
   {
      Scanner input = new Scanner(System.in);

      // objects
      ArrayList<Friends> friendsList = new ArrayList<>();

      Friends a1 = new Friends("James", 10);
      Friends a2 = new Friends("Christopher", 17);
      Friends a3 = new Friends("George", 25);
      Friends a4 = new Friends("Linda", 31);
      Friends a5 = new Friends("Karen", 62);

      friendsList.add(a1);
      friendsList.add(a2);
      friendsList.add(a3);
      friendsList.add(a4);
      friendsList.add(a5);

      // menu

      int menu_choice;

      String name;
      int age;

      do
      {
         System.out.println("\n1. Add a Friend");
         System.out.println("2. Remove a Friend");
         System.out.println("3. Display all Friends");
         System.out.println("4. Exit\n");

         System.out.print("\nSelect one option: ");
         menu_choice = input.nextInt();

         switch (menu_choice)
         {
            case 1:
               System.out.print("Enter Friend's name: ");
               name = input.next();

               System.out.print("Enter Friend's age: ");
               age = input.nextInt();

               Friends a6 = new Friends(name, age);
               friendsList.add(a6);
               break;

            case 2:
               System.out.print("Enter Friend's name to remove: ");
               name = input.next();

               friendsList.remove(name);
               break;

            case 3:
               for(int k = 0; k < friendsList.size(); k++)
               {
                  System.out.println(friendsList.get(k).name + " " + friendsList.get(k).age);
               }
               break;

            case 4:
               System.exit(0);

         }//end switch        



      } while (menu_choice != 4);


   }//end main



}//end class

使用我的构造函数和方法类

进行更新
public class Friends 
   {
      public String name;
      public int age;

      // parameters
      public Friends(String _name, int _age)
      {
         name = _name;
         age = _age;
      }

       // set name
       public void setName(String friendName)
       {
          name = friendName;
       }

       // get name
       public String getName()
       {
          return name;
       }

       // set age
       public void setAge(int friendAge)
       {
          age = friendAge;
       }

       // get age
       public int getAge()
       {
          return age;
       }

       // return toString()
       public String toString()
       {
       return getName() + " " + getAge();
       }

    } //end clas

2 个答案:

答案 0 :(得分:3)

使用的简单机制是removeIf。类似的东西:

friendsList.removeIf(friend -> friend.hasName(name);

注意这使用Java 8。

假设hasName方法为Friend。如果您只有getName,那么:

friendsList.removeIf(friend -> friend.getName().equals(name));

答案 1 :(得分:1)

简单问题:不是将Friends对象传递给remove()方法,而是只传递String。编写自定义删除代码。

另请注意,始终从下到上进行索引删除,以便不跳转条目:

            System.out.print("Enter Friend's name to remove: ");
            name = input.next();

            //                  friendsList.remove(name);
            System.out.println("Trying to locate <" + name + ">");
            for (int i = friendsList.size() - 1; i >= 0; --i) {
                final Friends f = friendsList.get(i);
                System.out.println("\tChecking with <" + friendsList.get(i).name + ">");
                if (f.name.equals(name)) {
                    System.out.println("Found that bitch at index " + i);
                    friendsList.remove(i);
                    System.out.println("... and kicked him out");
                }
            }

            break;