Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / First Aid / December 2005

Tip: Looking for answers? Try searching our database.

Creating Integer object from int

Thread view: 
Mike - 09 Dec 2005 21:21 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?

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.



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.