Java 查找和替换链接列表中的节点

Java 查找和替换链接列表中的节点,java,linked-list,Java,Linked List,我需要能够找到并替换链接列表中的用户信息。我一直在阅读一些教程和示例,但似乎不起作用。用于链接列表的set方法无效。所以我想知道我是不是实施错了还是怎么了。任何帮助都会很好。另外,仅供参考,我的链表是从一个文件中加载的,基本上只是它的用户信息,每个元素位于不同的行上 i、 e 代码: private void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt){ LinkedList帐户=新建LinkedList(); //用户信息

我需要能够找到并替换链接列表中的用户信息。我一直在阅读一些教程和示例,但似乎不起作用。用于链接列表的set方法无效。所以我想知道我是不是实施错了还是怎么了。任何帮助都会很好。另外,仅供参考,我的链表是从一个文件中加载的,基本上只是它的用户信息,每个元素位于不同的行上

i、 e

代码:

private void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt){
LinkedList帐户=新建LinkedList();
//用户信息
字符串username=jTextFieldP3.getText();
字符串密码=jPasswordFieldP1.getText();
字符串email=jTextFieldP4.getText();
String name=jTextFieldP1.getText();
字符串种类=(字符串)jcomboxp4.getSelectedItem();
字符串性别=(字符串)jcomboxp3.getSelectedItem();
字符串年龄=(字符串)jComboxP1.getSelectedItem();
字符串状态=(字符串)jcomboxp2.getSelectedItem();
字符串hobby=jTextFieldP2.getText();
//组合框
字符串passchange=(字符串)jcomboxp13.getSelectedItem();
字符串emailchange=(字符串)jcomboxp14.getSelectedItem();
字符串名称更改=(字符串)jcomboxp6.getSelectedItem();
String breedchange=(String)jcomboxP7.getSelectedItem();
字符串genderchange=(字符串)jcomboxp8.getSelectedItem();
String agechange=(String)jComboxP9.getSelectedItem();
String statechange=(String)jComboxP10.getSelectedItem();
字符串hobbychange=(字符串)jcomboxp11.getSelectedItem();
字符串accountcancel=(字符串)jcomboxp5.getSelectedItem();
帐户a=新帐户(用户名、密码、电子邮件、姓名、品种、性别、年龄、状态、爱好);
账户。添加(a);
if(username.equals(“”)| password.equals(“”)| email.equals(“”)//如果密码和用户名为空>执行此操作>>
{
jButtonP1.setEnabled(false);
jTextFieldP3.setText(“”);
jPasswordFieldP1.setText(“”);
jTextFieldP4.setText(“”);
jButtonP1.setEnabled(true);
此.setVisible(true);
}
else if(a.onList(用户名)| | a.onList(密码)| | a.onList(电子邮件))
{
int index=account.indexOf(嗜好);
account.set(索引,“新字符串”);
}
其他的
{
}
}
这里的问题是indexOf()将返回-1,因为它在包含帐户值的列表中搜索字符串值

不能按列表中元素的字段在列表中搜索。您必须手动搜索该帐户,然后设置“嗜好”字段:

for(Account acc : account){
    if(acc.getHobby().equals(hobby)){
        acc.setHobby("New String");
    }
}

谢谢,但是假设我首先找到了用户名。我将如何迭代到下一个节点并以这种方式更改它们?主要是因为用户名永远不会改变。基本上,当你创建一个列表时,你可以期望indexOf方法只为你之前放入的对象返回有意义的值。但是您需要在列表中搜索特定字段中具有特定值的元素。列表无法为您执行此操作。您必须按for循环手动搜索并检查其中的每个对象。然后,您可以根据自己的意愿进行搜索,并根据自己的意愿进行修改。
private void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) {
    LinkedList<Account> account = new LinkedList<Account>();
    //user information
    String username = jTextFieldP3.getText();
    String password = jPasswordFieldP1.getText();
    String email = jTextFieldP4.getText();
    String name = jTextFieldP1.getText();
    String breed = (String) jComboBoxP4.getSelectedItem();
    String gender = (String) jComboBoxP3.getSelectedItem();
    String age = (String) jComboBoxP1.getSelectedItem();
    String state = (String) jComboBoxP2.getSelectedItem();
    String hobby = jTextFieldP2.getText();
    //combo boxes
    String passchange = (String) jComboBoxP13.getSelectedItem();
    String emailchange = (String) jComboBoxP14.getSelectedItem();
    String namechange = (String) jComboBoxP6.getSelectedItem();
    String breedchange = (String) jComboBoxP7.getSelectedItem();
    String genderchange = (String) jComboBoxP8.getSelectedItem();
    String agechange = (String) jComboBoxP9.getSelectedItem();
    String statechange = (String) jComboBoxP10.getSelectedItem();
    String hobbychange = (String) jComboBoxP11.getSelectedItem();
    String accountcancel = (String) jComboBoxP5.getSelectedItem();

    Account a = new Account(username, password, email, name, breed, gender, age, state, hobby);
    account.add(a);

    if(username.equals("") || password.equals("") || email.equals("")) // If password and username is empty > Do this >>>
    {
        jButtonP1.setEnabled(false);
        jTextFieldP3.setText("");
        jPasswordFieldP1.setText("");
        jTextFieldP4.setText("");
        jButtonP1.setEnabled(true);
        this.setVisible(true);

    }
    else if(a.onList(username) || a.onList(password) || a.onList(email))
    {
        int index = account.indexOf(hobby);
        account.set(index, "New String");
    }
    else
    {

    }

}
int index = account.indexOf(hobby);
account.set(index, "New String");
for(Account acc : account){
    if(acc.getHobby().equals(hobby)){
        acc.setHobby("New String");
    }
}