简体   繁体   English

Arraylist仅打印最后添加的元素

[英]Arraylist only prints last added element

I have a function which generates pairs of keys for an array of users passed in (using RSA algorithm), it appears to generate the keys correctly for each user and adds them to an array list. 我有一个函数,用于为传入的用户数组生成密钥对(使用RSA算法),它似乎可以为每个用户正确生成密钥并将它们添加到数组列表中。 However,when I'm trying to print the output,it appears to print only the last element. 但是,当我尝试打印输出时,它似乎只打印最后一个元素。 Can't seem to figure out what I'm doing wrong. 似乎无法弄清楚我在做什么错。

Here is the function which generates the keys and returns an arraylist:- it takes a string array of users as a parameter. 这是生成密钥并返回arraylist的函数:-它使用用户的字符串数组作为参数。

public static  ArrayList<User> generateUserKeys(String [] users){

    ArrayList <User> usrs = new ArrayList<User>();

    KeyPair k;

    for ( int i=0;i<users.length;i++)
    {

        k=generateKeyPair();

        usrs.add(new User(users[i],k.getPublic(),k.getPrivate()));
        System.out.println("User Name is :"+ usrs.get(i).getUserName());
        System.out.println("Public Key is :"+ usrs.get(i).getPublicKey());
        System.out.println("Private Key is :" + usrs.get(i).getPrivateKey());



    }   


    return usrs;

}

Here is how I'm testing it:- 这是我测试的方式:-

String [] users =  
{"alisy@tcd.ie","yimk@tcd.ie","bachas@tcd.ie","tannerh@tcd.ie"};

 ArrayList<User> usrz= generateUserKeys(users);
 Iterator<User> itr = usrz.iterator();

 while(itr.hasNext())
{
    System.out.println(itr.next().getUserName());

}

The output I'm receiving is 我收到的输出是

tannerh@tcd.ie
tannerh@tcd.ie
tannerh@tcd.ie
tannerh@tcd.ie

I haven´t seen any problems in your generateUserKeys method code. 我在您的generateUserKeys方法代码中没有看到任何问题。 Probably the User class is not ok, have you set the user name value as "tannerh@tcd.ie" inside the User class? 可能是User类不好,您是否在User类中将用户名值设置为“ tannerh@tcd.ie”?

What is the result of the others System.out.println(...) lines? 其他System.out.println(...)行的结果是什么? It should print "User Name is : tannerh@tcd.ie" four times too. 它也应该打印四次“用户名是:tannerh@tcd.ie”。

I tried running it as is, obviously there are few assumptions that I made in terms of what code has been used for User or KeyPairGenerator. 我尝试按原样运行它,显然,对于用户或KeyPairGenerator使用的代码,我几乎没有做出任何假设。 Here is my code and I did not get the kind of output which was described in the problem. 这是我的代码,但没有得到问题中描述的那种输出。 Can you help me to see if it has to do with static or something else? 您能帮我看看它是否与静态或其他有关吗? @PaulBoddington - Please do not treat this as judging what you have already said. @PaulBoddington-请不要将此视为您已经说过的话。 I am posting this on basis of a testing I did. 我将根据我所做的测试来发布此内容。 Caveat: I can be wrong here. 警告:我可能在这里错了。

Source Code: 源代码:

import java.security.KeyPair;
import java.security.KeyPairGenerator;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.util.ArrayList;
import java.util.Iterator;

class User {
    private String userName;
    private PublicKey pubKey;
    private PrivateKey privKey;

    public User(String string, PublicKey public1, PrivateKey private1) {
        userName = string;
        pubKey = public1;
        privKey = private1;
    }

    public String getUserName() {
        return userName;
    }

    public String getPublicKey() {
        return pubKey.toString();
    }

    public String getPrivateKey() {
        return privKey.toString();
    }
}

public class RSAKeyGenExample {

    public static ArrayList<User> generateUserKeys(String[] users) throws NoSuchAlgorithmException {
        ArrayList<User> usrs = new ArrayList<User>();

        KeyPair k;
        KeyPairGenerator keyGen = null;
        keyGen = KeyPairGenerator.getInstance("RSA");

        for (int i = 0; i < users.length; i++) {
            k = keyGen.generateKeyPair();

            usrs.add(new User(users[i], k.getPublic(), k.getPrivate()));
            System.out.println("User Name is :" + usrs.get(i).getUserName());
            System.out.println("Public Key is :" + usrs.get(i).getPublicKey());
            System.out.println("Private Key is :" + usrs.get(i).getPrivateKey());
        }
        return usrs;
    }

    public static void main(String[] args) throws NoSuchAlgorithmException {
        String[] users = { "alisy@tcd.ie", "yimk@tcd.ie", "bachas@tcd.ie", "tannerh@tcd.ie" };

        ArrayList<User> usrz = generateUserKeys(users);

        Iterator<User> itr = usrz.iterator();
        while (itr.hasNext()) {
            System.out.println(itr.next().getUserName());
        }
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM