Example usage for java.util List get

List of usage examples for java.util List get

Introduction

In this page you can find the example usage for java.util List get.

Prototype

E get(int index);

Source Link

Document

Returns the element at the specified position in this list.

Usage

From source file:Main.java

public static <T> T pickRandomItem(List<T> items) {
    return items.get(new Random().nextInt(items.size()));
}

From source file:Main.java

public static <T> T last(List<T> list) {
    return list.get(list.size() - 1);
}

From source file:Main.java

public static <T> T firstOrNull(List<T> list) {
    return list.size() > 0 ? list.get(0) : null;
}

From source file:Main.java

public static <T> void exchange(List<T> a, int i, int j) {
    T temp = a.get(i);
    a.set(i, a.get(j));/*from w  w  w.j  a v  a2 s .co  m*/
    a.set(j, temp);
}

From source file:Main.java

public static <T> T chooseRandomObject(List<T> list, Random rnd) {
    return list.get(rnd.nextInt(list.size()));
}

From source file:Main.java

public static <T> T random(List<T> list) {
    return list.get((int) Math.floor(Math.random() * list.size()));
}

From source file:Main.java

public static int getMaxWidth(List<String> list) {
    int max_width = list.get(0).length();
    for (String line : list) {
        if (line.length() > max_width)
            max_width = line.length();//from   w ww .  j av  a 2  s  . co m
    }

    return max_width;
}

From source file:Main.java

/**
 * /*w w w.  j a va 2s. c om*/
 * @param <T>
 * @param list
 * @return
 */
public static <T> T getFirstElementFromList(List<T> list) {
    return list.size() > 0 ? list.get(0) : null;
}

From source file:Main.java

public static <A> A head(List<A> list) {
    return list.get(0);
}

From source file:Main.java

/**
 * Returns a random element a list.//from w w  w  .j  av  a 2s. c  o m
 *
 * @param list  The list to choose from
 * @return A random element from the list
 */
public static <T> T choice(List<T> list) {
    return list.get(GENERATOR.nextInt(list.size()));
}