【问题标题】:Adding Objects To Linked List Using Iterator使用迭代器将对象添加到链表
【发布时间】:2013-03-20 18:54:55
【问题描述】:

好的,我试图将一些学生对象添加到链接列表中,但我不允许使用链接列表的 .add 方法,所以当用户调用 removeStudent 方法时,他们输入学生 ID 号,然后它使用该数组检查列表中的对象

这是我添加方法的代码:

public void deleteStudent(int studentID)
{
    while (iter.hasNext())
    {
       Student ob = iter.next();
       if (ob.getStudentID() == studentID)
       {
         iter.remove();
         break;
       }
     }
  }

当我运行这个时,我得到这个错误:

Exception in thread "main" java.util.ConcurrentModificationException
    at java.util.LinkedList$ListItr.checkForComodification(LinkedList.java:953)
    at java.util.LinkedList$ListItr.next(LinkedList.java:886)
    at student.Registry.deleteStudent(Registry.java:30)
    at student.Registry.main(Registry.java:51)
Java Result: 1

【问题讨论】:

  • 这应该如何将Students 添加到List
  • 这可能会有所帮助:stackoverflow.com/a/1496221/2087646
  • 这看起来像是一个很好的删除方法,但不是一个添加方法。
  • 如果可能的话,能否请您发布您的原始任务?你能用什么,不能用什么。看起来该任务是针对链表行为的,您可以使用的内容受到限制。
  • 好的,这就是我想要做的:建立一个学生注册系统,可以添加、删除和查看学生,所以我的注册表类有 2 个方法:deleteStudent(int studentID) )这将接受一个整数值,该值将是学生 ID 号,现在当我在此参数中输入一个 ID 时,它将遍历 LinkedList 以找到一个已输入 ID 的学生对象。如果找到则删除,如果未找到则出现错误消息

标签: java iterator linked-list


【解决方案1】:

ConcurrentModificationException 基本上意味着您在创建列表迭代器和使用它之间修改了列表。您需要做的是在将所有内容添加到列表或通过任何其他方式修改它之后创建和使用迭代器。

【讨论】:

    【解决方案2】:

    编辑:尝试使用本地迭代器:

    public void deleteStudent(int studentID){
      Iterator<Student> iterator=listStudent.iterator();
      while (iter.hasNext()){
        Student ob = iterator.next();
        if (ob.getStudentID() == studentID){
          iterator.remove(student);
          break;
        }
      }
    }
    

    这样,列表和本地迭代器之间就没有并发修改。但这会修改​​列表,如果您在调用此方法后继续尝试使用以前的迭代器,您可能会遇到问题。

    编辑:AbstractList 维护一个“modCount”(修改计数)属性,该属性计算您在列表中添加、删除等的数量。

    当您在 List 上获取迭代器时,迭代器会记住此 modCount,以确保您不会使用迭代器之外的方法编辑列表。

    例子:

    List myList=new ArrayList();
    //modCount for the list is 0
    
    myList.add("test");
    //modCount for the list is 1
    
    Iterator iterator=myList.iterator();
    //modCount for the list is 1
    //expected modCount for the iterator is initialized to 1
    
    myList.add("test 2");
    //modCount for the list is 2
    //expected modCount for the iterator is initialized to 1
    
    iterator.remove("test");
    //modCount != expectedModCount => throw new ConcurrentModificationException()
    

    【讨论】:

    • 我认为您可以在迭代时删除一个项目,如果您在创建列表期间尝试这样做,您将无法这样做。您需要在迭代和修改列表之前完成创建列表。
    • 我们看一下ArrayList使用的迭代器的remove方法 public void remove() { if (lastRet
    • 我们在这里处理的是 LinkedList。
    • 您当然可以在迭代期间删除项目;这就是使用迭代器的全部意义所在。您只需要使用 Iterator.remove() 方法,而不是其他删除方法。
    • 这也不起作用,会出现同样的错误
    【解决方案3】:

    您的原始任务不清楚,但您似乎受到 LinkedList API 的限制。

    没有别的了。

    使用链表删除(和修改)元素并不是一件容易的事——您可能必须安全地遍历整个列表。 (关于链表的好消息是插入很容易)。

    这会起作用(也有其他方法):

    public void deleteStudent(int studentID){
    ...
      LinkedList<Student> modifiedStudentList = new LinkedList<>();
      while (iter.hasNext()){
        Student ob = iterator.next();
        if (ob.getStudentID() != studentID){
            modifiedStudentList.addLast(ob) 
        }
      }
      studentList = modifiedStudentList;
    }
    

    因此,您的列表将包含它之前拥有的所有学生,但具有 studentID 的学生除外,如果列表中不存在该学生,则不会删除任何内容。

    您必须遍历集合中的所有元素,但这是您的老师设置的 API 限制的代价。

    【讨论】:

    • 为指导干杯,我想我现在已经对它进行了排序,我的迭代器被贴在方法之外,我只为每个方法使用一个,如果我公开地将迭代器添加到它们工作正常的方法中。
    猜你喜欢
    • 1970-01-01
    • 2019-05-05
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    • 2016-01-02
    • 2012-04-27
    • 1970-01-01
    相关资源
    最近更新 更多