访问和修改数组列表

时间:2016-02-05 09:09:56

标签: java arrays arraylist

我想创建一个程序,允许用户删除具有指定工资单号码的员工。

该程序应如下运作:

程序显示arraylist中的当前员工,然后要求用户输入他们想要删除的工资单号码。然后,用户输入三名工作人员之一的工资单编号,然后按Enter键。按Enter键后,程序应从阵列列表中删除该特定工作人员并再次显示整个列表(错过了他们已明显删除的工作人员)。如果他们不希望删除任何工资单编号,则工资单编号条目应为0,然后应再次显示该列表的内容。

我有以下文件和结构:

---编辑---:ArrayListTest.java

import java.util.*;

import personnelPackage.Personnel;

public class ArrayListTest
{
    static Scanner keyboard = new Scanner(System.in);

    public static void main(String[] args)
    {
        long searchQuery;

        ArrayList<Personnel> staffList = new ArrayList<Personnel>();
        Personnel[] staff =
            {new Personnel(123456,"Smith","John"),
             new Personnel(234567,"Jones","Sally Ann"),
             new Personnel(999999,"Black","James Paul")};

        for (Personnel person:staff)
            staffList.add(person);

        do
        {
            showDisplay(staffList);

            System.out.print("\nPlease enter a payroll number to search: ");
            searchQuery = keyboard.nextLong();

            searchForPayrollNumber(staffList, searchQuery);


        }while(!(searchQuery == 0));


    }

    private static void showDisplay(ArrayList<Personnel> staffList)
    {
        System.out.print("\n------------- CURRENT STAFF LIST -------------\n");
        for (Personnel person : staffList)
        {
            System.out.println("Payroll number: " + person.getPayNum());
            System.out.println("Surname: " + person.getSurname());
            System.out.println("First name(s): " + person.getFirstNames() + "\n");
        }
    }

    public static void searchForPayrollNumber(ArrayList<Personnel> staffList, long searchQuery)
    {
        long index = staffList.indexOf(searchQuery);;

        for (Personnel person: staffList)
        {
            if (person.getPayNum() == searchQuery)
            {               
                System.out.print("\n------------- Staff member found and removed! -------------");
                System.out.println("\n\nFirst Name(s): " + person.getFirstNames());
                System.out.println("\nSurname: " + person.getSurname());
                System.out.print("\n-----------------------------------------------");

                staffList.remove(index);
                return;
            }
        }

        System.out.print("\n------------- No staff members found. Program terminated -------------");
        return;

    }

}

Personnel.java (在自己的名为personnelPackage的包中)

package personnelPackage;

public class Personnel
{
    private long payrollNum;
    private String surname;
    private String firstNames;

    public Personnel(long payrollNum, String surname, String firstNames)
    {
        this.payrollNum = payrollNum;
        this.surname = surname;
        this.firstNames = firstNames;
    }

    public long getPayNum()
    {
        return payrollNum;
    }

    public String getSurname()
    {
        return surname;
    }

    public String getFirstNames()
    {
        return firstNames;
    }

    public void setSurname(String newName)
    {
        surname = newName;
    }
}

如果我输入工资单编号,我的分钟会识别第一名工作人员。任何其他工作人员都没有找到&#39;。我哪里错了?如何从阵列中删除特定的工资单编号,并要求用户输入其他输入(直到数组为&#34;为空&#34;)。

4 个答案:

答案 0 :(得分:3)

删除return阻止中的else声明。

    for (Personnel person: staff)
    {
        if (person.getPayNum() == searchQuery)
        {
            System.out.print("\n------------- Staff member found! -------------");
            System.out.println("\n\nFirst Name(s): " + person.getFirstNames());
            System.out.println("Surname: " + person.getSurname());
            return;
        }
        else
        {
            System.out.print("\n------------- No staff members found -------------");
            return;  // Remove this one.
        }
    }

通过在条件的每个分支中使用return语句,此循环将只执行一次。

请注意,您可能还想将else的内容移到循环之外,否则每个被检查的人都会被告知“没有找到工作人员”。

    for (Personnel person: staff)
    {
        if (person.getPayNum() == searchQuery)
        {
            System.out.print("\n------------- Staff member found! -------------");
            System.out.println("\n\nFirst Name(s): " + person.getFirstNames());
            System.out.println("Surname: " + person.getSurname());
            return;
        }
    }
    System.out.print("\n------------- No staff members found -------------");

修改有关删除元素的问题:

您当前的代码无效:indexOf将返回-1,因为该列表包含Personnel个实例,而非工资单号。如果这确实有效,那么您就不需要再次迭代列表来再次找到该元素:您已经知道它在列表中的位置。

要进行有效删除,您需要使用Iterator;增强的for循环不起作用。粗略的实现如下:

Iterator<Personnel> it = staff.iterator();
while (it.hasNext()) {
  Personnel person = it.next();
  if (person.getPayNum() == searchQuery)

{         // ...打印等         it.remove();         返回;       }     }

答案 1 :(得分:1)

您必须从搜索中删除return;语句。这指示jvm从方法返回。

 if (person.getPayNum() == searchQuery)
            {
                System.out.print("\n------------- Staff member found! -------------");
                System.out.println("\n\nFirst Name(s): " + person.getFirstNames());
                System.out.println("Surname: " + person.getSurname());
                // return; // This return is optional, won't interfere with your business. 
            }
            else
            {
                System.out.print("\n------------- No staff members found -------------");
                // return; // This one has to be removed for proper function.
            }

答案 2 :(得分:1)

问题是你的if / else中的“return”。当你在else中返回时,main方法停止。因此,如果搜索到的人不是第一个,则该方法结束。只需删除else子句并将打印放在for循环之外。

    for (Personnel person: staff)
    {

        if (person.getPayNum() == searchQuery)
        {
            System.out.print("\n------------- Staff member found! -------------");
            System.out.println("\n\nFirst Name(s): " + person.getFirstNames());
            System.out.println("Surname: " + person.getSurname());
            return;
        }



    }
    System.out.print("\n------------- No staff members found -------

答案 3 :(得分:0)

在上面的代码中,您在{else} return中阻止。从else块中删除return语句,以便循环执行。这里循环不是激励因为在elese块中然后是return语句。

    public static int searchForPayrollNumber(Personnel[] staff)
    {
int i=0;
        long searchQuery;

        System.out.print("\nPlease enter a payroll number to search: ");
        searchQuery = keyboard.nextLong();

        for (Personnel person: staff)
        {

            if (person.getPayNum() == searchQuery)
            {
                System.out.print("\n------------- Staff member found! -------------");
                System.out.println("\n\nFirst Name(s): " + person.getFirstNames());
                System.out.println("Surname: " + person.getSurname());
                i++;    
                break;
            }

        }
return i;

    }

在上面的函数检查方法返回值。如果它是0则没有找到记录。