【问题标题】:Updating elements in arraylist, is there a better way than this?更新arraylist中的元素,有没有比这更好的方法?
【发布时间】:2016-04-02 14:20:09
【问题描述】:

我正在使用 swing 制作一个简单的应用程序,而且我是数组列表和列表迭代器的新手。我正在尝试做的是一个按钮,它允许您更新有关数组列表中人员的信息。我应该在不使用 for 或 for-each 循环的情况下做到这一点。这是我的代码:

String searchedName = JOptionPane.showInputDialog(null, "Type in the first name of the person you want to update:");
ListIterator<Student> listIt = peopleArray.listIterator();
    while(listIt.hasNext()){
        String firstname = listIt.next().getFirstname();
            if(searchedName.equalsIgnoreCase(firstname){
                String newFirstname = JOptionPane.showInputDialog(null, "Input new first name:");
                String newLastname = JOptionPane.showInputDialog(null, "Type in new last name:");
                String newEmail = JOptionPane.showInputDialog(null, "Type in new email:");
                String newOccupation = JOptionPane.showInputDialog(null, "Type in new occupation:");
                listIt.previous().setFirstname(newFirstname);
                listIt.next().setLastname(newLastname);
                listIt.previous().setEmail(newEmail);
                listIt.next().setOccupation(newOccupation);
    }
}

此代码有效,但看起来很奇怪。我是否必须像那样跳回第四个(listIt.next() 然后 listIt.previous())还是有更好的方法?

//否

【问题讨论】:

  • 你为什么不得到Student(使用listIt.next())?然后你可以在不影响迭代器的情况下将它操作到你心中的内容。

标签: java swing arraylist


【解决方案1】:

你不必来回跳跃,确保只调用一次 next()。这个呢:

    String searchedName = JOptionPane.showInputDialog(null, "Type in the first name of the person you want to update:");
        ListIterator<Student> listIt = peopleArray.listIterator();
            while(listIt.hasNext()){
                Student studentToUpdate = listIt.next();
                    if(searchedName.equalsIgnoreCase(studentToUpdate.getFirstname()){
                        String newFirstname = JOptionPane.showInputDialog(null, "Input new first name:");
                        String newLastname = JOptionPane.showInputDialog(null, "Type in new last name:");
                        String newEmail = JOptionPane.showInputDialog(null, "Type in new email:");
                        String newOccupation = JOptionPane.showInputDialog(null, "Type in new occupation:");
                        studentToUpdate.setFirstname(newFirstname);
                        studentToUpdate.next().setLastname(newLastname);
                        studentToUpdate.setEmail(newEmail);
                        studentToUpdate.setOccupation(newOccupation);
            }
        }

【讨论】:

  • 这解决了我的问题,谢谢!没想到为循环创建一个“studentToUpdate”对象,简单!
  • 这不是什么大不了的事情,但通常你会在第二行使用通用的Iterator 而不是ListIterator,这样代码就可以与任何Iterable 一起使用,而不需要一个列表。它不需要ListIterator 的额外力量。
【解决方案2】:

使用旧迭代器语法的常规习惯用法是:

Iterator<Student> studentIterator = peopleArray.iterator();
while(studentIterator.hasNext()){
  Student student = studentIterator.next();
  // Do stuff with student. For example:
  if(student.getFirstName().equalsIgnoreCase(firstname)) {
    // ...
  }
} 

【讨论】:

    猜你喜欢
    • 2018-01-30
    • 2023-03-06
    • 2021-04-17
    • 1970-01-01
    • 2019-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多