get random element from string array java 1

get random element from string array java

String random = (fruits[new Random().nextInt(fruits.length)]);

Here is what the above code is Doing:
1. We create an array of strings called fruits.
2. We create a new Random object.
3. We use the Random object to generate a random number between 0 and the length of the fruits array.
4. We use the random number to get a random fruit from the fruits array.
5. We print the random fruit to the console.

Similar Posts