Java 如何从ArrayList中获取最大值并显示信息

Java 如何从ArrayList中获取最大值并显示信息,java,Java,我需要从ArrayList中找到最高的价值(销售额),并显示每个季度哪个商店的销售额最高 BufferedReader inputFile = null; PrintWriter outputFile = null; BookStore bs; ArrayList <BookStore> data = new ArrayList(); try { inputFi

我需要从ArrayList中找到最高的价值(销售额),并显示每个季度哪个商店的销售额最高

        BufferedReader inputFile = null;
        PrintWriter outputFile = null;

        BookStore bs;
        ArrayList <BookStore> data = new ArrayList();

        try
        {
            inputFile = new BufferedReader (new FileReader ("bookstoresales.txt"));
            outputFile = new PrintWriter (new FileWriter ("bookstoreoutput.txt"));

            String indata = null;

            while ((indata = inputFile.readLine()) != null)
            {
                System.out.println (indata);
                StringTokenizer st = new StringTokenizer (indata, ",");

                String storeBranch = st.nextToken();
                String storeAddress = st.nextToken();
                double jan = Double.parseDouble (st.nextToken());
                double feb = Double.parseDouble (st.nextToken());
                double mac = Double.parseDouble (st.nextToken());
                double apr = Double.parseDouble (st.nextToken());
                double may = Double.parseDouble (st.nextToken());
                double jun = Double.parseDouble (st.nextToken());
                double jul = Double.parseDouble (st.nextToken());
                double aug = Double.parseDouble (st.nextToken());
                double sep = Double.parseDouble (st.nextToken());
                double oct = Double.parseDouble (st.nextToken());
                double nov = Double.parseDouble (st.nextToken());
                double dec = Double.parseDouble (st.nextToken());

                bs = new BookStore (storeBranch, storeAddress, jan, feb, mac, apr, may, jun, jul, aug, sep, oct, nov, dec);

                data.add(bs);
            }

            outputFile.println ("\n\n=================================== The Highest Bookstore Sales Per Quarter  ===================================");


            for (int i=0; i<data.size(); i++)
            {
                //This is where I got stucked.
            }

            outputFile.println ("\nQuarter 1 : The Highest sales is Bookstores : " + //getbranch name from the highest sales  + " with value of + //the highest value");


            inputFile.close();
            outputFile.close();
        }

        catch (FileNotFoundException fnf)
        {
            System.out.println ("File not found!");
        }

        catch (IOException ioe)
        {
            System.out.println (ioe.getMessage());
        }
    }
}
BufferedReader输入文件=null;
PrintWriter outputFile=null;
书店学士;
ArrayList数据=新的ArrayList();
尝试
{
inputFile=new BufferedReader(新文件阅读器(“bookstoresales.txt”);
outputFile=新的PrintWriter(新的FileWriter(“bookstoreautput.txt”);
字符串indata=null;
而((indata=inputFile.readLine())!=null)
{
System.out.println(indata);
StringTokenizer st=新的StringTokenizer(indata,“,”);
字符串storeBranch=st.nextToken();
字符串storeAddress=st.nextToken();
double jan=double.parseDouble(st.nextToken());
double feb=double.parseDouble(st.nextToken());
double mac=double.parseDouble(st.nextToken());
double apr=double.parseDouble(st.nextToken());
double may=double.parseDouble(st.nextToken());
double jun=double.parseDouble(st.nextToken());
double jul=double.parseDouble(st.nextToken());
double-aug=double.parseDouble(st.nextToken());
double sep=double.parseDouble(st.nextToken());
double oct=double.parseDouble(st.nextToken());
double nov=double.parseDouble(st.nextToken());
double dec=double.parseDouble(st.nextToken());
bs=新书店(storeBranch,storeAddress,一月、二月、mac、四月、五月、六月、七月、八月、九月、十月、十一月、十二月);
数据。添加(bs);
}
outputFile.println(“\n\n==========================================================================================================================================================================================================”;

对于(int i=0;i创建一个名为
max
的变量。在循环中,如果列表项高于
max
,则将
max
设置为该项

BookStore max = data.get(0);
for (BookStore bs : data) {
    if (bs.sales > max.sales)  {
        max = bs;
    }
}

另一种方法是使用:

使用Java 8:

BookStore max = 
    data.stream()
        .max(Comparator.comparingDouble(bs -> bs.getJan() + ... + bs.getDec()))
        .get();

如果你手工做这个,你会怎么做?你会从顶部开始,向下看列表,记住你所见过的最大值。考虑使用数组<代码>双[ ]月=新的双[ 12 ]。
而不是让所有这些双变量都通过了解如何循环数组列表来启动。建议尝试使用资源,而不是手动关闭inputFile和outputFile。此外,
bs
可能应该在while循环中声明。
BookStore.sales
不是每个季度的总数。这将给出每月最多。
Collections.max(data);
BookStore max = 
    data.stream()
        .max(Comparator.comparingDouble(bs -> bs.getJan() + ... + bs.getDec()))
        .get();