Java 更新arraylist中的元素,有比这更好的方法吗?

Java 更新arraylist中的元素,有比这更好的方法吗?,java,swing,arraylist,Java,Swing,Arraylist,我正在用swing制作一个简单的应用程序,对于ArrayList和listiterator我是新手。我尝试做的是一个按钮,它允许您更新arraylist中人员的信息。我应该这样做,而不使用for或for-each循环。这是我的密码: String searchedName = JOptionPane.showInputDialog(null, "Type in the first name of the person you want to update:"); ListIterator<

我正在用swing制作一个简单的应用程序,对于ArrayList和listiterator我是新手。我尝试做的是一个按钮,它允许您更新arraylist中人员的信息。我应该这样做,而不使用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);
    }
}
String searchedName=JOptionPane.showInputDialog(null,“输入要更新的人的名字:”);
ListIterator listIt=peopleArray.ListIterator();
while(listIt.hasNext()){
String firstname=listIt.next().getFirstname();
if(searchedName.equalsIgnoreCase(firstname){
字符串newFirstname=JOptionPane.showInputDialog(null,“输入新名字:”);
字符串newLastname=JOptionPane.showInputDialog(null,“键入新姓氏:”);
字符串newEmail=JOptionPane.showInputDialog(null,“键入新电子邮件:”);
字符串newocculation=JOptionPane.showInputDialog(null,“键入新职业:”);
listIt.previous().setFirstname(newFirstname);
listIt.next().setLastname(newLastname);
listIt.previous().setEmail(newEmail);
listIt.next().setocculation(newocculation);
}
}
这段代码可以工作,但看起来很奇怪。我必须像那样跳回第四行(listIt.next()和listIt.previous()),还是有更好的方法


//N

您不必来回跳转,请确保只调用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);
            }
        }
String searchedName=JOptionPane.showInputDialog(null,“输入要更新的人的名字:”);
ListIterator listIt=peopleArray.ListIterator();
while(listIt.hasNext()){
Student studentToUpdate=listIt.next();
if(searchedName.equalsIgnoreCase(studentToUpdate.getFirstname()){
字符串newFirstname=JOptionPane.showInputDialog(null,“输入新名字:”);
字符串newLastname=JOptionPane.showInputDialog(null,“键入新姓氏:”);
字符串newEmail=JOptionPane.showInputDialog(null,“键入新电子邮件:”);
字符串newocculation=JOptionPane.showInputDialog(null,“键入新职业:”);
studentToUpdate.setFirstname(newFirstname);
studentToUpdate.next().setLastname(newLastname);
studentToUpdate.setEmail(newEmail);
学生更新职业设置(新职业);
}
}

您不必来回跳转,请确保只调用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);
            }
        }
String searchedName=JOptionPane.showInputDialog(null,“输入要更新的人的名字:”);
ListIterator listIt=peopleArray.ListIterator();
while(listIt.hasNext()){
Student studentToUpdate=listIt.next();
if(searchedName.equalsIgnoreCase(studentToUpdate.getFirstname()){
字符串newFirstname=JOptionPane.showInputDialog(null,“输入新名字:”);
字符串newLastname=JOptionPane.showInputDialog(null,“键入新姓氏:”);
字符串newEmail=JOptionPane.showInputDialog(null,“键入新电子邮件:”);
字符串newocculation=JOptionPane.showInputDialog(null,“键入新职业:”);
studentToUpdate.setFirstname(newFirstname);
studentToUpdate.next().setLastname(newLastname);
studentToUpdate.setEmail(newEmail);
学生更新职业设置(新职业);
}
}

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

Iterator<Student> studentIterator = peopleArray.iterator();
while(studentIterator.hasNext()){
  Student student = studentIterator.next();
  // Do stuff with student. For example:
  if(student.getFirstName().equalsIgnoreCase(firstname)) {
    // ...
  }
} 
Iterator studentIterator=peopleArray.Iterator();
while(studentIterator.hasNext()){
Student=studentIterator.next();
//与学生一起做事。例如:
if(student.getFirstName().equalsIgnoreCase(firstname)){
// ...
}
} 

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

Iterator<Student> studentIterator = peopleArray.iterator();
while(studentIterator.hasNext()){
  Student student = studentIterator.next();
  // Do stuff with student. For example:
  if(student.getFirstName().equalsIgnoreCase(firstname)) {
    // ...
  }
} 
Iterator studentIterator=peopleArray.Iterator();
while(studentIterator.hasNext()){
Student=studentIterator.next();
//与学生一起做事。例如:
if(student.getFirstName().equalsIgnoreCase(firstname)){
// ...
}
} 

你为什么不得到
学生
(使用
listIt.next()
)?然后你可以在不影响迭代器的情况下随心所欲地操作它。你为什么不得到
学生
(使用
listIt.next()
)?然后你可以在不影响迭代器的情况下随心所欲地操作它。这解决了我的问题,谢谢!我没有想到创建“studentToUpdate”循环对象,很简单!这不是一个很大的东西,但通常你会在第二行使用一个通用的
迭代器,而不是
列表迭代器,这样代码就可以处理任何
Iterable
,而不需要一个列表。它不需要
列表迭代器的额外功能。这解决了我的问题,谢谢!不是吗考虑为循环创建一个“studentToUpdate”对象,很简单!这不是一件大事,但通常您会在第二行使用泛型
迭代器而不是
ListIterator
,这样代码就可以与任何
Iterable
一起工作,而不需要列表。它不需要
ListIterator
的额外功能。