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[ ]
4 Respuestas
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.
0
I am working whit primitive data
0
thanks for every body!!
0
Use wrapper classes of the primitive and you van use thos method.