简体   繁体   中英

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. 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.

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. Probably the User class is not ok, have you set the user name value as "tannerh@tcd.ie" inside the User class?

What is the result of the others System.out.println(...) lines? It should print "User Name is : tannerh@tcd.ie" four times too.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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