问题描述
需要梳理的基础上的日期列csv文件。这是masterRecords数组列表的样子
Need to sort a csv file based on the date column. This is how the masterRecords array list looks like
GBEP-1-2-4,FRAG,PMTypeEthernet,NEND,TDTN,15-MIN,Dec 15 2014 - 07:15:00 AM MYT,+0,COMPL
GBEP-1-2-1,FRAG,PMTypeEthernet,NEND,TDTN,15-MIN,Dec 15 2014 - 07:00:00 AM MYT,+0,COMPL
GBEP-2-2-1,FRAG,PMTypeEthernet,NEND,TDTN,15-MIN,Dec 15 2014 - 07:30:00 AM MYT,+0,COMPL
我需要从我创建了一个code梳理出来日七时15分00秒,七点30分00秒,等它基于梳理:
I need to sort it out based from the date 07:15:00, 07:30:00, etc. I created a code to sort it out:
// Date is fixed on per 15min interval
ArrayList<String> sortDate = new ArrayList<String>();
sortDate.add(":00:");
sortDate.add(":15:");
sortDate.add(":30:");
sortDate.add(":45:");
BufferedWriter bw = new BufferedWriter(new FileWriter(tempPath + filename));
for (int k = 0; k < sortDate.size(); k++) {
String date = sortDate.get(k);
for (int j = 0; j < masterRecords.size(); j++) {
String[] splitLine = masterRecords.get(j).split(",", -1);
if (splitLine[10].contains(date)) {
bw.write(masterRecords.get(j) + System.getProperty("line.separator").replaceAll(String.valueOf((char) 0x0D), ""));
masterRecords.remove(j);
}
}
}
bw.close();
您可以从上面它会看到环通的第一阵列(sortDate)和环路直通再次第二个数组这是masterRecord,写在一个新的文件上。这似乎为新的文件整理出来进行工作,但我发现我的masterRecord有10000条记录,但创建一个新文件记录收缩到5000后,林假设它如何从主列表中删除的记录。任何人都知道这是为什么?
You can see from above it will loop thru a first array (sortDate) and loop thru again on the second array which is the masterRecord and write it on a new file. It seems to be working as the new file is sorted out but I notice that my masterRecord has 10000 records but after creating a new file the record shrinks to 5000, Im assuming its how I remove the records from the master list. Anyone knows why?
推荐答案
是不是安全地删除一个循环内的项目。
您对迭代器遍历数组,例如:
Is not safe to remove an item inside of a loop.You have to iterate array over Iterator, for example:
List<String> names = ....
Iterator<String> i = names.iterator();
while (i.hasNext()) {
String s = i.next(); // must be called before you can call i.remove()
// Do something
i.remove();
}
该文件说:
此类的iterator和listIterator方法返回的迭代器是快速失败的:如果列表随时结构上修改迭代器创建之后,以任何方式,除了通过迭代器自身的remove或add方法,迭代器将抛出一个ConcurrentModificationException的。因此,在并发的修改,迭代器很快就会完全失败,而不是在将来不确定的时间任意冒险,不确定性的行为。
这篇关于基于列的日期排序java的csv文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!