我需要從ArrayList中找到最高價值(銷售額),並顯示哪家商店的每個季度銷售額最高。如何從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());
}
}
}
現在我需要獲得並顯示商店名稱以及最高的銷售價值。對於ArrayList,我感到很困惑。
如果你正在做這個manu盟友,你會怎麼做?你會從頂部開始往下看清單,記住迄今爲止看到的最大價值。 – dimo414
考慮使用一個數組''double [] months = new double [12]'而不是所有那些雙變量 – Sid
首先確定如何遍歷ArrayList。 – mdnghtblue