Example usage for java.util List set

List of usage examples for java.util List set

Introduction

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

Prototype

E set(int index, E element);

Source Link

Document

Replaces the element at the specified position in this list with the specified element (optional operation).

Usage

From source file:Main.java

static <T extends Comparable<T>> void nthElement(int start, int n, int end, List<T> list) {
    List<T> newList = new ArrayList<T>();
    for (int i = start; i < end; i++)
        newList.add(list.get(i));// w  ww  . j a  v a2  s  .  c  om
    Collections.sort(newList);
    for (int i = start, j = 0; i < end; i++, j++)
        list.set(i, newList.get(j));
}

From source file:Main.java

private static <T extends Comparable<T>> void eqBrute(List<T> list, int lo, int hi) {
    if ((hi - lo) == 1) {
        if (list.get(hi).compareTo(list.get(lo)) < 0) {
            T e = list.get(lo);/*from  w ww . j  a v  a 2  s.  co  m*/
            list.set(lo, list.get(hi));
            list.set(hi, e);
        }
    } else if ((hi - lo) == 2) {
        int pmin = list.get(lo).compareTo(list.get(lo + 1)) < 0 ? lo : lo + 1;
        pmin = list.get(pmin).compareTo(list.get(lo + 2)) < 0 ? pmin : lo + 2;
        if (pmin != lo) {
            T e = list.get(lo);
            list.set(lo, list.get(pmin));
            list.set(pmin, e);
        }
        eqBrute(list, lo + 1, hi);
    } else if ((hi - lo) == 3) {
        int pmin = list.get(lo).compareTo(list.get(lo + 1)) < 0 ? lo : lo + 1;
        pmin = list.get(pmin).compareTo(list.get(lo + 2)) < 0 ? pmin : lo + 2;
        pmin = list.get(pmin).compareTo(list.get(lo + 3)) < 0 ? pmin : lo + 3;
        if (pmin != lo) {
            T e = list.get(lo);
            list.set(lo, list.get(pmin));
            list.set(pmin, e);
        }
        int pmax = list.get(hi).compareTo(list.get(hi - 1)) > 0 ? hi : hi - 1;
        pmax = list.get(pmax).compareTo(list.get(hi - 2)) > 0 ? pmax : hi - 2;
        if (pmax != hi) {
            T e = list.get(hi);
            list.set(hi, list.get(pmax));
            list.set(pmax, e);
        }
        eqBrute(list, lo + 1, hi - 1);
    }
}

From source file:Main.java

public static <E> void swapAll(final List<E> list, final int off, final int len) {
    if (list instanceof RandomAccess) {
        final int last = off + len - 1;
        for (int i = off, j = last; i < j; i++, j--) {
            list.set(i, list.set(j, list.get(i)));
        }//from  www. j ava  2 s .c  o  m
    } else {
        final int end = off + len;
        final ListIterator<E> iteratorI = list.listIterator(off);
        final ListIterator<E> iteratorJ = list.listIterator(end);
        E tmp;

        while (iteratorI.nextIndex() < iteratorJ.nextIndex()) {
            tmp = iteratorI.next();
            iteratorI.set(iteratorJ.previous());
            iteratorJ.set(tmp);
        }
    }
}

From source file:Main.java

private static <T> void eqBrute(List<T> list, int lo, int hi, Comparator<? super T> c) {
    if ((hi - lo) == 1) {
        if (c.compare(list.get(hi), list.get(lo)) < 0) {
            T e = list.get(lo);/*from   w  w w  . j a  v a  2 s  . c o m*/
            list.set(lo, list.get(hi));
            list.set(hi, e);
        }
    } else if ((hi - lo) == 2) {
        int pmin = c.compare(list.get(lo), list.get(lo + 1)) < 0 ? lo : lo + 1;
        pmin = c.compare(list.get(pmin), list.get(lo + 2)) < 0 ? pmin : lo + 2;
        if (pmin != lo) {
            T e = list.get(lo);
            list.set(lo, list.get(pmin));
            list.set(pmin, e);
        }
        eqBrute(list, lo + 1, hi, c);
    } else if ((hi - lo) == 3) {
        int pmin = c.compare(list.get(lo), list.get(lo + 1)) < 0 ? lo : lo + 1;
        pmin = c.compare(list.get(pmin), list.get(lo + 2)) < 0 ? pmin : lo + 2;
        pmin = c.compare(list.get(pmin), list.get(lo + 3)) < 0 ? pmin : lo + 3;
        if (pmin != lo) {
            T e = list.get(lo);
            list.set(lo, list.get(pmin));
            list.set(pmin, e);
        }
        int pmax = c.compare(list.get(hi), list.get(hi - 1)) > 0 ? hi : hi - 1;
        pmax = c.compare(list.get(pmax), list.get(hi - 2)) > 0 ? pmax : hi - 2;
        if (pmax != hi) {
            T e = list.get(hi);
            list.set(hi, list.get(pmax));
            list.set(pmax, e);
        }
        eqBrute(list, lo + 1, hi - 1, c);
    }
}

From source file:Main.java

public static <T> void nthElement(int start, int n, int end, List<T> list, Comparator<T> comparator) {
    List<T> newList = new ArrayList<T>();
    for (int i = start; i < end; i++)
        newList.add(list.get(i));// w w w .  ja v a2s  . c  o  m
    Collections.sort(newList, comparator);
    for (int i = start, j = 0; i < end; i++, j++)
        list.set(i, newList.get(j));
}

From source file:Main.java

/**
 * Parses the _revisions dict from a document into an array of revision ID strings
 *///  w w  w .  java 2s  .c  o  m
public static List<String> parseCouchDBRevisionHistory(Map<String, Object> docProperties) {
    Map<String, Object> revisions = (Map<String, Object>) docProperties.get("_revisions");
    if (revisions == null) {
        return null;
    }
    List<String> revIDs = (List<String>) revisions.get("ids");
    Integer start = (Integer) revisions.get("start");
    if (start != null) {
        for (int i = 0; i < revIDs.size(); i++) {
            String revID = revIDs.get(i);
            revIDs.set(i, Integer.toString(start--) + "-" + revID);
        }
    }
    return revIDs;
}

From source file:com.intuit.karate.JsonUtils.java

public static void setValueByPath(DocumentContext doc, String path, Object value) {
    if ("$".equals(path)) {
        throw new RuntimeException("cannot replace root path $");
    }/* w w w  . ja  v a  2s .c  om*/
    Pair<String, String> pathLeaf = getParentAndLeafPath(path);
    String left = pathLeaf.getLeft();
    String right = pathLeaf.getRight();
    if (right.endsWith("]")) { // json array
        int indexPos = right.lastIndexOf('[');
        int index = Integer.valueOf(right.substring(indexPos + 1, right.length() - 1));
        right = right.substring(0, indexPos);
        List list;
        String listPath = left + "." + right;
        try {
            list = doc.read(listPath);
            if (index < list.size()) {
                list.set(index, value);
            } else {
                list.add(value);
            }
        } catch (Exception e) {
            logger.trace("will create non-existent path: {}", listPath);
            list = new ArrayList();
            list.add(value);
            doc.put(left, right, list);
        }
    } else {
        doc.put(left, right, value);
    }
    logger.trace("after set: {}", doc.jsonString());
}

From source file:Main.java

/**
 * Unify the objects in the list according to the hash code IMPORTANT :
 * hashcode usage here is different from the Java Spec , IT SHOULD BE
 * UNIUQUE FOR EACH OBJECT WHICH SHOULD HOLD SOMTHING LIKE THE DATABASE
 * PRIMARY KEY.//from w ww . j  a  v a 2 s .c om
 *
 * @param hash
 *            the hash
 * @param list
 *            the list
 */
public static void unifyReferences(final Hashtable hash, final List list) {
    if (list != null) {
        for (int i = 0; i < list.size(); i++) {
            final Object itemAtList = list.get(i);
            final Object unifiedReferences = unifyReferences(hash, itemAtList);
            list.set(i, unifiedReferences);
        }
    }
}

From source file:com.fegati.utilities.CSVPreprocessor.java

private static void cleanTailingEmptyStrings(List<String[]> rows) {
    for (int i = 0; i < rows.size(); i++) {
        rows.set(i, removeTailingEmptyStringsInRow(rows.get(i)));
    }//from   w w w .j  a v a2  s  .  c o m
}

From source file:Main.java

public static <T> void setElement(List<T> list, int index, T value) {
    if (index == list.size()) {
        list.add(value);/*from ww  w. j  a  va2  s . co m*/
    } else if (index > list.size()) {
        for (int i = list.size(); i < index; i++) {
            list.add(null);
        }
        list.add(value);
    } else {
        list.set(index, value);
    }
}