How to set the size of TXXXXArrayList object from Trove? For example, is it possible to set the size of TIntArrayList
?
The only way I found is to add elements (zeros). Even bulk methods, which allowing initialize or add primitive arrays, do elementwise copym i.e. doubling the work.
While it is possible to set the size of an array with plain java:
int[] array = new int[1000000];
it will be created and filled with zeros. Trove equivalent does twice:
TIntArrayList array = new TIntArrayList(new int[1000000]);
First it allocates primitive array, then copies it inside class.
How to do in one step?
UPDATE
Wrap method allows speed up, but prohibits growing as payoff:
TIntArrayList array = TIntArrayList.wrap(new int[1000000]);
UPDATE2
Capacity is not the size. The program below prints zero and throws ArrayIndexOutOfBoundsException
:
package tests.gnu.trove;
import gnu.trove.list.array.TIntArrayList;
public class Try_Capacity {
public static void main(String[] args) {
TIntArrayList array = new TIntArrayList(10);
array.ensureCapacity(10);
System.out.println(array.size());
array.set(5, 10);
}
}
You should either use the constructor that takes an int capacity:
or use the "ensureCapacity" method:
So, for your example just do:
new TIntArrayList( 1000000 )
UPDATE
To "set the size" (better way to say that would be to "fill" the collection), I would then use the "fill" method.
UPDATE 2
If you already have an array with the values you want, instead of a "fill", use insert: