加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

java – 仅返回ArrayList中的最后一个元素

发布时间:2020-12-15 04:41:44 所属栏目:Java 来源:网络整理
导读:我一直在教自己 java,我一直坚持一个问题,无论我做什么似乎无法解决.我做了一些研究,但提供的所有选项似乎都没有用.希望你们能够教我一些东西. 我有一个.txt文件,其中包含: AccountName1:Password1 AccountName2:Password2 AccountName3:Password3 AccountN
我一直在教自己 java,我一直坚持一个问题,无论我做什么似乎无法解决.我做了一些研究,但提供的所有选项似乎都没有用.希望你们能够教我一些东西.

我有一个.txt文件,其中包含:

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

然后读取文件的元素并将其插入List:

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

解决方法

问题是在while循环之前声明名称和密码.这些变量存储上次遇到的用户名和密码.当while循环结束时,这些变量分别存储AccountName5和Password5的值.

当您输入第二个for循环时,首先使用存储AccountName5和Password5的名称和密码创建一个新的UserAccount.

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

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读