1: package example1;
2: import java.util.ArrayList;
3: import java.util.Collections;
4: public class RandomStringFromArrayList {
5: public static void main(String[] args) {
6: // create a 1D Array
7: String companyName[] = { "Apple", "HTC", "Samsung", "Microsoft" };
8: ArrayList<String> list = new ArrayList<String>();
9: for (int i = 0; i < companyName.length; i++) {
10: list.add(i, companyName[i]);
11: }
12: for (int j = 1; j <= 10; j++) {
13: Collections.shuffle(list);
14: String CompanyName = list.get(0);
15: System.out.println(CompanyName);
16: }
17: }
18: }
Monday, September 7, 2015
Java random string generator (not unique)
This code generates random Array elements. The Array elements are first taken into ArrayList and then Collection.Shuffle method is used to shuffle the elements. This produce a unique random elements.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment