Obtener elemento aleatorio de la matriz de cadenas Java
String random = (fruits[new Random().nextInt(fruits.length)]);
Yucky Yak
String random = (fruits[new Random().nextInt(fruits.length)]);
import java.util.Random;
public class Selection
{
public static void main(String[] args)
{
Random rand = new Random();
String[] animals = {"Dog", "Cat", "Fish", "Turtle", "Penguin"}; // array of animals
System.out.println("Console picked " + animals[rand.nextInt(animals.length)]); // prints the random animal selected from array
}
}
String[] fruits = {"Apple","Mango","Peach","Banana","Orange","Grapes","Watermelon","Tomato"};