Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Convert Integer List to int array

 
Padmini Mysore
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 14691
16
Eclipse IDE Ubuntu VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Padmini Mysore
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3913
9
Chrome Fedora Hibernate
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
toArray() method returns an array of type Object (API). You cannot convert from Object[] to int[].
[ September 23, 2008: Message edited by: Vijitha Kumara ]
 
Padmini Mysore
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15284
39
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looping through the list and copying the elements into an array is the only way to do this.
 
Paul Beckett
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.


 
Gamini Sirisena
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 338
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dear all,
I'm seriously following this thread.
Any replies on this thread?
Regards.
 
Rob Spoor
Sheriff
Pie
Posts: 20529
54
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
Mike Jensen
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 333
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21107
32
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 54
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very useful information. Thansk a lot !!
 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48961
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How can a zero-length array not be empty? The memory use will be minimal.
 
Bob Grossman
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Good information.
Most of the time iterating is the only option. toArray() method is indeed useless in 90% situations.
 
Rishi Shah
Ranch Hand
Posts: 43
Java Mac Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question is why you even want obtain a primitive int array from your List.
 
Ashish Dutt
Ranch Hand
Posts: 172
Java MySQL Database Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 48961
60
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure he is still around after 3¾ years?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic