Example usage for java.util ArrayList remove

List of usage examples for java.util ArrayList remove

Introduction

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

Prototype

public boolean remove(Object o) 

Source Link

Document

Removes the first occurrence of the specified element from this list, if it is present.

Usage

From source file:com.kaytat.simpleprotocolplayer.MainActivity.java

private ArrayList<String> getUpdatedArrayList(SharedPreferences prefs, AutoCompleteTextView view,
        String keyJson, String keySingle) {
    // Retrieve the values from the shared preferences
    ArrayList<String> arrayList = getListFromPrefs(prefs, keyJson, keySingle);

    // Make sure the most recent IP is on top
    arrayList.remove(view.getText().toString());
    arrayList.add(0, view.getText().toString());

    if (arrayList.size() >= 4) {
        arrayList.subList(4, arrayList.size()).clear();
    }// www .  j a  v a  2 s .co m

    return arrayList;
}

From source file:edu.umn.natsrl.evaluation.ContourPlotter.java

private String[] getTimes() {
    Vector<EvaluationResult> results = (Vector<EvaluationResult>) this.eval.getResult().clone();
    ArrayList<String> times = results.get(0).getColumn(0);

    // remove not time format (hh:mm:ss)
    for (int i = 0; i < times.size(); i++) {
        if (!times.get(i).matches("[0-9]+:[0-9]+:[0-9]+")) {
            times.remove(i--);
        }//from   w  w  w.  j  ava  2s. c om
    }

    return times.toArray(new String[times.size()]);
}

From source file:com.buaa.cfs.utils.StringUtils.java

/**
 * Split a string using the given separator, with no escaping performed.
 *
 * @param str       a string to be split. Note that this may not be null.
 * @param separator a separator char// w  w  w . j av a  2 s  .c o  m
 *
 * @return an array of strings
 */
public static String[] split(String str, char separator) {
    // String.split returns a single empty result for splitting the empty
    // string.
    if (str.isEmpty()) {
        return new String[] { "" };
    }
    ArrayList<String> strList = new ArrayList<String>();
    int startIndex = 0;
    int nextIndex = 0;
    while ((nextIndex = str.indexOf(separator, startIndex)) != -1) {
        strList.add(str.substring(startIndex, nextIndex));
        startIndex = nextIndex + 1;
    }
    strList.add(str.substring(startIndex));
    // remove trailing empty split(s)
    int last = strList.size(); // last split
    while (--last >= 0 && "".equals(strList.get(last))) {
        strList.remove(last);
    }
    return strList.toArray(new String[strList.size()]);
}

From source file:fr.paris.lutece.plugins.suggest.utils.SuggestUtils.java

/**
 * move a element in the list// w ww. j ava  2s  . c  om
 * @param nOldPosistion the old position
 * @param nNewPosition the new position
 * @param list The list
 */
public static void moveElement(int nOldPosistion, int nNewPosition, ArrayList<Integer> list) {
    Integer element = list.get(nOldPosistion - 1);
    list.remove(nOldPosistion - 1);
    list.add(nNewPosition - 1, element);
}

From source file:com.predic8.membrane.core.transport.http.ConnectionManager.java

private int closeOldConnections() {
    ArrayList<ConnectionKey> toRemove = new ArrayList<ConnectionKey>();
    ArrayList<Connection> toClose = new ArrayList<Connection>();
    long now = System.currentTimeMillis();
    log.trace("closing old connections");
    int closed = 0, remaining;
    synchronized (this) {
        // close connections after their timeout
        for (Map.Entry<ConnectionKey, ArrayList<OldConnection>> e : availableConnections.entrySet()) {
            ArrayList<OldConnection> l = e.getValue();
            for (int i = 0; i < l.size(); i++) {
                OldConnection o = l.get(i);
                if (o.deathTime < now) {
                    // replace [i] by [last]
                    if (i == l.size() - 1)
                        l.remove(i);
                    else
                        l.set(i, l.remove(l.size() - 1));
                    --i;//from ww w .j  a  v a  2  s. c  o  m
                    closed++;
                    toClose.add(o.connection);
                }
            }
            if (l.isEmpty())
                toRemove.add(e.getKey());
        }
        for (ConnectionKey remove : toRemove)
            availableConnections.remove(remove);
        remaining = availableConnections.size();
    }
    for (Connection c : toClose) {
        try {
            c.close();
        } catch (Exception e) {
            // do nothing
        }
    }
    if (closed != 0)
        log.debug("closed " + closed + " connections");
    return remaining;
}

From source file:mastermind.RandomGenerator.java

public String getRandomDigitStringDuplicateFree(int length) {
    if (length >= 1 && length <= 10) {
        ArrayList<Integer> values = new ArrayList<Integer>();
        String digits = "";
        int nextInt = 0;
        for (int i = 0; i < 10; i++) {
            values.add(i);//w w  w .j  av a  2s . com
        }
        do {
            nextInt = getRandomInt(values.size() - 1);
            digits += values.get(nextInt);
            values.remove(nextInt);
        } while (digits.length() != length);
        return digits;
    } else {
        return "-1";
    }
}

From source file:CodeTimer.java

/**
 * Calibrates the timer for the machine// ww  w  .j a  va  2s.  c om
 * 
 * @param numTests
 *            10000 might be about right
 */
public static void recalibrate(int numTests) {
    boolean print = false;
    CodeTimer codeTimer = new CodeTimer("calibrate", null, null);
    // warm the JIT
    for (int i = 0; i < 1024; i++) {
        codeTimer.tick("foo");
        codeTimer.lastTick(false);
    }

    // find how out long it takes to call click(), so that time can
    // be accounted for
    ArrayList<Long> selfTimeObservations = new ArrayList<Long>(numTests);
    for (int i = 0; i < numTests; i++) {
        long nanoSelfTime = -(System.nanoTime() - System.nanoTime());

        codeTimer.tick("foo");

        long t0 = System.nanoTime();
        codeTimer.tick("bar");
        long t1 = System.nanoTime();

        codeTimer.tick("baz");
        codeTimer.lastTick(false);

        long currentSelfTime = t1 - t0 - nanoSelfTime;
        if (print) {
            System.out.println("calibrating : currentSelfTime == " + currentSelfTime + ", nanoSelfTime == "
                    + nanoSelfTime);
        }
        selfTimeObservations.add(new Long(currentSelfTime));
    }
    // sort the times
    Collections.sort(selfTimeObservations);
    if (print) {
        for (int i = 0; i < selfTimeObservations.size(); i++) {
            System.out.println("calibrating : selfTimeObservations.get(i) == " + selfTimeObservations.get(i));
        }
    }
    // cut out the slowest 5% which are assumed to be outliers
    for (int i = 0; i < (int) (numTests * 0.05); i++) {
        selfTimeObservations.remove(0);
    }
    // cut out the fastest 5% which are assumed to be outliers
    for (int i = 0; i < (int) (numTests * 0.05); i++) {
        selfTimeObservations.remove(selfTimeObservations.size() - 1);
    }
    if (print) {
        System.out.println(
                "calibrating : Slimmed list: selfTimeObservations.size() == " + selfTimeObservations.size());
        for (int i = 0; i < selfTimeObservations.size(); i++) {
            System.out.println("calibrating : selfTimeObservations.get(i) == " + selfTimeObservations.get(i));
        }
    }
    // find the average
    long sumOfSelfTimes = 0;
    for (int i = 0; i < selfTimeObservations.size(); i++) {
        sumOfSelfTimes += selfTimeObservations.get(i).longValue();
    }
    SELF_TIME = sumOfSelfTimes / selfTimeObservations.size();
    if (print) {
        System.out.println("calibrating : SELF_TIME == " + SELF_TIME);
    }
}

From source file:edu.caltech.ipac.firefly.server.util.ipactable.DataGroupReader.java

public static DataGroup getEnumValues(File inf, int cutoffPoint) throws IOException {

    TableDef tableMeta = IpacTableUtil.getMetaInfo(inf);
    List<DataType> cols = tableMeta.getCols();

    HashMap<DataType, List<String>> enums = new HashMap<DataType, List<String>>();
    ArrayList<DataType> workList = new ArrayList<DataType>();

    for (DataType dt : cols) {
        if (dt.getDataType().isAssignableFrom(Float.class) || dt.getDataType().isAssignableFrom(Double.class)) {
            continue;
        }/*www.  j av a 2 s .  c o  m*/
        enums.put(dt, new ArrayList<String>());
        workList.add(dt);
    }
    DataGroup dg = new DataGroup(null, cols);

    BufferedReader reader = new BufferedReader(new FileReader(inf), IpacTableUtil.FILE_IO_BUFFER_SIZE);
    String line = null;
    int lineNum = 0;
    try {
        line = reader.readLine();
        lineNum++;
        while (line != null) {
            DataObject row = IpacTableUtil.parseRow(dg, line, false);
            if (row != null) {
                List<DataType> ccols = new ArrayList<DataType>(workList);
                for (DataType dt : ccols) {
                    String v = String.valueOf(row.getDataElement(dt));
                    List<String> l = enums.get(dt);
                    if (l == null || l.size() >= cutoffPoint
                            || (dt.getDataType().isAssignableFrom(String.class) && v.length() > 20)) {
                        workList.remove(dt);
                        enums.remove(dt);
                    } else if (!l.contains(v)) {
                        l.add(v);
                    }
                }
            }
            line = reader.readLine();
            lineNum++;
            if (enums.size() == 0) {
                break;
            }
        }
    } catch (Exception e) {
        String msg = e.getMessage() + "<br>on line " + lineNum + ": " + line;
        if (msg.length() > 128)
            msg = msg.substring(0, 128) + "...";
        logger.error(e, "on line " + lineNum + ": " + line);
        throw new IOException(msg);
    } finally {
        reader.close();
    }

    if (enums.size() > 0) {
        for (DataType dt : enums.keySet()) {
            List<String> values = enums.get(dt);
            Collections.sort(values, DataGroupUtil.getComparator(dt));
            dg.addAttribute(
                    TemplateGenerator.createAttributeKey(TemplateGenerator.Tag.ITEMS_TAG, dt.getKeyName()),
                    StringUtils.toString(values, ","));
        }
    }

    return dg;
}

From source file:de.ids_mannheim.korap.index.FieldDocument.java

/**
 * Deserialize token stream data (LEGACY).
 *//*  w  w  w.  j a v a2 s.  co m*/
public void setFields(ArrayList<Map<String, Object>> fields) {
    Map<String, Object> primary = fields.remove(0);
    this.setPrimaryData((String) primary.get("primaryData"));

    for (Map<String, Object> field : fields) {

        String fieldName = (String) field.get("name");
        MultiTermTokenStream mtts = this.newMultiTermTokenStream();

        for (ArrayList<String> token : (ArrayList<ArrayList<String>>) field.get("data")) {

            try {
                MultiTermToken mtt = new MultiTermToken(token.remove(0));

                for (String term : token) {
                    mtt.add(term);
                }
                ;

                mtts.addMultiTermToken(mtt);
            } catch (CorpusDataException cde) {
                this.addError(cde.getErrorCode(), cde.getMessage());
            }
            ;
        }
        ;

        // TODO: This is normally dependend to the tokenization!
        //       Add this as meta information to the document
        // Store this information as well as tokenization information
        // as meta fields in the tokenization term vector
        if (field.containsKey("foundries")) {
            // TODO: Do not store positions!
            String foundries = (String) field.get("foundries");
            this.addKeyword("foundries", foundries);
            super.setFoundries(foundries);
        }
        ;
        if (field.containsKey("tokenization")) {
            String tokenization = (String) field.get("tokenization");
            this.addString("tokenization", tokenization);
            super.setTokenization(tokenization);
        }
        ;

        this.addTV(fieldName, this.getPrimaryData(), mtts);
    }
    ;
}

From source file:css.variable.converter.CSSVariableConverter.java

/**
 * Go through existing CSS Files and replace variable access with variable
 * values//  w  ww  .java  2s  . c o m
 *
 * @param rootCSSVars All css variables you want to convert from name to value
 */
private static void editForRelease(ArrayList<CSSVar> rootCSSVars) {
    for (File cssFile : theCSSList) {

        ArrayList<CSSVar> localCSSVars = getCSSVars(cssFile, ":local");
        for (CSSVar cssVar : rootCSSVars) {
            if (!localCSSVars.contains(cssVar)) {
                localCSSVars.add((CSSVar) (cssVar.clone()));
            }
        }

        // This will store info the new text for the file we will write below
        ArrayList<String> filesNewInfo = new ArrayList<String>();
        try {

            Scanner fileReader = new Scanner(cssFile);
            while (fileReader.hasNextLine()) {

                String currentLine = fileReader.nextLine();

                // If we find variables, replace them, with their value
                if (currentLine.contains("var(")) {
                    for (CSSVar var : localCSSVars) {
                        while (currentLine.contains(var.getName())) {
                            currentLine = currentLine.replace("var(" + var.getName() + ")", var.getValue());
                        }
                    }
                }
                // Add new currentLine to be written
                filesNewInfo.add(currentLine);
            }
            fileReader.close();
        } catch (FileNotFoundException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }

        // Write the new files below
        try {
            BufferedWriter writer = new BufferedWriter(new FileWriter(cssFile.getPath()));

            while (!filesNewInfo.isEmpty()) {
                writer.write(filesNewInfo.get(0));
                writer.newLine();
                filesNewInfo.remove(0);
            }
            writer.close();
        } catch (IOException ex) {
            Logger.getLogger(CSSVariableConverter.class.getName()).log(Level.SEVERE, null, ex);
        }
    }
}