Java 方法查找对象将添加到的ArrayList索引
我有一个Java 方法查找对象将添加到的ArrayList索引,java,object,arraylist,Java,Object,Arraylist,我有一个ArrayList,它使用customer类填充客户信息。在我的addCustomerRecord方法中,我在addCustomerRecord方法中调用findAddIndex,以便在显示数据之前对输入的数据进行排序。这是我的代码,不要介意filewhich方法,我不使用它 public class CustomerDemo { //arrayList of customer objects public static ArrayList<Customer>
ArrayList
,它使用customer
类填充客户信息。在我的addCustomerRecord
方法中,我在addCustomerRecord
方法中调用findAddIndex
,以便在显示数据之前对输入的数据进行排序。这是我的代码,不要介意filewhich
方法,我不使用它
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;
}
}
公共类CustomerDemo
{
//客户对象的arrayList
public static ArrayList customerAL=new ArrayList();
公共静态void main(字符串[]args)
{
//保留菜单选项
字符串menuChoice=“”;
扫描仪kb=新扫描仪(System.in);
System.out.println(“要添加记录,请按'a':\n”
+“要显示所有记录,请按'D':\n”
+“要退出,请按'Q':\n”);
//循环启动读取
menuChoice=kb.nextLine();
//使输入不区分大小写
menuChoice=menuChoice.toLowerCase();
做
{
如果(menuChoice.equals(“a”))
addCustomerRecord(kb);
else if(menuChoice.equals(“d”))
{
displayCustomerRecords();
}
else if(menuChoice.equals(“q”))
{
System.out.println(“程序退出…”);
系统出口(0);
}
其他的
{
System.out.println(“输入不正确。请重新输入有效的输入:\n”);
menuChoice=kb.nextLine();
menuChoice=menuChoice.toLowerCase();
}
System.out.println(“要添加记录,请按'a':\n”
+“要显示所有记录,请按'D':\n”
+“要退出,请按'Q':\n”);
menuChoice=kb.nextLine();
menuChoice=menuChoice.toLowerCase();
}而(menuChoice.equals(“a”)| | menuChoice.equals(“d”)| | menuChoice.equals(“q”);
kb.close();
}
/*公共静态void displayCustomerRecords()
{
System.out.println();
对于(int i=0;i
看起来你正在重新发明轮子,威廉
用以下代码替换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;
}
private static Comparator Comparator=Comparator.comparating(客户::getLastName)
.Then比较(客户::getFirstName)
.Then比较(客户::getCustID)
.然后比较(客户::getPhoneNumber);
公共静态int FindAddress(客户客户)
{
int位置=0;
如果(!customerAL.isEmpty())
{
对于(客户客户列表:CustomerAll)
{
if(comparator.compare(customerInList,cust)>0){
打破
}
位置++;
}
}
返回位置;
}
我们使用Java的增强for循环遍历数组,并使用Java8声明的比较器(我相信这是这个赋值的关键)比较对象
如果您可以研究接口并在Customer
类中实现它,这将是一个好主意。这样,您只需调用customerInList.compareTo(cust)
即可比较这两个对象
如前所述,这不是一个好的实践,不应该在生产代码中使用。看起来您正在重新发明轮子 用以下代码替换
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;
}
private static Comparator Comparator=Comparator.comparating(客户::getLastName)
.Then比较(客户::getFirstName)
.Then比较(客户::getCustID)
.然后比较(客户)