Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.List;

import java.util.Random;

public class Main {
    private static final Random RANDOM = new Random();

    /**
     * Random parameter returns random item from array.
     * 
     * @param <E>
     *            Class for array where you get random item.
     * @param e
     *            A random parameter for the array.
     * @return A random item from an array.
     */
    public static <E> E randomItem(E[] e) {
        if (e.length == 0) {
            return null;
        }
        return e[RANDOM.nextInt(e.length)];
    }

    /**
     * Random parameter returns random item from list.
     * 
     * @param <E>
     *            Class for list where you get random item.
     * @param e
     *            A random parameter for the array.
     * @return A random item from a list.
     */
    public static <E> E randomItem(List<E> e) {
        if (e.isEmpty()) {
            return null;
        }
        return e.get(RANDOM.nextInt(e.size()));
    }
}