Get four numbers random in a array -


i have array

   string[] arr={"a","b","c","d","e","f","a","d","m","e"}; 

i want random 4 different numbers array. how must do?

list<string> shuffled = collections.shuffle(arrays.aslist(arr)); list<string> fourrandoms = shuffled.sublist(0, 4); 

or, if want leave array untouched:

list<string> shuffled = collections.shuffle(new arraylist<string>(arrays.aslist(arr))); list<string> fourrandoms = shuffled.sublist(0, 4); 

edit: remove duplicates array, use set:

set<string> uniquevalues = new hashset<string>(arrays.aslist(arr)); list<string> shuffled = collections.shuffle(new arraylist<string>(uniquevalues)); list<string> fourrandoms = shuffled.sublist(0, 4); 

learn standard java collections, , avoid using arrays completely. should use set beginning if duplicates not allowed.


Comments

Popular posts from this blog

c# - Send Image in Json : 400 Bad request -

jquery - Fancybox - apply a function to several elements -

An easy way to program an Android keyboard layout app -