只返回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 / setter类如下:

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  

但我希望它返回:

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()正确返回列表?

尼尔斯·比伦(Niels Billen):

问题是申报namepasswordwhile循环之前。这些变量存储最后遇到的用户名和密码。当while循环结束时,这些变量分别存储AccountName5Password5

当您进入第二个for循环时,您首先要UserAccount使用using namepassword存储AccountName5and 创建一个新的Password5

如果只想打印此列表,则无需创建列表内容的副本。做就是了:

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章