• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Convert Integer List to int array

 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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[]?
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Looping through the list and copying the elements into an array is the only way to do this.
 
Ranch Hand
Posts: 96
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 24
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.


 
Ranch Hand
Posts: 378
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Ranch Hand
Posts: 338
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Dear all,
I'm seriously following this thread.
Any replies on this thread?
Regards.
 
Sheriff
Posts: 22783
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://commons.apache.org/lang/api/org/apache/commons/lang/ArrayUtils.html#toPrimitive(java.lang.Integer[])
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 352
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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..



 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Very useful information. Thansk a lot !!
 
Ranch Hand
Posts: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 83
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Greenhorn
Posts: 5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Good information.
Most of the time iterating is the only option. toArray() method is indeed useless in 90% situations.
 
Ranch Hand
Posts: 43
Mac Ruby Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Another question is why you even want obtain a primitive int array from your List.
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Are you sure he is still around after 3¾ years?
 
reply
    Bookmark Topic Watch Topic
  • New Topic