在innnermost for循环中遍历元素

时间:2014-05-01 17:00:08

标签: java

我陷入了一个非常奇怪的情况..那就是有两个嵌套的for循环和内部最多的循环我正在测试一些条件,如果那个条件变为真,那么在那个状态我出来的内部为循环同时从该索引本身的aray列表中删除元素,通过使用continue语句但现在我想要同样的内部最多for循环继续为arraylist的下一个遗留元素但是现在它没有发生...实际上现在发生了rite,如果条件在内部for循环中变为true,那么该索引处的元素将从araylist中删除 然后流程转到继续语句,再次将流程转移到内部for循环的开始,这是完美但从那时开始,现在从内部for循环的流程应该进入内部循环,这不会发生

根据我的分析,当我在从内部for循环中的条件变为true时创建问题的情况下从列表中删除元素  请建议,因为继续再次收回流向内循环的乞讨然后从那里开始流程内部循环的结束不应该发生它应该进入for循环以备用arraylist元素,请指教

//outermost for loop
for (File file : updatedFile) {

   //fetching data into the list 
   List<teetobjects> abcobjects = (List<teetobjects>) feedObjectsGetter.getObjects(file);

   //inner most for loop begains
   for (teetobjects pecdeedObject : abcobjects) {

      //checking some condition for each element in the inner most element ....
      if (st.contains(s1)) {

         // if the condition is true then removing the element of the 
         // aaraylist named abcobjects within inner for loop
         abcobjects.remove(recFeedObject);
         continue;
      }

   }
}

3 个答案:

答案 0 :(得分:1)

您无法使用&#34;从每个&#34;中删除正在迭代的容器中的对象。 for循环的版本:如果从正在迭代的集合中删除对象,则会得到ConcurrentModificationException

为了实现此目的,您需要明确使用ListIterator<T>,并在其上调用remove

List<teetobjects> abcobjects = (List<teetobjects>)feedObjectsGetter.getObjects(file);
//inner most for loop begins
ListIterator<teetobjects> iter = abcobjects.listIterator();
while (iter.hasNext()) {
    teetobjects pecdeedObject = iter.next();
    //checking some condition for each element in the inner most element ....
    if (st.contains(s1)) {
         // if the condition is true then removing the element of the 
         // aaraylist named abcobjects within inner for loop
         iter.remove();
         continue;
    }
}

答案 1 :(得分:0)

CopyOnWriteArrayList是一个更好的类而不是List,因为在修改列表时同时读取写入会产生ConcurrentModicationException。没有必要使用continue,因为它将迭代剩余的元素

答案 2 :(得分:0)

根据评论进行更新

没有区别只是将List替换为CopyOnWriteArrayList。

List<teetobjects> abcobjects = new CopyOnWriteArrayList<teetobjects>(); 

在您的情况下尝试

List<teetobjects> abcobjects = new CopyOnWriteArrayList<teetobjects>((List<teetobjects>) feedObjectsGetter.getObjects(file));

完整代码

//outermost for loop
for (File file : updatedFile) {

   //fetching data into the list 
   List<teetobjects> abcobjects = (List<teetobjects>) feedObjectsGetter.getObjects(file);

   /**
    * ArrayList are not threadsafe. Mutative operations like remove() method inside a for-each loop will result in ConcurrentModificationException.
    * One option is to use iterator as given in solution by dasblinkenlight. But iterators are more verbose and kind of outdated (just my opinion :) )
    * An alternative is CopyOnWriteArrayList. It is a thread-safe variant of ArrayList in which all mutative operations (add, set, and so on) 
    * are implemented by making a fresh copy of the underlying array. All these are underlying and the user may not be aware about this. 
    * So you could use it same like ArrayList. Just like  ConcurrentHashMap for HashMap.
    * 
    * So once you have the List<teetobjects> you can convert it to CopyOnWriteArrayList and the do mutative looping
    */
   List<teetobjects> concurrentAbcobjects = new CopyOnWriteArrayList<teetobjects>(abcobjects);

   //You could also initialize this as directly passing as a constructor argument from feedObjectsGetter as
   //List<teetobjects> abcobjects = new CopyOnWriteArrayList<teetobjects>((List<teetobjects>) feedObjectsGetter.getObjects(file));

   //inner most for loop begains
   // once you have the concurrent object ready you can loop using for-each as before.
   for (teetobjects pecdeedObject : concurrentAbcobjects) {

      //checking some condition for each element in the inner most element ....
      if (st.contains(s1)) {

         // if the condition is true then removing the element of the 
         // aaraylist named abcobjects within inner for loop
         abcobjects.remove(recFeedObject);
         continue;
      }

   }