我有一个arraylist domainDailyData
将存储域名/应用数据,请参阅以下内容
MIT/MIT0010, MIT/MIT0010, MIT/MIT0011, MBO/MBO0010, MIT/MIT0010, MIT/MIT0010, MIT/MIT0010, MIT/MOF0011, MIT/MIT0010]
我希望我的程序可以计算这些元素在arraylist中发生了多少次,之后我将删除计数元素以避免再次计数并将提取的信息存储到另一个arraylist call domainMonthlyData1
。但我发现domainDailyData
内的部分数据未被删除。
这是我的源代码
< / p>
System.out.println("The domainDailyData before is :"+domainDailyData);
int count11 = 0;
for(int c = domainDailyData.size()-1;c>=0;c--)
{
String domainAndApps1 = domainDailyData.get(c); //get the domain/apps out
count11 = Collections.frequency(domainDailyData,domainAndApps1); // counting the number of occurrence
System.out.println("Count of mail is :"+count11);
String [] splittedData = domainAndApps1.split(splitBy); // the domain/apps
// System.out.println(Emaildata1);
String domain1 = splittedData [0];
String apps1 = splittedData[1];
// System.out.println("The extracted domain is :"+domain1); // display domain
// System.out.println("The extracted apps is :"+apps1); // display apps
domainMonthlyData1.add(domain1);
domainMonthlyData1.add(apps1); //add into arraylist
domainMonthlyData1.add(String.valueOf(count11)); //add into arraylist
Iterator<String> it1 = domainDailyData.iterator();
while(it1.hasNext()) // remove the counted domain/apps
{
String domainAndApps2 = it1.next();
if(domainAndApps1.equals(domainAndApps2))
{
it1.remove();
c--;
}
}
}
System.out.println("The domainDailyData after is :"+domainDailyData);
System.out.println("The domainMonthlyData1 is :"+domainMonthlyData1);
这是我程序中的样本输出
The domainDailyData before is :MIT/MIT0010, MIT/MIT0010, MIT/MIT0011, MBO/MBO0010, MIT/MIT0010, MIT/MIT0010, MIT/MIT0010, MIT/MOF0011, MIT/MIT0010]
Count of mail is :2
Count of mail is :5
The domainDailyData after is :[MIT/MIT0011, MIT/MOF0011]
The domainMonthlyData1 is :[MBO, MBO0010, 2, MIT, MIT0010, 5]
答案 0 :(得分:1)
您可能始终获取列表的第一个元素来进行计数。由于它将在稍后的Iterator
循环中删除,您可以安全地在下一次迭代中再次获取第一个元素,以便计算下一个元素。
因此,您的代码如下所示。
while (domainDailyData.size() > 0)
{
String domainAndApps1 = domainDailyData.get(0);
// your logic
Iterator<String> it1 = domainDailyData.iterator();
while(it1.hasNext()) // remove the counted domain/apps
{
String domainAndApps2 = it1.next();
if(domainAndApps1.equals(domainAndApps2))
{
it1.remove();
}
}
}