All Downloads are FREE. Search and download functionalities are using the official Maven repository.

utils.arrays.ArrayUtilities Maven / Gradle / Ivy

There is a newer version: 1.6.9
Show newest version
package utils.arrays;

import java.util.List;
import java.util.Random;

/**
 * Utility class for array-related operations.
 *
 * @author Egecan Sen
 * @version 1.6.2 (Documented in 1.6.2, released in an earlier version)
 */
public class ArrayUtilities {

    /**
     * Returns a random item from the provided list.
     *
     * @param  the type of elements in the list
     * @param items      the list from which to select a random item
     * @return a randomly selected item from the list
     * @throws IllegalArgumentException if the list is empty
     */
    public static  ItemType getRandomItemFrom(List items) {
        if (items.isEmpty()) {
            throw new IllegalArgumentException("Cannot get a random item from an empty list");
        }
        int randomIndex = new Random().nextInt(items.size());
        return items.get(randomIndex);
    }
}





© 2015 - 2025 Weber Informatics LLC | Privacy Policy