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

/**
 * Finds the first index of a minimal element in a list.
 * @param list The <code>List</code> to search.
 * @return The first index of a minimal element in the list.
 *///from  w w w . jav  a2  s . c  o m
public static <T extends Comparable<? super T>> int indexOfMin(List<T> list) {
    T min = list.get(0);
    int index = 0;
    for (int i = 0, n = list.size(); i < n; i++) {
        T value = list.get(i);
        if (value.compareTo(min) < 0) {
            min = value;
            index = i;
        }
    }
    return index;
}

From source file:Main.java

public static boolean containsStrictly(List list, Object obj) {
    int index = list.indexOf(obj);
    return index == -1 ? false : list.get(index) == obj;
}

From source file:Main.java

public static <T> T qnth(List<T> sample, int n, Comparator<T> comp) {
    T pivot = sample.get(0);
    List<T> below = new ArrayList<T>(), above = new ArrayList<T>();
    for (T s : sample) {

        int flag = comp.compare(s, pivot);
        if (flag < 0) {
            above.add(s);//w w  w.  j  a va  2 s .co m
        } else if (flag > 0) {
            below.add(s);
        }
    }
    int i = below.size(), j = sample.size() - above.size();
    if (n < i)
        return qnth(below, n, comp);
    else if (n >= j)
        return qnth(above, n - j, comp);
    else
        return pivot;
}

From source file:Main.java

public static Object asListItem(Object node, String key, int idx) {
    List<?> list = asList(node, key);
    return (idx >= 0 && idx < list.size() ? list.get(idx) : null);
}

From source file:Main.java

public static <T> void print(List<List<T>> list) {
    for (int i = 0; i < list.size(); i++) {
        for (int j = 0; j < list.get(i).size(); j++) {
            System.out.print(list.get(i).get(j) + " ");
        }/*from  w  w w.  j a  va  2 s.co  m*/
        System.out.println();
    }
}

From source file:Main.java

/**
 * Return the item no <code>index</code> of <code>l</code> if it exists, otherwise
 * <code>null</code>.//from w w w .  j  av  a  2s.c o  m
 * 
 * @param <T> type of list.
 * @param l the list.
 * @param index the wanted index.
 * @return the corresponding item of <code>l</code> or <code>null</code>.
 */
public static <T> T getNoExn(List<T> l, int index) {
    return index >= 0 && index < l.size() ? l.get(index) : null;
}

From source file:Main.java

public static Employee employeeMaker() {
    List<String> names = Arrays.asList("John Doe", "Jane Doe", "Mark Twain", "Oliver Twist", "Ferdinand IV");

    return new Employee(names.get((int) (Math.random() * names.size())), Math.random() * 20_000);
}

From source file:Main.java

public static Element getChildElement(Element parent, String tagName) {
    List<Element> elements = getElements(parent, tagName);
    return (elements.size() > 0) ? elements.get(0) : null;
}

From source file:com.github.pmerienne.cf.util.DRPCUtils.java

@SuppressWarnings({ "unchecked", "rawtypes" })
public static <T extends Object> T extractSingleValue(String drpcResult, int index) {
    List<List<Object>> values = (List) JSONValue.parse(drpcResult);
    return (T) values.get(0).get(index);
}

From source file:Main.java

/**
 * This is a convenience method to return the first element from a list. If the list is empty, then null is
 * returned.//from   ww  w.  j  a  va  2s  . c o  m
 * 
 * @param list
 * @return
 */
public static <T> T getFirstElement(List<T> list) {
    if (!isEmpty(list)) {
        return list.get(0);
    }

    return null;
}