I'm experimenting with collections and got myself stuck. I want a list
(ArrayList) to contain numbers, but it seems an ArrayList cannot
contain primitive int types. It seems to work OK with Integer. But how
do I cast an array of int to an array of Integer?
I've tried
Integer[] numbers = { (Integer) 1, (Integer) 2}
and
List list = new ArrayList(Arrays.asList((Integer) numbers));
Here is the code. Note that it worked when I had the array as String
and operated on letters.
import java.util.*;
public class TestCollection {
public static void main(String args[]) {
int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
List list = new ArrayList(Arrays.asList((Integer) numbers));
Iterator it1 = list.iterator();
while(it1.hasNext())
{
Integer x = (Integer) it1.next();
System.out.print("==>"+x+"[");
Iterator it2 = list.iterator();
while(it2.hasNext())
{
Integer y = (Integer) it2.next();
if (!(x==y))
{
System.out.print(y);
}
else
{
System.out.print(" ");
}
}
System.out.println("]");
}
}
}
Thanks for any help.
Mike
Mike - 09 Dec 2005 21:29 GMT
I forgot to say that I've also tried this
int[] numbers = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
Integer[] ints = Integer.toInteger(numbers);
List list = new ArrayList(Arrays.asList(ints));
Mike
Oliver Wong - 09 Dec 2005 21:47 GMT
> I'm experimenting with collections and got myself stuck. I want a list
> (ArrayList) to contain numbers, but it seems an ArrayList cannot
[quoted text clipped - 4 lines]
>
> Integer[] numbers = { (Integer) 1, (Integer) 2}
The simplest way to get an Integer of a specific value is to use the
valueOf() factory method:
Integer[] numbers = { Integer.valueOf(1), Integer.valueOf(2)};
- Oliver
Thomas Hawtin - 10 Dec 2005 00:42 GMT
>>I've tried
>>
[quoted text clipped - 4 lines]
>
> Integer[] numbers = { Integer.valueOf(1), Integer.valueOf(2)};
Integer.valueOf was introduced in 1.5. If the 1.5 library is available
then so should autoboxing[1], so Mike's version should work. For 1.4 and
earlier, use new Integer(1).
Tom Hawtin
[1] If you compile with -source 1.4 -target 1.4 but leaving the
bootclasspath set to a latter Java library, you could end up with
Integer.valueOf and no autoboxing. The code would run in 1.4 up until
some point when it throw an AbstractMethodError (or something similar).
Which just goes to show that you should set -Xbootclasspath if you set
-target.

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/
Roedy Green - 09 Dec 2005 22:55 GMT
>I'm experimenting with collections and got myself stuck. I want a list
>(ArrayList) to contain numbers, but it seems an ArrayList cannot
>contain primitive int types. It seems to work OK with Integer. But how
>do I cast an array of int to an array of Integer?
you can't cast, you have to convert. You create a new array and
convert each element and copy it over.
See http://mindprod.com/applets/converter.html
for how to convert int <->Integer
If you work with individual elements, Java 1.5+ will automatically
interconvert int/integer for you with autoboxing.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.
Roedy Green - 09 Dec 2005 22:57 GMT
On Fri, 09 Dec 2005 22:55:55 GMT, Roedy Green
<my_email_is_posted_on_my_website@munged.invalid> wrote, quoted or
indirectly quoted someone who said :
> you can't cast, you have to convert. You create a new array and
>convert each element and copy it over.
>See http://mindprod.com/applets/converter.html
>for how to convert int <->Integer
To make that clearer, you can't cast entire arrays. You have to copy
over elements to a new array of the correct type in a loop.

Signature
Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.