1

I am using the following way to put int values from a ResultSet into an int array. This seems very innefficient but I can't figure out how to get an array of primitive int's where I don't know the size beforehand.

List<Integer> ints = ArrayList<Integer>();

while ( results.next ) ints.add( results.getInt( "id" );

int[] intsArray = new int[ ints.size() ];
for ( int i = 0; i < ints.length; i++ ) int[ i ] = ints.get( i ); //auto-boxes here

I need these to be a primitive array as that's what a method requires.

Don Rhummy
  • 24,730
  • 42
  • 175
  • 330
  • Hello, you can use [Apache Common Lang](http://commons.apache.org/proper/commons-lang/) to assign an array of int : `int[] intArray = ArrayUtils.toPrimitive(ints.toArray(new Integer[ints.size()]));` – Greg Jeanmart Jun 29 '16 at 20:18
  • @gjeanmart but aren't they doing the same thing internally? I'd be including an entire library (and loading new classes) just to do a simple few lines of code: https://github.com/apache/commons-lang/blob/master/src/main/java/org/apache/commons/lang3/ArrayUtils.java – Don Rhummy Jun 29 '16 at 20:22
  • @DonRhummy : You right the [code is quite similar](https://commons.apache.org/proper/commons-lang/apidocs/src-html/org/apache/commons/lang3/ArrayUtils.html). Otherwise if you are using Java-8, you can try the stream API like that `int[] intArray = ints.stream().mapToInt(i -> i).toArray();` – Greg Jeanmart Jun 29 '16 at 20:26

2 Answers2

2

If you don't know the number of values read from the result set you really can't avoid using a list.

But you can avoid using a List<Integer> if you have a list class which can store primitive int values like for example TIntList of the trove project.

In the end you should really measure the performance impact of using intermediate Integer objects before spending too much energy on that question.

wero
  • 32,544
  • 3
  • 59
  • 84
0

Convert ArrayList to array

You would have to guess an initial size and then resize the array as you fill its last element, repeating as needed as you go through your result set.

This behavior is exactly what ArrayList already gives you. But already debugged. ;-)

So just use ArrayList<Integer>. Convert to an array at the end.

You might think to use Collection::toArray. But that utility can only return an array of objects, not primitives.

See this Question, for several good ways to convert to array of primitives.

My choice would be calling on Google Guava. While adding Guava for this one purpose might be overkill, Guava is so useful so often that I add the library by default to most any new Java project.

List< Integer > list = ... ;
int[] values = Ints.toArray( list ) ;

If your deployment environment were extremely tight on memory, or if you had a very large number of elements, or if you were doing this operation so often as to impact performance, only then would I resort to filling-and-resizing an array int manually yourself.

Community
  • 1
  • 1
Basil Bourque
  • 303,325
  • 100
  • 852
  • 1,154
  • They basically do the same thing. It appears there's no way around creating an object list and looping. https://github.com/google/guava/blob/master/guava/src/com/google/common/primitives/Ints.java – Don Rhummy Jun 30 '16 at 13:30
  • @DonRhummy No, there is no magic, still looping to fill a new array. Indeed, the Guava code is even inefficient in that it creates an extra array, a temporary array of `Object`, rather than iterating over the Collection (not sure why, don't see the benefit). But this code is already written, already debugged. I trust code written by dedicated Googlers and public peers so much more than I trust my own lousy coding. – Basil Bourque Jun 30 '16 at 16:46