我有一个CSV文件,我以Java的ArrayList存储。下面是我跑在ArrayList中查找索引
public class StockData {
private ArrayList<StockRecord> records;
public StockData() {
records = new ArrayList<StockRecord>();
}
//reads the file from the folder
public void loadPriceData(String filepath) {
try {
Scanner scanner = new Scanner(new FileReader(filepath));
String line;
StockRecord record;
scanner.nextLine();
while (scanner.hasNextLine()) {
line = scanner.nextLine();
String[] results= line.split(",");
double open = Double.parseDouble(results[1]);
double high = Double.parseDouble(results[2]);
double low = Double.parseDouble(results[3]);
double close = Double.parseDouble(results[4]);
double volume = Double.parseDouble(results[5]);
double adjClose = Double.parseDouble(results[6]);
//create the record
record = new StockRecord(results[0], open, high, low, close, volume, adjClose);
records.add(record);
}
scanner.close();
} catch (Exception e) {
System.out.println("Error: " + e.getMessage());
}
}
}
代码正常不过,我有这个项目的下一部分故障代码。 数据实施例
- 日期打开高低关闭卷形关闭
- 6/10/2011 128.85 128.93 127.26 127.6 126.97 238629400
- 2011年6月13日127.89 128.24 127.05 127.7 127.07 207599800
- 6 /二千零十一分之十四128.87 129.77 128.82 129.32 160570400 128.68
- 2011/6/15 128.24 129.3 126.68 127.02 300958000 126.39
- 2011/6/16 127.06 127.97 126.32 127.3 308032800 126.67
我试图打印出日期范围的AdjClose价格。例如2011年6月13日至2016年6月15日之间。看起来我需要首先找到日期的索引,然后打印价格记录。
我该怎么做?我不知道从哪里开始。我是否首先需要将日期保存在自己的列表/数组中并使用它来查找索引?
感谢
您将日期存储为字符串,这将使得它们难以比较。您可以将它们存储为java.time.LocalDate。 –