【问题标题】:Element inside ArrayList fail to removeArrayList 中的元素无法删除
【发布时间】:2018-04-07 11:06:53
【问题描述】:

我有一个数组列表domainDailyData,它将存储域/应用程序数据,请参考以下

MIT/MIT0010, MIT/MIT0010, MIT/MIT0011, MBO/MBO0010, MIT/MIT0010, MIT/MIT0010, MIT/MIT0010, MIT/MOF0011, MIT/MIT0010]



我希望我的程序可以计算这些元素在 arraylist 中发生了多少次,之后我将删除计数的元素以避免它再次被计数并将提取的信息存储到另一个 arraylist 调用中domainMonthlyData1。但是我发现domainDailyData里面的一些数据没有被删除。

这是我的源代码

               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]

【问题讨论】:

  • 虽然你使用Iterator删除了ArrayList,但是你的外层循环仍然使用删除前的old索引。
  • @Alex 怎么修改呢?我在stackoverflow中搜索解决方案并来到这个阶段
  • @Berger 感谢您的链接,我会看看它。之间,在我来到这个阶段之前,我也在stackoverflow中搜索了解决方案

标签: java arraylist


【解决方案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();
        }
    }
}

【讨论】:

  • 非常感谢,很有帮助
猜你喜欢
  • 2013-02-24
  • 2016-07-23
  • 1970-01-01
  • 2017-09-12
  • 2014-08-28
  • 2012-04-25
  • 1970-01-01
相关资源
最近更新 更多