java.util.ArrayList.ensureCapacity()方法的使用及代码示例

x33g5p2x  于2022-01-15 转载在 其他  
字(9.5k)|赞(0)|评价(0)|浏览(277)

本文整理了Java中java.util.ArrayList.ensureCapacity()方法的一些代码示例,展示了ArrayList.ensureCapacity()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。ArrayList.ensureCapacity()方法的具体详情如下:
包路径:java.util.ArrayList
类名称:ArrayList
方法名:ensureCapacity

ArrayList.ensureCapacity介绍

[英]Ensures that after this operation the ArrayList can hold the specified number of elements without further growing.
[中]确保在此操作之后,ArrayList可以保存指定数量的元素,而不会进一步增长。

代码示例

代码示例来源:origin: skylot/jadx

protected void addAll(Collection<? extends JNode> nodes) {
  rows.ensureCapacity(rows.size() + nodes.size());
  rows.addAll(nodes);
  if (!addDescColumn) {
    for (JNode row : rows) {
      if (row.hasDescString()) {
        addDescColumn = true;
        break;
      }
    }
  }
}

代码示例来源:origin: apache/ignite

/**
 * Ensure counters capacity and initial state.
 *
 * @param minCap Desired minimum capacity.
 */
private void capacity(int minCap) {
  counters.ensureCapacity(minCap);
  while (counters.size() < minCap)
    counters.add(0);
}

代码示例来源:origin: hibernate/hibernate-orm

/**
 * Read this object state back in from the given stream as part of de-serialization
 * 
 * @param in The stream from which to read our serial state
 */
@Override
@SuppressWarnings("unchecked")
public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException {
  sorted = in.readBoolean();
  final int numberOfExecutables = in.readInt();
  executables.ensureCapacity( numberOfExecutables );
  if ( numberOfExecutables > 0 ) {
    for ( int i = 0; i < numberOfExecutables; i++ ) {
      E e = (E) in.readObject();
      executables.add( e );
    }
  }
  final int numberOfQuerySpaces = in.readInt();
  if ( numberOfQuerySpaces < 0 ) {
    this.querySpaces = null;
  }
  else {
    querySpaces = new HashSet<Serializable>( CollectionHelper.determineProperSizing( numberOfQuerySpaces ) );
    for ( int i = 0; i < numberOfQuerySpaces; i++ ) {
      querySpaces.add( in.readUTF() );
    }
  }
}

代码示例来源:origin: naman14/Timber

private void addToPlayList(final long[] list, int position, long sourceId, TimberUtils.IdType sourceType) {
  final int addlen = list.length;
  if (position < 0) {
    mPlaylist.clear();
    position = 0;
  }
  mPlaylist.ensureCapacity(mPlaylist.size() + addlen);
  if (position > mPlaylist.size()) {
    position = mPlaylist.size();
  }
  final ArrayList<MusicPlaybackTrack> arrayList = new ArrayList<MusicPlaybackTrack>(addlen);
  for (int i = 0; i < list.length; i++) {
    arrayList.add(new MusicPlaybackTrack(list[i], sourceId, sourceType, i));
  }
  mPlaylist.addAll(position, arrayList);
  if (mPlaylist.size() == 0) {
    closeCursor();
    notifyChange(META_CHANGED);
  }
}

代码示例来源:origin: wildfly/wildfly

@Override
  public void run() {
    while (true) {
      Pair p;
      synchronized (filesToCompare) {
        if (!filesToCompare.hasNext()) {
          break;
        }
        p = filesToCompare.next();
      }
      compareFiles(args[p.getX()], args[p.getY()], messageDeliverOrder);
      int size = messageDeliverOrder.size();
      messageDeliverOrder.clear();
      messageDeliverOrder.ensureCapacity(size);
    }            
    
    System.out.println(getName() + " finished!");
  }
}

代码示例来源:origin: apache/ignite

/**
 * Initialize {@link #args} and increase its capacity and size up to given argument if needed.
 * @param size new expected size.
 */
private void ensureArgsSize(int size) {
  if (args == null)
    args = new ArrayList<>(size);
  args.ensureCapacity(size);
  while (args.size() < size)
    args.add(null);
}

代码示例来源:origin: aa112901/remusic

public synchronized ArrayList<Playlist> getNetPlaylist() {
  ArrayList<Playlist> results = new ArrayList<>();
  Cursor cursor = null;
  try {
    cursor = mMusicDatabase.getReadableDatabase().query(PlaylistInfoColumns.NAME, null,
        null, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
      results.ensureCapacity(cursor.getCount());
      do {
        if (!cursor.getString(4).equals("local"))
          results.add(new Playlist(cursor.getLong(0), cursor.getString(1), cursor.getInt(2), cursor.getString(3), cursor.getString(4)));
      } while (cursor.moveToNext());
    }
    return results;
  } finally {
    if (cursor != null) {
      cursor.close();
      cursor = null;
    }
  }
}

代码示例来源:origin: aa112901/remusic

private void addToPlayList(final long[] list, int position) {
  final int addlen = list.length;
  if (position < 0) {
    mPlaylist.clear();
    position = 0;
  }
  mPlaylist.ensureCapacity(mPlaylist.size() + addlen);
  if (position > mPlaylist.size()) {
    position = mPlaylist.size();
  }
  final ArrayList<MusicTrack> arrayList = new ArrayList<MusicTrack>(addlen);
  for (int i = 0; i < list.length; i++) {
    arrayList.add(new MusicTrack(list[i], i));
  }
  mPlaylist.addAll(position, arrayList);
  if (mPlaylist.size() == 0) {
    closeCursor();
    notifyChange(META_CHANGED);
  }
}

代码示例来源:origin: protostuff/protostuff

protected static <T> void grow(ArrayList<T> list, int size)
{
  int previousSize = list.size();
  list.ensureCapacity(size);
  List<T> l = Collections.nCopies(size - previousSize, null);
  list.addAll(l);
}

代码示例来源:origin: alibaba/Tangram-Android

/**
 * append new cards into adapter
 *
 * @param cards new cards will be added to the end
 */
public void appendGroup(@Nullable List<L> cards) {
  if (cards == null || cards.size() == 0)
    return;
  createSnapshot();
  final List<LayoutHelper> helpers = new LinkedList<>(getLayoutHelpers());
  mCards.ensureCapacity(mCards.size() + cards.size());
  helpers.addAll(transformCards(cards, mData, mCards));
  setLayoutHelpers(helpers);
  diffWithSnapshot();
  // not use {@link notifyItemRangeChanged} because not sure whether animations is required
  // notifyItemRangeChanged(oldItem, newitems);
  notifyDataSetChanged();
}

代码示例来源:origin: stackoverflow.com

public static void ensureSize(ArrayList<?> list, int size) {
  // Prevent excessive copying while we're adding
  list.ensureCapacity(size);
  while (list.size() < size) {
    list.add(null);
  }
}

代码示例来源:origin: aa112901/remusic

public ArrayList<MusicTrack> getPlaylist(final long playlistid) {
  ArrayList<MusicTrack> results = new ArrayList<>();
  Cursor cursor = null;
  try {
    cursor = mMusicDatabase.getReadableDatabase().query(PlaylistsColumns.NAME, null,
        PlaylistsColumns.PLAYLIST_ID + " = " + String.valueOf(playlistid), null, null, null, PlaylistsColumns.TRACK_ORDER + " ASC ", null);
    if (cursor != null && cursor.moveToFirst()) {
      results.ensureCapacity(cursor.getCount());
      do {
        results.add(new MusicTrack(cursor.getLong(1), cursor.getInt(0)));
      } while (cursor.moveToNext());
    }
    return results;
  } finally {
    if (cursor != null) {
      cursor.close();
      cursor = null;
    }
  }
}

代码示例来源:origin: jamesagnew/hapi-fhir

private boolean addToHeldComments(int valueIdx, List<String> theCommentsToAdd, ArrayList<ArrayList<String>> theListToAddTo) {
  if (theCommentsToAdd.size() > 0) {
    theListToAddTo.ensureCapacity(valueIdx);
    while (theListToAddTo.size() <= valueIdx) {
      theListToAddTo.add(null);
    }
    if (theListToAddTo.get(valueIdx) == null) {
      theListToAddTo.set(valueIdx, new ArrayList<String>());
    }
    theListToAddTo.get(valueIdx).addAll(theCommentsToAdd);
    return true;
  }
  return false;
}

代码示例来源:origin: wildfly/wildfly

public SimpleName addAll(final int pos, final Name name) {
  if (name instanceof SimpleName) {
    segments.addAll(pos, ((SimpleName) name).segments);
    return this;
  }
  final ArrayList<String> segments = this.segments;
  final int size = name.size();
  final int ourSize = segments.size();
  segments.ensureCapacity(ourSize + size);
  for (int i = 0; i < size; i ++) {
    add(pos + i, name.get(i));
  }
  return this;
}

代码示例来源:origin: alibaba/Tangram-Android

/**
 * append new cards into adapter
 *
 * @param cards new cards will be added to the end
 */
public void appendGroup(@Nullable List<L> cards) {
  if (cards == null || cards.size() == 0)
    return;
  createSnapshot();
  final List<LayoutHelper> helpers = new LinkedList<>(getLayoutHelpers());
  mCards.ensureCapacity(mCards.size() + cards.size());
  helpers.addAll(transformCards(cards, mData, mCards));
  setLayoutHelpers(helpers);
  diffWithSnapshot();
  // not use {@link notifyItemRangeChanged} because not sure whether animations is required
  // notifyItemRangeChanged(oldItem, newitems);
  notifyDataSetChanged();
}

代码示例来源:origin: airbnb/epoxy

void addPayload(@Nullable EpoxyModel<?> payload) {
 if (payload == null) {
  return;
 }
 if (payloads == null) {
  // In most cases this won't be a batch update so we can expect just one payload
  payloads = new ArrayList<>(1);
 } else if (payloads.size() == 1) {
  // There are multiple payloads, but we don't know how big the batch will end up being.
  // To prevent resizing the list many times we bump it to a medium size
  payloads.ensureCapacity(10);
 }
 payloads.add(payload);
}

代码示例来源:origin: aa112901/remusic

public synchronized ArrayList<DownloadDBEntity> getDownLoadedListAll() {
  ArrayList<DownloadDBEntity> results = new ArrayList<>();
  Cursor cursor = null;
  try {
    cursor = mMusicDatabase.getReadableDatabase().query(DownFileStoreColumns.NAME, null,
        null, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
      results.ensureCapacity(cursor.getCount());
      do {
        results.add(new DownloadDBEntity(cursor.getString(0), cursor.getLong(1), cursor.getLong(2),
            cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getInt(7)));
      } while (cursor.moveToNext());
    }
    return results;
  } finally {
    if (cursor != null) {
      cursor.close();
      cursor = null;
    }
  }
}

代码示例来源:origin: AppliedEnergistics/Applied-Energistics-2

this.lines.ensureCapacity( this.getMaxRows() );
  this.lines.add( n );
this.getScrollBar().setRange( 0, this.lines.size() - LINES_ON_PAGE, 2 );

代码示例来源:origin: wildfly/wildfly

public SimpleName addAll(final Name suffix) {
  if (suffix instanceof SimpleName) {
    segments.addAll(((SimpleName) suffix).segments);
    return this;
  }
  final ArrayList<String> segments = this.segments;
  final int size = suffix.size();
  final int ourSize = segments.size();
  segments.ensureCapacity(ourSize + size);
  for (int i = 0; i < size; i ++) {
    add(suffix.get(i));
  }
  return this;
}

代码示例来源:origin: klout/brickhouse

private void addVector(Object listObj, VectorArrayAggBuffer myagg, ListObjectInspector inputOI) {
  int listLen = inputOI.getListLength(listObj);
  if (listLen > myagg.sumArray.size())
    myagg.sumArray.ensureCapacity(listLen);
  for (int i = 0; i < listLen; ++i) {
    Object listElem = inputOI.getListElement(listObj, i);
    double listElemDbl = NumericUtil.getNumericValue(
        (PrimitiveObjectInspector) inputOI.getListElementObjectInspector(), listElem);
    Double oldVal = myagg.sumArray.get(i);
    if (oldVal != null) {
      myagg.sumArray.set(i, oldVal + listElemDbl);
    } else {
      myagg.sumArray.set(i, listElemDbl);
    }
  }
}

相关文章