toArray() method returns an array of type Object (API). You cannot convert from Object[] to int[]. [ September 23, 2008: Message edited by: Vijitha Kumara ]
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.
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
posted
1
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. <pre> /** * Converts an array of Integer objects to an array of integer primitives * * @param integerList the integer list * * @return an array of integer primitives */ static int[] toIntArray(List<Integer> integerList) { int[] intArray = new int[integerList.size()]; for (int i = 0; i < integerList.size(); i++) { intArray[i] = integerList.get(i); } return intArray; } </pre>
Tom Silverman: SCJP5, SCJD6, SCWCD5, SCBCD5, IBM-142, ScrumMaster
Gamini Sirisena
Ranch Hand
Joined: Aug 05, 2008
Posts: 347
posted
0
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
posted
0
Dear all, I'm seriously following this thread. Any replies on this thread? Regards.
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?
The Colt library has more goodies than this, check it out
Harshana Dias
Ranch Hand
Joined: Jun 11, 2007
Posts: 325
posted
0
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..
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: 53
posted
0
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
posted
0
Very useful information. Thansk a lot !!
Bob Grossman
Ranch Hand
Joined: Dec 18, 2008
Posts: 59
posted
0
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: 32716
4
posted
0
How can a zero-length array not be empty? The memory use will be minimal.
Bob Grossman
Ranch Hand
Joined: Dec 18, 2008
Posts: 59
posted
0
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
posted
0
Good information.
Most of the time iterating is the only option. toArray() method is indeed useless in 90% situations.