Get Random Element From Array using Java

Random().nextInt method

package app;

import java.util.Random;

public class Main
{
    public static void main(String[] args)
    {
        Integer[] data = {10, 3, 17, 50, 15};
        int index = new Random().nextInt(data.length);
        Integer element = data[index];

        System.out.println(element);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.