dimanche 25 septembre 2016

How can I add 100 random elements to an empty array with duplicates?

In this code, I'm shuffling 100 random elements instead of adding 100 random elements with duplicates, and returning 10 unique keys. How can I do that?

import java.util.ArrayList; import java.util.Collections;

public class UniqueRandomElements {

/**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here

    ArrayList<Integer> uniqueKeys = new ArrayList<Integer>();
    for (int i = 0; i < 101; i++) {
        uniqueKeys.add(new Integer(i));
    }
    Collections.shuffle(uniqueKeys);
    for (int i = 0; i < 101; i++) {
        System.out.println(uniqueKeys.get(i));
}
}

}




Aucun commentaire:

Enregistrer un commentaire