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

Only returning the last element in ArrayList

我一直在自学 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();}
    }
}

getter/setterclass如下:

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  

但我希望它 return:

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

我知道 a.toString() return 是正确的,但我的 namPw.getName()namPw.getPassword() 只给我列表的最后一个元素。

我不理解或遗漏了什么?如何将 namPw.getName()namPw.getPassword() 正确地添加到 return 列表?

问题出在 while 循环之前 namepassword 的声明。这些变量存储最后遇到的用户名和密码。当while循环结束时,这些变量分别存储值AccountName5Password5

当您进入第二个 for 循环时,您首先使用 namepassword 创建一个新的 UserAccount,其中存储 AccountName5Password5 .

如果您只想打印此列表,则不需要创建列表内容的副本。只需做:

 for(AccountInfo a : accounts) {
     System.out.println(a.toString() + " " + a.getName() + " " + a.getPassword());
 }

是因为:

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());

无需在循环中创建新对象。您已经在 a 中获取对象。 删除对象创建行。它正在创建具有名称和密码的对象,因为它在循环之外而永远不会改变。 检查以下解决方案:

for(AccountInfo a: accounts){
        name = a.getName();
        password = a.getPassword();               
        System.out.println(a.toString() + "         " + name + " " + password);
}