在我编写的这个程序中,我必须打印出在商店消费最多的顾客的姓名。我需要帮助在数组列表中搜索花费最多的客户。
package bestcustomer;
import java.util.*;
/**
*
* @author muf15
*/
public class BestCustomer {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
ArrayList<Double> sales = new ArrayList<Double>();
ArrayList<String> names = new ArrayList<String>();
double salesAmount;
System.out.println("Enter the sales for first customer: ");
salesAmount = in.nextDouble();
while(salesAmount !=0)
{
sales.add(salesAmount);
System.out.println("Enter customers name");
names.add(in.next());
System.out.println("Enter the next sales amount, 0 to exit: ");
salesAmount = in.nextDouble();
}
String bestCustomer = nameOfBestCustomer(sales, names);
}
public static String nameOfBestCustomer(ArrayList<Double> sales,
ArrayList<String> customers)
{
String name = "";
double maxSales;
return name;
}
}
最佳答案
您应该将这两个字段包装在一个名为 Customer
的类中,然后
使用Collections.max();
Collections.max(yourCollection, customComparator);
关于java - 查找数组列表中的最大值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22723465/