It's not a secret anymore!*
The moose likes Beginning Java and the fly likes Convert Integer List to int array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Convert Integer List to int array" Watch "Convert Integer List to int array" New topic
Author

Convert Integer List to int array

Padmini Mysore
Greenhorn

Joined: Jul 19, 2007
Posts: 3
Hi All,
is there a way, to convert a List of Integers to Array of ints (not integer). Something like List<Integer> to int []?
Please let me know.

Thanks
Christophe Verré
Sheriff

Joined: Nov 24, 2005
Posts: 14687
    
  16

is there a way

Yes.

Did you think at least how you could do it ? You know the size of the list, and you know what's in it.


[My Blog]
All roads lead to JavaRanch
Padmini Mysore
Greenhorn

Joined: Jul 19, 2007
Posts: 3
Sorry, I had to be more clear. What I meant was, using the toArray() method of util.ArrayList, is there a way to convert List<Integer> to int[]?
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3827

toArray() method returns an array of type Object (API). You cannot convert from Object[] to int[].
[ September 23, 2008: Message edited by: Vijitha Kumara ]

SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
Padmini Mysore
Greenhorn

Joined: Jul 19, 2007
Posts: 3
Thanks a lot for your messages.

So then the only way to do this, is to loop through the list and then add the elements to int array. Please let me know if there is a better way of doing this.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14116
    
  16

Looping through the list and copying the elements into an array is the only way to do this.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Paul Beckett
Ranch Hand

Joined: Jun 14, 2008
Posts: 96
if you want to change a List<Integer> into an Integer[] you can try the Collection.toArray(T[]) method. This uses generics so is java1.5 and later only.
Have a look at the javadoc for this to understand its behaviour - it may not be exactly what you need (ie it does not give you an array of primitives).

However, I'd probably approach this problem by creating the array manually and then iterating over the list to populate the array.
Tom Silverman
Greenhorn

Joined: Nov 01, 2008
Posts: 24
IMHO there is no generic way to do this. Java treats an array of primitives or objects as an Array so autoboxing will not help here. I think there is just no avoiding doing it manually.



Tom Silverman: SCJP5, SCJD6, SCWCD5, SCBCD5, IBM-142, ScrumMaster
Gamini Sirisena
Ranch Hand

Joined: Aug 05, 2008
Posts: 347
If you are simply trying to reduce the verbosity of your code then Integer[] can be coded the same way as int [] for accessing elements. This is Autoboxing and Autounboxing working for individual elements of the array. But as explained above if the Type int[] is being expected no luck. Check the code below.

ramya narayanan
Ranch Hand

Joined: Oct 06, 2008
Posts: 338
Dear all,
I'm seriously following this thread.
Any replies on this thread?
Regards.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19684
    
  20

Well Tom has given a good example on how to convert a List<Integer> into an int[] (although the form of the loop could be changed). What more would you like to see?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Tanto Oei
Greenhorn

Joined: Mar 24, 2009
Posts: 1
http://commons.apache.org/lang/api/org/apache/commons/lang/ArrayUtils.html#toPrimitive(java.lang.Integer[])
Mike Jensen
Greenhorn

Joined: Aug 27, 2009
Posts: 3
I like to use the IntArrayList from Colt. (http://acs.lbl.gov/~hoschek/colt/)

Colt provides a set of Open Source Libraries for High Performance Scientific and Technical Computing in Java.

Api for the IntArrayList is: http://acs.lbl.gov/~hoschek/colt/api/cern/colt/list/IntArrayList.html

The IntArrayList can grow dynamically, you use it like this:

IntArrayList intList = new IntArrayList();

for (int i = 0; i < 10; i++)
{
intList.add(i);
}

intList.trimToSize();
int[] myIntArray = intList.elements();

The Colt library has more goodies than this, check it out

Harshana Dias
Ranch Hand

Joined: Jun 11, 2007
Posts: 327
Paul Beckett wrote:if you want to change a List<Integer> into an Integer[] you can try the Collection.toArray(T[]) method. This uses generics so is java1.5 and later only.
Have a look at the javadoc for this to understand its behaviour - it may not be exactly what you need (ie it does not give you an array of primitives).

However, I'd probably approach this problem by creating the array manually and then iterating over the list to populate the array.


Just try following and still showing Type mismatch: cannot convert from Object[] to String[]. Shouldn't generics recognized and make a String array..



Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Good question. So I looked in the API documentation for List and found out that the return type for List<E>'s method toArray() is Object[]. Not E[]. So the answer to your question is "No".

Which of course has nothing to do with what Paul Beckett said there because he was talking about a different method, namely the toArray(T[]) method.
steve claflin
Ranch Hand

Joined: Dec 04, 2008
Posts: 54
At first glance, I thought thaty Harshana's code would work with an explicit typecast. But, even though you can successfully turn an Object[] into a String[], it must have been originally created as a String[]. Merely containing only strings is not enough.



The commented out cast of objStrs1 fails because the original type of the array as an object is Object[], while the type instantiated for objStrs2 was String[]. You can see from the class name printouts that the class of objStrs2 is known to be String[], thus allowing the typecast to succeed.
Vinay M Raju
Greenhorn

Joined: Aug 01, 2011
Posts: 3
Very useful information. Thansk a lot !!
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Harshana Dias wrote:
Just try following and still showing Type mismatch: cannot convert from Object[] to String[]. Shouldn't generics recognized and make a String array..





Use:



You can also use



but this is less efficient, because it will throw away the String[0] (unless ls is empty) and create another String[] of the correct size.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
How can a zero-length array not be empty? The memory use will be minimal.
Bob Grossman
Ranch Hand

Joined: Dec 18, 2008
Posts: 69
Campbell Ritchie wrote:How can a zero-length array not be empty? The memory use will be minimal.


I said "unless ls [i.e., the list] is empty", not "unless it [i.e., the zero-length string array] is empty".

The memory use will be minimal, but it is more effort to make something and throw it away than not make it and not have to throw it away. Reduce, reuse, recycle!
spar mc
Greenhorn

Joined: Jul 30, 2012
Posts: 5
Good information.
Most of the time iterating is the only option. toArray() method is indeed useless in 90% situations.
Rishi Shah
Ranch Hand

Joined: Sep 05, 2012
Posts: 43

Another question is why you even want obtain a primitive int array from your List.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

Mike Jensen wrote:I like to use the IntArrayList from Colt. (http://acs.lbl.gov/~hoschek/colt/)

Colt provides a set of Open Source Libraries for High Performance Scientific and Technical Computing in Java.

Api for the IntArrayList is: http://acs.lbl.gov/~hoschek/colt/api/cern/colt/list/IntArrayList.html

The Colt library has more goodies than this, check it out


Thanks Mike, this seems to be a good source. Now can you also tell me how do i integrate such open source libraries in NetBeans IDE?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38508
    
  23
Are you sure he is still around after 3¾ years?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Convert Integer List to int array