ArrayLists(删除和更改元素)

时间:2016-09-04 23:54:55

标签: java arraylist

大家好我是Java的业余爱好者,对使用ArrayLists的程序有一些具体的问题。该程序由几个类组成,其目的是添加,更改,删除和显示电话簿中的朋友。我已经完成了添加和显示方法,但我在删除和更改方法方面遇到了问题。我在这个网站上看到了类似的案例,但它并没有帮助我解决我的问题。任何帮助都将非常感激。这就是我到目前为止所做的:

  

package bestfriends;

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

public class BFFHelper 
{
    ArrayList<BestFriends> myBFFs;
    Scanner keyboard = new Scanner(System.in);

    public BFFHelper()
    {
        myBFFs = new ArrayList<BestFriends>();
    }

    public void addABFF()
    {
        System.out.println("Enter a first name: ");
        String firstName = keyboard.next();
        System.out.println("Enter a last name: ");
        String lastName = keyboard.next();
        System.out.println("Enter a nick name: ");
        String nickName = keyboard.next();
        System.out.println("Enter a phone number: ");
        String cellPhone = keyboard.next();
        BestFriends aBFF = new BestFriends(firstName, lastName, nickName, cellPhone);
        myBFFs.add(aBFF);


    }

    public void changeABFF()
    {
        System.out.println("I am in changeBFF");
    }

    public void displayABFF()
    {
        System.out.println("My Best Friends Phonebook is: ");
        System.out.println(myBFFs);
    }

    public void removeABFF()
    {
        System.out.print("Enter a friend's name to be removed: ");
        int i = 0;
        boolean found = false;

        while (i < myBFFs.size() && !found)
        {
            if(firstName.equalsIgnoreCase(myBFFs.get(i).getFirstName()) && lastName.equalsIgnoreCase(myBFFs.get(i).getLastName()))
            {
                found = true;
            }
            else
                i++;
        }      
    }    
}

这是我的Helper类,我在使用removeABFF方法时遇到问题,仍然需要从头开始创建一个changeABFF方法。接下来是我的主要课程:

  

package bestfriends;
import java.util.Scanner;

public class BFFPhoneBook 
{

    public static void main(String args[])
    { 
        int menuOption = 0;
        Scanner keyboard = new Scanner(System.in);
        BFFHelper myHelper = new BFFHelper();

        do
        {

            System.out.println("1.  Add a Friend");
            System.out.println("2.  Change a Friend");
            System.out.println("3.  Remove a Friend");
            System.out.println("4.  Display a Friend");
            System.out.println("5.  Exit");

            System.out.print("Enter your selection: ");
            menuOption = keyboard.nextInt();

            switch (menuOption)
            {
                case 1:
                    myHelper.addABFF();
                    break;
                case 2:
                    myHelper.changeABFF();
                    break;
                case 3:
                    myHelper.removeABFF();
                    break;  
                case 4:
                    myHelper.displayABFF();
                    break;
                case 5:
                    break;
                default:
                    System.out.println("Invalid option.  Enter 1 - 5");

            }



        } while (menuOption != 5);
    }   
}

这是我上一堂课:

  

package bestfriends;

public class BestFriends {

    private static int friendNumber = 0;
    private int friendIdNumber;
    String firstName;
    private String lastName;
    private String nickName;
    private String cellPhoneNumber;


    public BestFriends (String aFirstName, String aLastName, String aNickName, String aCellPhone)
    {
        firstName = aFirstName;
        lastName = aLastName;
        nickName = aNickName;
        cellPhoneNumber = aCellPhone;
        friendIdNumber = ++friendNumber;
       // friendIdNumber = friendNumber++;


    }

    public boolean equals(Object aFriend)
    {
        if (aFriend instanceof BestFriends )
        {
            BestFriends myFriend = (BestFriends) aFriend;
            if (lastName.equals(myFriend.lastName) && firstName.equals(myFriend.firstName))
               return true;
            else
               return false;
        }
        else
            return false;



    }
    public String getFirstName()
    {
        return firstName;
    }

    public String getLastName()
    {
        return lastName;
    }

    public String getNickName()
    {
        return nickName;
    }

    public String getCellPhone()
    {
        return cellPhoneNumber;
    }

    public int getFriendId()
    {
        return friendIdNumber;
    }

    public String toString()
    {
        return friendIdNumber + ". " + firstName + " (" + nickName + ") " +  lastName + "\n" + cellPhoneNumber + "\n";
    }  
}

2 个答案:

答案 0 :(得分:1)

  

为了探索和操纵一个arraylist,使用iterator   该对象缺少Setters

声明变量

tf.train.batch_join()

删除

ArrayList<BestFriends> myBFFs;
Scanner keyboard = new Scanner(System.in);
BestFriends best;
public BFFHelper()
{
    myBFFs = new ArrayList<BestFriends>();
    best= new BestFriends();
}

更新

public void removeABFF()
{
    System.out.print("Enter a friend's name to be removed: ");
    String name= keyboard.next().toLowerCase();// entry name to be removed
    Iterator<BestFriends> nameIter = myBFFs.iterator(); //manipulate ArrayList
    while (nameIter.hasNext()){
        best = nameIter.next(); // obtained object list
        if (best.getNickName().trim().toLowerCase().equals(name)){ // if equals name
            nameIter.remove(best); // remove to arraylist
        }           
    }    
} 

答案 1 :(得分:0)

在您的删除方法中,您不接受任何值的输入

public void removeABFF()
{
    System.out.print("Enter a friend's name to be removed: ");
    int i = 0;
    boolean found = false;

    while (i < myBFFs.size() && !found)

    ....

当您使用firstNamerlastName查找对象时,您需要这些值

    System.out.println("Enter a first name: ");
    String firstName = keyboard.next();
    System.out.println("Enter a last name: ");
    String lastName = keyboard.next();