0
我需要能夠在鏈接列表中查找和替換用戶信息。我一直在閱讀幾個教程和例子,但它似乎沒有工作。用於鏈接列表的設置方法不起作用。所以我想知道我是否實施了錯誤或什麼。任何幫助都會很棒。也僅僅FYI我的鏈表是從一個文件加載的,基本上它的用戶信息與每個元素在不同的行上。查找並替換鏈接列表中的節點
即
int index = account.indexOf(hobby);
account.set(index, "New String");
代碼:
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
{
}
}
謝謝,但可以說我先找到了用戶名。我將如何迭代到每個下一個節點,然後改變它們?主要是因爲用戶名永遠不會改變。我只想要選擇改變一切。 – 2012-03-12 22:41:56
基本上,當您創建一個List時,您可以期望indexOf方法僅爲先前放入的對象返回有意義的值。但是您需要在列表中搜索某個字段中具有特定值的元素。列表不能爲你做這個。您必須通過for循環手動搜索並檢查每個對象內部。然後,您可以根據自己的意願進行搜索,並根據需要進行修改。 – 2012-03-12 22:46:03
甜蜜的謝謝生病試圖弄清楚 – 2012-03-12 22:50:59