Example usage for java.util ArrayList ensureCapacity

List of usage examples for java.util ArrayList ensureCapacity

Introduction

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

Prototype

public void ensureCapacity(int minCapacity) 

Source Link

Document

Increases the capacity of this ArrayList instance, if necessary, to ensure that it can hold at least the number of elements specified by the minimum capacity argument.

Usage

From source file:MainClass.java

public static void main(String[] a) {

    ArrayList list = new ArrayList();
    list.add("A");
    list.ensureCapacity(10);

    System.out.println(list.size());
}

From source file:MainClass.java

public static void main(String[] a) {

    ArrayList list = new ArrayList();

    list.add("A");
    list.ensureCapacity(10);
    list.trimToSize();/*from   w  w  w .j  a  v  a2s.c o  m*/
}

From source file:Main.java

public static void main(String args[]) {

    ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

    arrlist.add(10);/*  www  . ja va2 s . c om*/
    arrlist.add(50);
    arrlist.add(30);

    arrlist.ensureCapacity(5000);

}

From source file:MainClass.java

public static void main(String[] args) {
    ArrayList myList = new ArrayList(5);
    for (int i = 0; i < 5; i++) {
        myList.add(new Integer(i));
    }/*ww  w .jav a  2s .c om*/
    System.out.println("List contains " + myList.size() + " elements");

    Integer int2 = new Integer(2);
    System.out.println("List contains Integer(2): " + myList.contains(int2));
    System.out.println("Integer(2) is at index " + myList.indexOf(int2));

    myList.set(2, new Integer(99));
    System.out.println("Get element at index 2: " + myList.get(2));

    myList.ensureCapacity(15);
    for (int i = 10; i < 15; i++) {
        myList.add(new Integer(i));
    }

    myList.subList(10, 15).clear();
    myList.trimToSize();

    System.out.println(myList);
}

From source file:Main.java

public static <T> ArrayList<T> resize(ArrayList<T> list, int size) {
    if (list == null)
        list = new ArrayList<T>(size);
    list.ensureCapacity(size);
    for (int i = 0; i < size; i++)
        list.add(null);//from  w ww.j a  v a  2  s.com
    return list;
}

From source file:Main.java

/**
 * Returns map values summary list with unique elements only.
 *
 * @param map map to process/*from  ww  w  . j  a  v a 2 s.c  o m*/
 * @param <K> key object type
 * @param <V> value object type
 * @return map values summary list with unique elements only
 */
public static <K, V> ArrayList<V> valuesSummaryList(final Map<K, List<V>> map) {
    final ArrayList<V> summary = new ArrayList<V>(0);
    for (final Map.Entry<K, List<V>> entry : map.entrySet()) {
        final List<V> list = entry.getValue();
        summary.ensureCapacity(summary.size() + list.size());
        for (final V value : list) {
            if (!summary.contains(value)) {
                summary.add(value);
            }
        }
    }
    return summary;
}

From source file:org.bultreebank.labpipe.tools.SVMTagger.java

/**
 * Tags Line encoded String/*from  ww w  . j  av a 2s .  c  om*/
 * 
 * @param   lines   Line encoded data
 * @param   options LABPipe configuration
 * 
 * @return  String  - Line encoded tagged data
 * @throws SVMTConnectionExceptoin
 * @throws IncorrectInputException
 * @throws MissingContentException  
 */
public static String tagLinesString(String lines, Configuration options)
        throws SVMTConnectionExceptoin, IncorrectInputException, MissingContentException {
    BufferedReader br = null;
    try {
        StringBuilder sb = new StringBuilder();
        ArrayList<String> sentence = new ArrayList();
        sentence.ensureCapacity(100);
        br = new BufferedReader(new InputStreamReader(
                new ByteArrayInputStream(lines.getBytes(ServiceConstants.PIPE_CHARACTER_ENCODING))));
        String line = "";

        String eos = options.getProperty(Configuration.EOS_TOKEN);

        while ((line = br.readLine()) != null) {

            if (line.startsWith(eos)) {
                if (sentence.isEmpty()) {
                    continue;
                }
                sb.append(Misc.joinColumns(sentence.toArray((String[]) new String[0]),
                        SVMTagger.tagList(sentence, options), " "));
                sb.append(eos);
                sb.append("\n");
                sentence = new ArrayList();
                sentence.ensureCapacity(100);
            } else {
                if (line.trim().length() > 0 && !line.trim().equals("")) {
                    sentence.add(line);
                }
            }

        }

        if (sentence.size() > 0) {
            sb.append(Misc.joinColumns(sentence.toArray((String[]) new String[0]),
                    SVMTagger.tagList(sentence, options), " "));
            sb.append(eos);
            sb.append("\n");
        }

        br.close();
        return sb.toString();
    } catch (UnsupportedEncodingException ex) {
        logger.log(Level.SEVERE, ServiceConstants.EXCEPTION_UNSUPPORTED_ENCODING, ex);
    } catch (IOException ex) {
        logger.log(Level.SEVERE, ServiceConstants.EXCEPTION_IO, ex);
    } finally {
        try {
            br.close();
        } catch (IOException ex) {
            logger.log(Level.SEVERE, ServiceConstants.EXCEPTION_IO, ex);
        }
    }

    return null;
}

From source file:org.muckebox.android.net.RefreshHelper.java

public static Integer refreshTracks(long albumId) {
    try {/*from  ww w.  ja  v  a2 s .c  om*/
        ArrayList<ContentProviderOperation> operations = new ArrayList<ContentProviderOperation>(1);

        JSONArray json = ApiHelper.callApiForArray("tracks", null, new String[] { "album" },
                new String[] { Long.toString(albumId) });
        operations.ensureCapacity(operations.size() + json.length() + 1);

        operations.add(ContentProviderOperation
                .newDelete(Uri.withAppendedPath(MuckeboxProvider.URI_TRACKS_ALBUM, Long.toString(albumId)))
                .build());

        for (int j = 0; j < json.length(); ++j) {
            JSONObject o = json.getJSONObject(j);
            ContentValues values = new ContentValues();

            values.put(TrackEntry.SHORT_ID, o.getInt("id"));
            values.put(TrackEntry.SHORT_ARTIST_ID, o.getInt("artist_id"));
            values.put(TrackEntry.SHORT_ALBUM_ID, o.getInt("album_id"));

            values.put(TrackEntry.SHORT_TITLE, o.getString("title"));

            if (!o.isNull("tracknumber"))
                values.put(TrackEntry.SHORT_TRACKNUMBER, o.getInt("tracknumber"));

            if (!o.isNull("discnumber"))
                values.put(TrackEntry.SHORT_DISCNUMBER, o.getInt("discnumber"));

            values.put(TrackEntry.SHORT_LABEL, o.getString("label"));
            values.put(TrackEntry.SHORT_CATALOGNUMBER, o.getString("catalognumber"));

            values.put(TrackEntry.SHORT_LENGTH, o.getInt("length"));
            values.put(TrackEntry.SHORT_DATE, o.getString("date"));

            operations.add(ContentProviderOperation
                    .newDelete(
                            Uri.withAppendedPath(MuckeboxProvider.URI_TRACKS, Integer.toString(o.getInt("id"))))
                    .build());
            operations.add(
                    ContentProviderOperation.newInsert(MuckeboxProvider.URI_TRACKS).withValues(values).build());
        }

        Log.d(LOG_TAG, "Got " + json.length() + " Tracks");

        Muckebox.getAppContext().getContentResolver().applyBatch(MuckeboxProvider.AUTHORITY, operations);
    } catch (AuthenticationException e) {
        return R.string.error_authentication;
    } catch (SSLException e) {
        return R.string.error_ssl;
    } catch (IOException e) {
        Log.d(LOG_TAG, "IOException: " + e.getMessage());
        return R.string.error_reload_tracks;
    } catch (JSONException e) {
        return R.string.error_json;
    } catch (RemoteException e) {
        e.printStackTrace();
        return R.string.error_reload_tracks;
    } catch (OperationApplicationException e) {
        e.printStackTrace();
        return R.string.error_reload_tracks;
    }

    return null;
}

From source file:biomine.bmvis2.pipeline.EdgeLabelOperation.java

public void doOperation(VisualGraph g) throws GraphOperationException {
    Set<String> avail = g.getAvailableEdgeLabels();
    for (VisualEdge n : g.getEdges()) {
        BMEdge bmEdge = (BMEdge) g.getBMEntity(n);
        ArrayList<String> labels = new ArrayList<String>();
        labels.ensureCapacity(enabledLabels.size());
        if (bmEdge != null) {
            for (String lbl : avail) {
                if (enabledLabels.contains(lbl)) {
                    String l = bmEdge.get(lbl);
                    if (l != null)
                        labels.add(lbl.substring(0, 3) + ": " + l);
                }/*w w  w  . j  a va2  s.c  o  m*/
            }
            n.setExtraLabels(labels);
        }
    }
}

From source file:biomine.bmvis2.pipeline.NodeLabelOperation.java

public void doOperation(VisualGraph g) throws GraphOperationException {
    Set<String> avail = g.getAvailableNodeLabels();
    for (VisualNode n : g.getNodes()) {
        BMNode nod = n.getBMNode();/*from  w w  w.  j av  a2  s .c o  m*/
        ArrayList<String> labels = new ArrayList<String>();
        labels.ensureCapacity(enabledLabels.size());
        if (enabledLabels.contains("type") && !n.getShowNodeType())
            labels.add(n.getType());
        if (nod != null) {
            for (String lbl : avail) {
                if (enabledLabels.contains(lbl)) {
                    String l = nod.get(lbl);
                    if (l != null)
                        labels.add(lbl.substring(0, 3) + ": " + l);
                }
            }
        }
        n.setExtraLabels(labels);
    }
}