pleaz for every body how can i operate to make and random choise inside a n array or tab[ ], please tank for your help | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

pleaz for every body how can i operate to make and random choise inside a n array or tab[ ], please tank for your help

ramdom choise in an array or tab[ ]

7th Oct 2019, 8:11 PM
zang bengono Philibert stéphane
zang bengono Philibert stéphane - avatar
4 Answers
0
witch data type you are want to use ? If you don't use primitive data types I have a solution: /** * from any number of objects, one is randomly selected and returned * * @param array the objects available for selection * @return the randomly chosen object */ @SafeVarargs public static <T> T randomOfAny(T... array) { if (array == null || array.length == 0) throw new IllegalArgumentException("Illegal Argument!"); if(array.getClass().getTypeName().endsWith("[][]")) throw new IllegalArgumentException("primitive data type not allowed. use wrapper class"); return array[(int) (Math.random() * array.length)]; } this method take an array and return randomly an element form the given array. but that works only with not primitive data types. because generics don't works with.
7th Oct 2019, 8:22 PM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar
0
I am working whit primitive data
7th Oct 2019, 8:25 PM
zang bengono Philibert stéphane
zang bengono Philibert stéphane - avatar
0
thanks for every body!!
7th Oct 2019, 8:46 PM
zang bengono Philibert stéphane
zang bengono Philibert stéphane - avatar
0
Use wrapper classes of the primitive and you van use thos method.
7th Oct 2019, 10:22 PM
Kiuziu 💘 Berlin
Kiuziu 💘 Berlin - avatar