查找将添加对象的ArrayList索引的方法

时间:2017-10-18 03:47:47

标签: java object arraylist

我有ArrayList使用Customer课程填写了客户信息。在我的addCustomerRecord方法中,我在findAddIndex方法中调用addCustomerRecord,因此输入的数据将在显示数据之前进行排序。这是我的代码,不介意fileWhatever方法,我不使用它。

public class CustomerDemo
{
    //arrayList of customer objects
    public static ArrayList<Customer> customerAL = new ArrayList<>();



    public static void main (String[] args)
    {        
        //to hold menu choice
        String menuChoice = "";

        Scanner kb = new Scanner(System.in);

        System.out.println("To add a record press 'A': \n"
                + "to display all records press 'D': \n"
                + "to exit press 'Q': \n");

        //loop priming read
        menuChoice = kb.nextLine();

        //make input case insensitive
        menuChoice = menuChoice.toLowerCase();


        do
        {
            if(menuChoice.equals("a"))
                addCustomerRecord(kb);
            else if(menuChoice.equals("d"))
            {
                displayCustomerRecords();
            }
            else if(menuChoice.equals("q"))
            {
                System.out.println("Program exiting..");
                System.exit(0);
            }

            else
            {
                System.out.println("incorrect entry. Please re-enter a valid entry: \n");
                menuChoice = kb.nextLine();
                menuChoice = menuChoice.toLowerCase();
            }

            System.out.println("To add a record press 'A': \n"
                    + "to display all records press 'D': \n"
                    + "to exit press 'Q': \n");
            menuChoice = kb.nextLine();
            menuChoice = menuChoice.toLowerCase();
        }while(menuChoice.equals("a") || menuChoice.equals("d") || menuChoice.equals("q"));


        kb.close();
    }

   /*     public static void displayCustomerRecords()
    {
        System.out.println();
        for (int i = 0; i < customerAL.size(); ++i)
        {
            System.out.printf("%-15s", customerAL.get(i).getLastName());
            System.out.printf("%-15s", customerAL.get(i).getFirstName());
            System.out.printf("%-6s", customerAL.get(i).getCustID());
            System.out.printf("%15s\n", customerAL.get(i).getPhoneNumber());
        }
        System.out.println();
    }


    /**
     * prompts to enter customer data and mutator methods called 
     * with a Scanner object passed as an argument to set data
     * @param location index position of where the element will be added. 
     * @param kb a Scanner object to accept input
     */

    public static void addCustomerRecord(Scanner kb)
    {

        Customer currentCustomerMemoryAddress = new Customer();

        System.out.println("Enter first name: \n");
        String fName = kb.nextLine();
        currentCustomerMemoryAddress.setFirstName(fName);

        System.out.println("Enter last name: \n");
        String lName = kb.nextLine();
        currentCustomerMemoryAddress.setLastName(lName);

        System.out.println("Enter customer phone number: \n");
        String pNum = kb.nextLine();
        currentCustomerMemoryAddress.setPhoneNumber(pNum);

        System.out.println("Enter customer ID number: \n");
        String ID = kb.nextLine();
        currentCustomerMemoryAddress.setCustID(ID);

        int addLocation = findAddLocation(currentCustomerMemoryAddress);

        customerAL.add(addLocation, currentCustomerMemoryAddress);

        currentCustomerMemoryAddress = null;
    }

    public static int findAddLocation(Customer cust)
    {
        int location = 0;

        if(!customerAL.isEmpty())
        {
            for(int i = 0; i < customerAL.size(); i++)
            {
                //Stumped here
            }
        }
        else
            return location;
        return location;
    }
}    

1 个答案:

答案 0 :(得分:2)

看起来你在这里重新发明轮子威廉

使用以下代码替换displayCustomerRecords的代码:

public static void displayCustomerRecords()
    {
        System.out.println();
        customerAL.stream().map(c -> String.format("%-15s%-15s%-6s%15s\n",
                c.getLastName(), c.getFirstName(), c.getCustID(), c.getPhoneNumber()))
                .sorted()
                .forEach(System.out::print);
        System.out.println();
    }

<强>更新

考虑到您的评论,您可以通过以下方式替换findAddLocation方法:

private static Comparator<Customer> comparator = Comparator.comparing(Customer::getLastName)
                .thenComparing(Customer::getFirstName)
                .thenComparing(Customer::getCustID)
                .thenComparing(Customer::getPhoneNumber);


public static int findAddLocation(Customer cust)
{

    int location = 0;

    if(!customerAL.isEmpty())
    {
        for(Customer customerInList : customerAL)
        {
            if(comparator.compare(customerInList, cust) > 0) {
                break;
            }
            location++;
        }
    }
    return location;
}

我们使用Java增强的for循环遍历数组,并使用Java 8声明的比较器比较对象(我相信这是此任务的关键)。

如果您可以查看Comparable界面并在Customer课程中实施它,那将是一个好主意。这样你就可以简单地调用customerInList.compareTo(cust)来比较两个对象。

如前所述,是一种良好做法,不应在生产代码中使用。