4

以下のコードは、Google io オープン ソースから取得したものです。

com.google.android.apps.iosched.util.Lists.java http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/util/Lists .java

public static <E> ArrayList<E> newArrayList(E... elements) {
    int capacity = (elements.length * 110) / 100 + 5;
    ArrayList<E> list = new ArrayList<E>(capacity);
    Collections.addAll(list, elements);
    return list;
}

com.google.android.apps.iosched.util.Sets.java http://code.google.com/p/iosched/source/browse/android/src/com/google/android/apps/iosched/util/Sets .java

public static <E> HashSet<E> newHashSet(E... elements) {
    int capacity = elements.length * 4 / 3 + 1;
    HashSet<E> set = new HashSet<E>(capacity);
    Collections.addAll(set, elements);
    return set;
}

容量変数とはどういう意味ですか? 前もって感謝します!

4

2 に答える 2

2

Those collections internally use a fixed array to save the data. "capacity" is the initial number of elements the array can accommodate. When you add more elements than current capacity the internal array must be extended. This is a time consuming operation and the initial capacity tries to help in case you know how many elements will be added.

于 2012-03-23T19:35:41.183 に答える
1

これは ArrayList クラスの一部です。事前に容量を設定すると、大きなリストがいっぱいになるにつれてサイズを段階的に増やす必要がなくなり、代わりに必要なスペースが一度に割り当てられます。

http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

于 2012-03-23T19:33:23.727 に答える