只返回ArrayList中的最后一个元素

3

我是一名自学Java的学习者,目前遇到一个问题,无论我怎么尝试都无法解决。我已经做了一些研究,但提供的所有选项似乎都不起作用。希望大家能够教我一些东西。

我有一个包含以下内容的.txt文件:

AccountName1:Password1  
AccountName2:Password2  
AccountName3:Password3  
AccountName4:Password4  
AccountName5:Password5   

文件的元素然后被读取并插入到一个列表中:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

public abstract class AccountFileReader {

  private static Scanner sc;

  public static void main(String[] args) {  

      try {         
        // Enables ability to find file in any OS.
           String file = File.separator + "some folder name"
                         + File.seperator + "AccNamePw.txt";

           File f = new File(file);
           sc = new Scanner(f);

           List<AccountInfo> accounts = new ArrayList<AccountInfo>();
           String name = "";
           String password = "";

           while (sc.hasNext()){
            // Reads and checks if there is a new line
               String line = sc.nextLine();
            // Creates delimiter to make the different elements on file f
               String[] details = line.split(":");
            // Initializes 1st element
               name = details[0];
            // Initializes 2nd element
               password = details[1];           
            // Creates new object "a" that has the 2 elements from each line
               AccountInfo a = new AccountInfo(name, password);
            // Adds the "a" object to the "accounts" List
               accounts.add(a);
           }
        // Iterates list and prints out the list
           for(AccountInfo a: accounts){
        // The hiccup is in here somewhere. This for loop isn't working in 
        // a way I think it's supposed to.

        // Create new object of the getter, setter class to use in this loop
           AccountInfo namPw = new AccountInfo(name, password);
              name = namPw.getName();
              password = namPw.getPassword();               
              System.out.println(a.toString() + "         " + name 
                                 + " " + password);
           }
        } catch (FileNotFoundException e) {e.printStackTrace();}
    }
}

获取器/设置器类如下所示:
public class AccountInfo{
private String name;
private String password;

public AccountInfo(String name, String password) {
    this.setName(name);
    this.setPassword(password);
}

public void setName(String name) { this.name = name; }

public String getName() { return name; }

public void setPassword(String password) { this.password = password; }    

public String getPassword() { return password; }

public String toString(){ return name + " "+ password; }
}

我的输出是:
AccountName1:Password1      AccountName5:Password5  
AccountName2:Password2      AccountName5:Password5  
AccountName3:Password3      AccountName5:Password5  
AccountName4:Password4      AccountName5:Password5  
AccountName5:Password5      AccountName5:Password5  

But I want it to return:

AccountName1:Password1      AccountName1:Password1  
AccountName2:Password2      AccountName2:Password2  
AccountName3:Password3      AccountName3:Password3  
AccountName4:Password4      AccountName4:Password4  
AccountName5:Password5      AccountName5:Password5  

我知道a.toString()返回正确,但是我的namPw.getName()namPw.getPassword()只返回List的最后一个元素。

我没有理解或者遗漏了什么?如何让namPw.getName()namPw.getPassword()正确返回List中的元素?

3个回答

5
问题在于在 while 循环之前声明了 namepassword 变量。这些变量存储了最后遇到的用户名和密码。当 while 循环结束时,这些变量分别存储了值为 AccountName5Password5
进入第二个 for 循环时,你首先使用存储着 AccountName5Password5namepassword 创建了一个新的 UserAccount
如果你只是想打印这个列表,那么不需要创建列表内容的副本。只需执行如下操作:
 for(AccountInfo a : accounts) {
     System.out.println(a.toString() + " " + a.getName() + " " + a.getPassword());
 }

你比我先完成了。这里加上+1,我会删除我的回答 :( - apexlol
1
感谢您的帮助。非常感激。 - Charles

1
这是因为这个原因:
for(AccountInfo a: accounts){
    **AccountInfo namPw = new AccountInfo(name, password);**
          name = namPw.getName();
          password = namPw.getPassword();               
          System.out.println(a.toString() + "         " + name 
                             + " " + password);

你正在循环遍历已创建的AccountInfo对象,然后创建一个新的AccountInfo对象,并传入名称和密码(每次读取新行时设置的名称和密码,因此它们在读取文件时的最后一个设置值将是它们的值)。
不确定为什么要创建新的AccountInfo对象。但为了得到想要的结果,您需要这样做:
AccountInfo namPw = new AccountInfo(a.getName(), a.getPassword());

1
不需要在循环中创建新对象。您已经在a中获取了对象。 删除对象创建行。它正在创建一个带有名称和密码的对象,这永远不会改变,因为它在循环外部。 请查看以下解决方案:
for(AccountInfo a: accounts){
        name = a.getName();
        password = a.getPassword();               
        System.out.println(a.toString() + "         " + name + " " + password);
}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接