java – 查找和替换链接列表中的元素
我需要能够在链接列表中搜索某些唯一元素(用户名,密码,电子邮件),找到这些元素后,我需要转到列表中的下一个节点并开始一系列语句,允许用户更改配置文件信息.由于某种原因我的代码不起作用,我不知道如何解决它.任何帮助都会很棒!
GUI的外观 链接到帐户类:http://pastebin.com/jnBrcnP1 因此,用户填写所需信息,如果他们想要更改“名称”或“性别”等个人资料信息,他们会更改信息,然后将旁边的ComboBox设置为“是”,然后单击“保存设置”按钮. 以下是链接列表的外观: tobi tobi123 tobi@hotmail.com tobi Mixed Breed Male 1-2 Virginia Walking peppy peppy123 peppy@hotmail.com peppy Chihuahua Male 5-6 Virginia Eating 这是我的按钮代码: private void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) { //New Linked List created from file LinkedList<Account> account = new LinkedList<Account>(); try { read(account,"doggydates.txt"); } catch (Exception e) { System.err.println(e.toString()); } display(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(); //change 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(); //cancel combo box String accountcancel = (String) jComboBoxP5.getSelectedItem(); 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 { ListIterator<Account> itr = account.listIterator(); while (itr.hasNext()) { Account item = itr.next(); if(item.getUsername().equals(username) && item.getPassword().equals(password)) { if(passchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.setDataAtCurrent(password); } } } if(emailchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(email); } } } if(namechange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(name); } } } if(breedchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(breed); } } } if(genderchange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(gender); } } } if(agechange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(age); } } } if(statechange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(state); } } } if(hobbychange.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.goToNext(); acc.setDataAtCurrent(hobby); } } } if(accountcancel.equals("Yes")) { for(Account acc : account){ if(acc.getUsername().equals(username)){ acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); acc.goToNext(); acc.deleteCurrentNode(); } } } } } String file_name = "doggydates.txt"; try { FileWriter fstream = new FileWriter(file_name); BufferedWriter out = new BufferedWriter(fstream); ListIterator itr2 = account.listIterator(); while (itr2.hasNext()) { Account element = (Account) itr2.next(); out.write("" + element); out.newLine(); } out.close(); System.out.println("File created successfully."); } catch (Exception e) { } } } 阅读方法: public static void read(LinkedList<Account> account,String inputFileName) throws java.io.IOException{ BufferedReader infile = new BufferedReader(new FileReader(inputFileName)); while(infile.ready()) { String username = readLine(infile); String password = readLine(infile); String email = readLine(infile); String name = readLine(infile); String breed = readLine(infile); String gender = readLine(infile); String age = readLine(infile); String state = readLine(infile); String hobby = readLine(infile); Account a = new Account(username,password,email,name,breed,gender,age,state,hobby); account.add(a); a.showList(); } infile.close(); } 解决方法
查看代码,执行以下操作:
首先,LinkedList< Account> account = new LinkedList< Account>();这意味着每次用户单击“保存设置”按钮时都会创建列表. 二,ListIterator< Account> itr = account.listIterator();,但是帐号是一个空列表!因此,您无法将任何单个对象与任何人员数据进行比较. 最后,但并非最不重要的,阅读(帐户,“doggydates.txt”);在完成所有比较之后,我猜你正在读取文件数据并填写列表. 重新组织您的代码,然后重试. 更新: > Account类是您的实体.此类必须包含列表的数据.您可以像LinkedList的Node一样重用此类,指定指向其他Account对象引用的指针,无需使用ListNode实例. 您可以将设计更改为更简单的设计.我会给你2个样品供你设计: 表单1.将您的Acount类用作LinkedList节点. public class Account { private Account next; private Account previous; private String username; private String password; private String email; private String name; private String breed; private String gender; private String age; private String state; private String hobby; //constructor logic... //getters and setters... //toString method (for System.out.println) } public class AccountLinkedList { private Account head; private int size; public AccountLinkedList() { this.size = 0; this.head = null; } public boolean insert(Account account) { if (this.head == null) { this.head = account; } else { Account current = head; while (current.getNext() != null) { current = current.getNext(); } //maintain the LinkedList order current.setNext(account); account.setPrevious(current); } } public Account findAccount(Account account) { Account current = head; while (current != null) { if (current.equals(account) { return current; } current = current.getNext(); } return null; } //create the other methods to search,delete and traverse your list... } public class MyProgram { public void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) { Account account = new Account(); //set the account data,I'll just stick to username and //password attributes String username = jTextFieldP3.getText(); String password = jPasswordFieldP1.getText(); account.setUsername(username); account.setPassword(password); //perform the update updateData(); } public void updateData(Account account) { AccountLinkedList accountList = new AccountLinkedList; //loading data into our list loadDataFromFile(accountList,"myFile.txt"); //perform the search operation Account accountAux = accountList.findAccount(account); //if the account is found,then update the accountAux data if (accountAux != null) { updateAccountData(accountAux,account); } else { accountList.insert(account); } //saving the modified data saveDataToFile(accountList,"myFile.txt"); } } 表单2.像实体一样使用Account类并使用Java LinkedList实现: //check that now you don't have the next and previous attributes public class Account { private String username; private String password; private String email; private String name; private String breed; private String gender; private String age; private String state; private String hobby; //constructor logic... //getters and setters... //toString method (for System.out.println) } public class MyProgram { public void jButtonP1ActionPerformed(java.awt.event.ActionEvent evt) { Account account = new Account(); //set the account data,I'll just stick to username and //password attributes String username = jTextFieldP3.getText(); String password = jPasswordFieldP1.getText(); account.setUsername(username); account.setPassword(password); //perform the update updateData(); } public void updateData(Account account) { LinkedList<Account> accountList = new LinkedList<Account>(); //loading data into our list loadDataFromFile(accountList,"myFile.txt"); //perform the search operation ListIterator<Account> li = accountList.listIterator(); Account accountAux = null; while(li.hasNext()) { accountAux = li.next(); //matching the account data outside with the current account //of the list iterator,this is just a sample,you can change //this logic if (accountAux.equals(account) { updateAccountData(accountAux,account); break; } accountAux = null; } //in case the account was not fount in the LinkedList,add it. if (accountAux == null) accountList.add(account); //saving the modified data saveDataToFile(accountList,"myFile.txt"); } } IMO,我会坚持使用Form 2而不是Form1.希望它能帮到你. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |