Example usage for java.util LinkedList add

List of usage examples for java.util LinkedList add

Introduction

In this page you can find the example usage for java.util LinkedList add.

Prototype

public void add(int index, E element) 

Source Link

Document

Inserts the specified element at the specified position in this list.

Usage

From source file:Main.java

/**
 * <p>//  w  w  w. j  a  va  2  s.c o  m
 * Converts an Object reference that is known to be an array into a List.
 * Semantically very similar to {@link java.util.Arrays#asList(Object[])}
 * except that this method can deal with arrays of primitives in the same
 * manner as arrays as objects.
 * </p>
 * 
 * <p>
 * A new List is created of the same size as the array, and elements are
 * copied from the array into the List. If elements are primitives then they
 * are converted to the appropriate wrapper types in order to return a List.
 * </p>
 * 
 * @param in
 *            an array of Objects or primitives (null values are not
 *            allowed)
 * @return a List containing an element for each element in the input array
 * @throws IllegalArgumentException
 *             thrown if the in parameter is null or not an array
 */
public static List<Object> asList(Object in) {
    if (in == null || !in.getClass().isArray()) {
        throw new IllegalArgumentException("Parameter to asObjectArray must be a non-null array.");
    } else {
        int length = Array.getLength(in);
        LinkedList<Object> list = new LinkedList<Object>();
        for (int i = 0; i < length; ++i) {
            list.add(i, Array.get(in, i));
        }

        return list;
    }
}

From source file:msearch.filmeSuchen.sender.MediathekReader.java

static void listeSort(LinkedList<String[]> liste, int stelle) {
    //Stringliste alphabetisch sortieren
    GermanStringSorter sorter = GermanStringSorter.getInstance();
    if (liste != null) {
        String str1;//from w ww  .  j  av a 2s. c o  m
        String str2;
        for (int i = 1; i < liste.size(); ++i) {
            for (int k = i; k > 0; --k) {
                str1 = liste.get(k - 1)[stelle];
                str2 = liste.get(k)[stelle];
                // if (str1.compareToIgnoreCase(str2) > 0) {
                if (sorter.compare(str1, str2) > 0) {
                    liste.add(k - 1, liste.remove(k));
                } else {
                    break;
                }
            }
        }
    }
}

From source file:natalia.dymnikova.cluster.scheduler.impl.find.optimal.FindOptimalAddressesStrategy.java

private <T> List<List<T>> getAllCombines(final List<List<T>> values, final int i) {
    if (i == values.size()) {
        final List<List<T>> list = new LinkedList<>();
        list.add(emptyList());//from   w w w  .  j a  v  a2s . c o  m
        return list;
    }

    final List<List<T>> result = new LinkedList<>();
    final List<List<T>> previous = getAllCombines(values, i + 1);

    values.get(i).forEach(value -> {
        previous.forEach(combine -> {
            final LinkedList<T> e = new LinkedList<>(combine);
            e.add(0, value);
            result.add(e);
        });
    });

    return result;
}

From source file:Main.java

/**
 * Inserts into an ascending sorted list an element.
 *
 * Preconditions: The element has to implement the {@code Comparable}
 * interface and the list have to be sorted ascending. Both conditions will
 * not be checked: At runtime a class cast exception will be thrown
 * if the element does not implement the comparable interface and and if the
 * list is not sorted, the element can't be insert sorted.
 *
 * @param <T>     element type//from w w  w.  j  av a2 s  .  co  m
 * @param list
 * @param element
 */
@SuppressWarnings("unchecked")
public static <T> void binaryInsert(LinkedList<? super T> list, T element) {
    if (list == null) {
        throw new NullPointerException("list == null");
    }

    if (element == null) {
        throw new NullPointerException("element == null");
    }

    boolean isComparable = element instanceof Comparable<?>;

    if (!isComparable) {
        throw new IllegalArgumentException("Not a comparable: " + element);
    }

    int size = list.size();
    int low = 0;
    int high = size - 1;
    int index = size;
    int cmp = 1;

    while ((low <= high) && (cmp > 0)) {
        int mid = (low + high) >>> 1;
        Comparable<? super T> midVal = (Comparable<? super T>) list.get(mid);

        cmp = midVal.compareTo(element);

        if (cmp < 0) {
            low = mid + 1;
        } else if (cmp > 0) {
            high = mid - 1;
        }
    }

    for (int i = low; (i >= 0) && (i < size) && (index == size); i++) {
        Comparable<? super T> elt = (Comparable<? super T>) list.get(i);

        if (elt.compareTo(element) >= 0) {
            index = i;
        }
    }

    list.add(index, element);
}