This week's book giveaways are in the Jython/Python and Object-Oriented programming forums.
We're giving away four copies each of Machine Learning for Business: Using Amazon SageMaker and Jupyter and Object Design Style Guide and have the authors on-line!
See this thread and this one for details.
Win a copy of Machine Learning for Business: Using Amazon SageMaker and JupyterE this week in the Jython/Python forum
or Object Design Style Guide in the Object-Oriented programming forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
  • Knute Snortum
Sheriffs:
  • Liutauras Vilda
  • Tim Cooke
  • Junilu Lacar
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Joe Ess
  • salvin francis
  • fred rosenberger

Convert Integer List to int array

 
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
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • 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[]?
 
Bartender
Posts: 4105
72
Hibernate Fedora Chrome
  • 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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • 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.
 
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.
 
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.


 
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.

 
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.
 
Sheriff
Posts: 21842
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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?
 
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

 
Ranch Hand
Posts: 352
  • 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..



 
Marshal
Posts: 24961
61
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.
 
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.
 
Greenhorn
Posts: 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Very useful information. Thansk a lot !!
 
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.
 
Marshal
Posts: 67527
257
  • 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!
 
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.
 
Ranch Hand
Posts: 43
Mac Ruby Java
  • 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.
 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • 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
Marshal
Posts: 67527
257
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Are you sure he is still around after 3¾ years?
 
It was the best of times. It was the worst of times. It was a tiny ad.
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!