java - 查找数组列表中的最大值

标签 java collections arraylist

在我编写的这个程序中,我必须打印出在商店消费最多的顾客的姓名。我需要帮助在数组列表中搜索花费最多的客户。

 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/

相关文章:

java - 如何在java中合并 map 列表?

jsf - 通过ui发送数组列表 :param to JSF ui:composition template

arraylist - 如何在 ArrayList 的 textArea 中设置文本?

java - 如何使用 type=redirect 重定向到另一个 Action 时传递参数?

java - 如何设置关闭按钮的 Action ?

swift - 创建一个 Swift 字典子类?

java - 当 ArrayList 在模型中更新时 JList 应该在 View 中更新

java - 如何在java中读取带有属性的xml文件?

java - 应用于没有 try-catch bug 的函数的 JVM 优化是否会引发异常?

java - 为什么我会使用 list.clear()