Example usage for java.util ArrayList get

List of usage examples for java.util ArrayList get

Introduction

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

Prototype

public 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> int ceil(ArrayList<T> list, T key, Comparator<? super T> c) {
    if (c.compare(list.get(0), key) >= 0) {
        return 0;
    }//www .  j ava2  s .c om
    if (c.compare(list.get(list.size() - 1), key) < 0) {
        return -1;
    }
    int start = 0, end = list.size() - 1, res;
    T mid;
    while (start < end - 1) {
        mid = list.get((start + end) / 2);
        res = c.compare(mid, key);
        //            System.out.println("res = " + res);
        //            System.out.println("mid = " + mid);
        if (res >= 0) {
            end = (start + end) / 2;
        } else {
            start = (start + end) / 2;
        }
        //            System.out.println("start = " + start);
        //            System.out.println("end = "+ end);
    }
    res = c.compare(list.get(start), key);
    if (res < 0) {
        return end;
    } else {
        if (res == 0) {
            return start;
        } else {
            return -1;
        }
    }

}

From source file:Main.java

private static void logInConsole(ArrayList<Integer> timePoints) {
    StringBuffer sb = new StringBuffer();
    for (int i = 0; i < timePoints.size(); i++) {
        sb.append(timePoints.get(i)).append(" ");
    }//from  w  w w . j  a v  a2  s .c om
    Log.d(TAG, sb.toString());
}

From source file:Main.java

/**
 *//*from   w  w  w .ja  v a2 s  .c  om*/
public static Element getElement(String name, int index, Element parent) {
    ArrayList<Element> toReturn = getElementsByTag(name, parent);
    if (index > toReturn.size() - 1) {
        return null;
    }
    return toReturn.get(index);
}

From source file:Main.java

private static String listToString(ArrayList<String> pathSpec) {
    if (pathSpec.isEmpty())
        return "";
    StringBuilder ret = new StringBuilder();
    ret.append(pathSpec.get(0));
    for (int i = 1, n = pathSpec.size(); i < n; i++)
        ret.append(File.separator).append(pathSpec.get(i));
    return ret.toString();
}

From source file:Main.java

public static ArrayList<String> cutString(ArrayList<String> list, int begin, int end) {

    ArrayList<String> newList = new ArrayList<>();

    for (int i = begin; i < end; i++) {
        newList.add(list.get(i));
    }//from ww w . j  a  va 2s . co m

    return newList;
}

From source file:Main.java

/**
 * Converts List of Characters to String.
 * @param characters input list./*from  w w w. java  2s  . co m*/
 * @return String value.
 */
public static String getText(ArrayList<Character> characters) {
    char[] chars = new char[characters.size()];

    for (int i = 0; i < chars.length; i++) {
        chars[i] = characters.get(i);
    }
    return new String(chars);
}

From source file:Main.java

public static double[][] toArray1(ArrayList<Double> a) {
    int dataDimen = a.size();
    double[][] res = new double[1][dataDimen];

    for (int i = 0; i < dataDimen; i++) {
        res[1][i] = a.get(i);
    }//from   ww w .j  a  v  a2 s.  c om

    return res;
}

From source file:Main.java

public static String getAllListValue(ArrayList<String> data) {
    String value = "";
    if (data == null)
        return value;
    for (int i = 0; i < data.size(); i++) {
        value = value + data.get(i) + (i == data.size() - 1 ? "" : ",");
    }/*w w  w.  j a  v  a2s .  c  o  m*/
    return value;
}

From source file:Main.java

/**
 * Swaps the indices "a" and "b" in the array "indices" if the corresponding
 * data values data[indices[a]] is greater than data[indices[b]].
 * @param <ComparableType> Type of data to compare to.
 * @param a first index//from   www . j  a  va  2s .co m
 * @param b second index
 * @param indices array of indices to index into "data", which is
 * modified by this method.
 * @param data ArrayList of values, unchanged.
 * @param comparator Comparator used to determine if two values
 * are greater than, less than, or equal to each other.
 * @return
 * True if swapped, false if left alone.
 */
private static <ComparableType> boolean swapIfAGreaterThanB(int a, int b, int[] indices,
        ArrayList<? extends ComparableType> data, Comparator<? super ComparableType> comparator) {
    final boolean doSwap = comparator.compare(data.get(indices[a]), data.get(indices[b])) > 0;
    if (doSwap) {
        swapArrayValues(a, b, indices);
    }
    return doSwap;

}

From source file:Main.java

/**
 * Adds the object to the list if it does not already exist
 * @param list/*from w w w  . j av  a2  s  .c  o  m*/
 * @param object
 * @return
 */
public static <T> boolean containsByReference(ArrayList<T> list, T object) {
    if (list == null)
        return false;
    int size = list.size();
    for (int i = 0; i < size; i++) {
        if (list.get(i) == object) {
            return true;
        }
    }
    return false;
}