Obtaining an int element from an arraylist?

Discussion in 'Java' started by nooneinparticular314159@yahoo.com, Apr 17, 2005.

  1. Guest

    I am trying to recover an integer from an arraylist:

    ArrayList pathlist = new ArrayList();

    pathlist has previously been populated with integers:

    public void AddEdgeAndNodeToPath(int e, int n) {
    pathlist.add(e);
    pathlist.add(n);
    }

    Now I want to extract them. But when I try to do this:
    int counter = 0;
    int x;

    for(counter = 0; counter < (pathlist.size()); counter++) {
    x = (int) pathlist.get(counter);
    }

    it complains that I am trying to convert an Object to an int. Are my
    ints forever trapped inside the ArrayList as objects? If not, how can
    I get them out and turn them back into ints? The fate of the world may
    rest upon your reply!

    Thanks!
     
    , Apr 17, 2005
    #1
    1. Advertising

  2. In article <>,
    wrote:

    > I am trying to recover an integer from an arraylist:
    >
    > ArrayList pathlist = new ArrayList();
    >
    > pathlist has previously been populated with integers:
    >
    > public void AddEdgeAndNodeToPath(int e, int n) {
    > pathlist.add(e);
    > pathlist.add(n);
    > }
    >
    > Now I want to extract them. But when I try to do this:
    > int counter = 0;
    > int x;
    >
    > for(counter = 0; counter < (pathlist.size()); counter++) {
    > x = (int) pathlist.get(counter);
    > }
    >
    > it complains that I am trying to convert an Object to an int. Are my
    > ints forever trapped inside the ArrayList as objects? If not, how can
    > I get them out and turn them back into ints? The fate of the world may
    > rest upon your reply!
    >
    > Thanks!


    Java 1.5 wrapped your ints in Integer instances because the ArrayList
    only takes Objects. (I can't say I like a feature that makes
    inefficient coding easier and less obvious.)

    If you're working with a lot of insertions, you could get a significant
    performance boost by creating your own array class that stores primitive
    ints.
     
    Kevin McMurtrie, Apr 18, 2005
    #2
    1. Advertising

  3. Jon Nall Guest

    On 2005-04-17 17:54:02 -0500, said:

    > I am trying to recover an integer from an arraylist:
    >
    > ArrayList pathlist = new ArrayList();
    >
    > pathlist has previously been populated with integers:
    >
    > public void AddEdgeAndNodeToPath(int e, int n) {
    > pathlist.add(e);
    > pathlist.add(n);
    > }
    >
    > Now I want to extract them. But when I try to do this:
    > int counter = 0;
    > int x;
    >
    > for(counter = 0; counter < (pathlist.size()); counter++) {
    > x = (int) pathlist.get(counter);
    > }


    Definitely listen to what Kevin said about performance. You probably
    should read about autoboxing:
    http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html

    However, if you really want to use an ArrayList and use int<->Integer
    autoboxing, you should declare your ArrayList as:

    ArrayList<Integer> pathlist = new ArrayList<Integer>();

    With this code you'll get a compile error if you try and add anything
    besides an Integer to that list. Further, you need no explicit cast
    when accessing an element in the list.

    As a final note, I believe if you change the cast in the code you
    posted to an Integer, it should work. (that is, "x = (Integer)
    pathlist.get(counter);").

    hope this helps,
    nall.
     
    Jon Nall, Apr 18, 2005
    #3
  4. James Korman Guest

    On Mon, 18 Apr 2005 01:04:44 GMT, Jon Nall <> wrote:

    >On 2005-04-17 17:54:02 -0500, said:
    >
    >> I am trying to recover an integer from an arraylist:
    >>
    >> ArrayList pathlist = new ArrayList();
    >>
    >> pathlist has previously been populated with integers:
    >>
    >> public void AddEdgeAndNodeToPath(int e, int n) {
    >> pathlist.add(e);
    >> pathlist.add(n);
    >> }
    >>
    >> Now I want to extract them. But when I try to do this:
    >> int counter = 0;
    >> int x;
    >>
    >> for(counter = 0; counter < (pathlist.size()); counter++) {
    >> x = (int) pathlist.get(counter);
    >> }

    >
    >Definitely listen to what Kevin said about performance. You probably
    >should read about autoboxing:
    >http://java.sun.com/j2se/1.5.0/docs/guide/language/autoboxing.html
    >
    >However, if you really want to use an ArrayList and use int<->Integer
    >autoboxing, you should declare your ArrayList as:
    >
    >ArrayList<Integer> pathlist = new ArrayList<Integer>();
    >
    >With this code you'll get a compile error if you try and add anything
    >besides an Integer to that list. Further, you need no explicit cast
    >when accessing an element in the list.
    >
    >As a final note, I believe if you change the cast in the code you
    >posted to an Integer, it should work. (that is, "x = (Integer)
    >pathlist.get(counter);").
    >
    >hope this helps,
    >nall.
    >


    ArrayList<Integer> pathlist = new ArrayList<Integer>();

    public static void AddEdgeAndNodeToPath(int e, int n)
    {
    pathlist.add(e);
    pathlist.add(n);
    }

    int counter = 0;
    int x;

    for(counter = 0; counter < (pathlist.size()); counter++) {
    // No cast to Integer required.
    x = pathlist.get(counter);
    System.out.println(x);
    }

    Jim
     
    James Korman, Apr 18, 2005
    #4
  5. Tony Morris Guest

    <> wrote in message
    news:...
    > I am trying to recover an integer from an arraylist:
    >
    > ArrayList pathlist = new ArrayList();
    >
    > pathlist has previously been populated with integers:
    >
    > public void AddEdgeAndNodeToPath(int e, int n) {
    > pathlist.add(e);
    > pathlist.add(n);
    > }
    >
    > Now I want to extract them. But when I try to do this:
    > int counter = 0;
    > int x;
    >
    > for(counter = 0; counter < (pathlist.size()); counter++) {
    > x = (int) pathlist.get(counter);
    > }
    >
    > it complains that I am trying to convert an Object to an int. Are my
    > ints forever trapped inside the ArrayList as objects? If not, how can
    > I get them out and turn them back into ints? The fate of the world may
    > rest upon your reply!
    >
    > Thanks!
    >


    Collection<Integer> pathlist = new ArrayList<Integer>();
    pathlist.add(7);
    int seven = pathlist.get(0);

    The warning that your compiler is giving you has a meaning.

    --
    Tony Morris

    JTiger Unit Test Framework for J2SE 1.5
    http://www.jtiger.org/
    Java Q&A (FAQ, Trivia)
    http://qa.jtiger.org/
    http://xdweb.net/~dibblego/
     
    Tony Morris, Apr 18, 2005
    #5
  6. paul Guest

    Hi all,

    ArrayList<Integer> pathlist = new ArrayList<Integer>();
    the statement above can only compiled by jdk5

    but what if i want to compile in jdk 1.4?

    i mean i still can do the int operation in the arraylist.

    pls help

    paul
    Tony Morris 寫é“:
    > <> wrote in message
    > news:...
    > > I am trying to recover an integer from an arraylist:
    > >
    > > ArrayList pathlist = new ArrayList();
    > >
    > > pathlist has previously been populated with integers:
    > >
    > > public void AddEdgeAndNodeToPath(int e, int n) {
    > > pathlist.add(e);
    > > pathlist.add(n);
    > > }
    > >
    > > Now I want to extract them. But when I try to do this:
    > > int counter = 0;
    > > int x;
    > >
    > > for(counter = 0; counter < (pathlist.size()); counter++) {
    > > x = (int) pathlist.get(counter);
    > > }
    > >
    > > it complains that I am trying to convert an Object to an int. Are

    my
    > > ints forever trapped inside the ArrayList as objects? If not, how

    can
    > > I get them out and turn them back into ints? The fate of the world

    may
    > > rest upon your reply!
    > >
    > > Thanks!
    > >

    >
    > Collection<Integer> pathlist = new ArrayList<Integer>();
    > pathlist.add(7);
    > int seven = pathlist.get(0);
    >
    > The warning that your compiler is giving you has a meaning.
    >
    > --
    > Tony Morris
    >
    > JTiger Unit Test Framework for J2SE 1.5
    > http://www.jtiger.org/
    > Java Q&A (FAQ, Trivia)
    > http://qa.jtiger.org/
    > http://xdweb.net/~dibblego/
     
    paul, May 14, 2005
    #6
  7. anzime Guest

    x = ((Integer)pathlist.get(counter)).intValue();
    ---anzime
     
    anzime, May 14, 2005
    #7
  8. Tony Morris Guest

    "paul" <> wrote in message
    news:...
    Hi all,

    ArrayList<Integer> pathlist = new ArrayList<Integer>();
    the statement above can only compiled by jdk5

    but what if i want to compile in jdk 1.4?

    i mean i still can do the int operation in the arraylist.

    pls help

    paul
    Tony Morris ??:
    > <> wrote in message
    > news:...
    > > I am trying to recover an integer from an arraylist:
    > >
    > > ArrayList pathlist = new ArrayList();
    > >
    > > pathlist has previously been populated with integers:
    > >
    > > public void AddEdgeAndNodeToPath(int e, int n) {
    > > pathlist.add(e);
    > > pathlist.add(n);
    > > }
    > >
    > > Now I want to extract them. But when I try to do this:
    > > int counter = 0;
    > > int x;
    > >
    > > for(counter = 0; counter < (pathlist.size()); counter++) {
    > > x = (int) pathlist.get(counter);
    > > }
    > >
    > > it complains that I am trying to convert an Object to an int. Are

    my
    > > ints forever trapped inside the ArrayList as objects? If not, how

    can
    > > I get them out and turn them back into ints? The fate of the world

    may
    > > rest upon your reply!
    > >
    > > Thanks!
    > >

    >
    > Collection<Integer> pathlist = new ArrayList<Integer>();
    > pathlist.add(7);
    > int seven = pathlist.get(0);
    >
    > The warning that your compiler is giving you has a meaning.
    >
    > --
    > Tony Morris
    >
    > JTiger Unit Test Framework for J2SE 1.5
    > http://www.jtiger.org/
    > Java Q&A (FAQ, Trivia)
    > http://qa.jtiger.org/
    > http://xdweb.net/~dibblego/


    I didn't say ArrayList<Integer> pathlist = new ArrayList<Integer>();
    I said Collection<Integer> pathlist = new ArrayList<Integer>();

    You can compile it with 1.4 by using:
    Collection pathlist = new ArrayList();
    pathlist.add(new Integer(7));
    int seven = ((Integer)pathlist.get(0)).intValue();

    --
    Tony Morris
    Software Engineer, IBM Australia.
    BInfTech, SCJP 1.4, SCJP 5.0, SCJD

    http://www.jtiger.org/ JTiger Unit Test Framework for Java
    http://qa.jtiger.org/ Java Q&A (FAQ, Trivia)
    http://xdweb.net/~dibblego/
     
    Tony Morris, May 15, 2005
    #8
  9. Tony Morris Guest

    "Tony Morris" <> wrote in message
    news:...
    >
    > "paul" <> wrote in message
    > news:...
    > Hi all,
    >
    > ArrayList<Integer> pathlist = new ArrayList<Integer>();
    > the statement above can only compiled by jdk5
    >
    > but what if i want to compile in jdk 1.4?
    >
    > i mean i still can do the int operation in the arraylist.
    >
    > pls help
    >
    > paul
    > Tony Morris ??:
    > > <> wrote in message
    > > news:...
    > > > I am trying to recover an integer from an arraylist:
    > > >
    > > > ArrayList pathlist = new ArrayList();
    > > >
    > > > pathlist has previously been populated with integers:
    > > >
    > > > public void AddEdgeAndNodeToPath(int e, int n) {
    > > > pathlist.add(e);
    > > > pathlist.add(n);
    > > > }
    > > >
    > > > Now I want to extract them. But when I try to do this:
    > > > int counter = 0;
    > > > int x;
    > > >
    > > > for(counter = 0; counter < (pathlist.size()); counter++) {
    > > > x = (int) pathlist.get(counter);
    > > > }
    > > >
    > > > it complains that I am trying to convert an Object to an int. Are

    > my
    > > > ints forever trapped inside the ArrayList as objects? If not, how

    > can
    > > > I get them out and turn them back into ints? The fate of the world

    > may
    > > > rest upon your reply!
    > > >
    > > > Thanks!
    > > >

    > >
    > > Collection<Integer> pathlist = new ArrayList<Integer>();
    > > pathlist.add(7);
    > > int seven = pathlist.get(0);
    > >
    > > The warning that your compiler is giving you has a meaning.
    > >
    > > --
    > > Tony Morris
    > >
    > > JTiger Unit Test Framework for J2SE 1.5
    > > http://www.jtiger.org/
    > > Java Q&A (FAQ, Trivia)
    > > http://qa.jtiger.org/
    > > http://xdweb.net/~dibblego/

    >
    > I didn't say ArrayList<Integer> pathlist = new ArrayList<Integer>();
    > I said Collection<Integer> pathlist = new ArrayList<Integer>();
    >
    > You can compile it with 1.4 by using:
    > Collection pathlist = new ArrayList();
    > pathlist.add(new Integer(7));
    > int seven = ((Integer)pathlist.get(0)).intValue();
    >
    > --
    > Tony Morris
    > Software Engineer, IBM Australia.
    > BInfTech, SCJP 1.4, SCJP 5.0, SCJD
    >
    > http://www.jtiger.org/ JTiger Unit Test Framework for Java
    > http://qa.jtiger.org/ Java Q&A (FAQ, Trivia)
    > http://xdweb.net/~dibblego/
    >
    >
    >


    My bad.
    Change 'Collection' to 'List' (*not* ArrayList).

    --
    Tony Morris
    Software Engineer, IBM Australia.
    BInfTech, SCJP 1.4, SCJP 5.0, SCJD

    http://www.jtiger.org/ JTiger Unit Test Framework for Java
    http://qa.jtiger.org/ Java Q&A (FAQ, Trivia)
    http://xdweb.net/~dibblego/
     
    Tony Morris, May 15, 2005
    #9
  10. paul Guest

    thanks . and the problem solved.

    paul

    Tony Morris 寫é“:
    > "Tony Morris" <> wrote in message
    > news:...
    > >
    > > "paul" <> wrote in message
    > > news:...
    > > Hi all,
    > >
    > > ArrayList<Integer> pathlist = new ArrayList<Integer>();
    > > the statement above can only compiled by jdk5
    > >
    > > but what if i want to compile in jdk 1.4?
    > >
    > > i mean i still can do the int operation in the arraylist.
    > >
    > > pls help
    > >
    > > paul
    > > Tony Morris ??:
    > > > <> wrote in message
    > > > news:...
    > > > > I am trying to recover an integer from an arraylist:
    > > > >
    > > > > ArrayList pathlist = new ArrayList();
    > > > >
    > > > > pathlist has previously been populated with integers:
    > > > >
    > > > > public void AddEdgeAndNodeToPath(int e, int n) {
    > > > > pathlist.add(e);
    > > > > pathlist.add(n);
    > > > > }
    > > > >
    > > > > Now I want to extract them. But when I try to do this:
    > > > > int counter = 0;
    > > > > int x;
    > > > >
    > > > > for(counter = 0; counter < (pathlist.size()); counter++) {
    > > > > x = (int) pathlist.get(counter);
    > > > > }
    > > > >
    > > > > it complains that I am trying to convert an Object to an int.

    Are
    > > my
    > > > > ints forever trapped inside the ArrayList as objects? If not,

    how
    > > can
    > > > > I get them out and turn them back into ints? The fate of the

    world
    > > may
    > > > > rest upon your reply!
    > > > >
    > > > > Thanks!
    > > > >
    > > >
    > > > Collection<Integer> pathlist = new ArrayList<Integer>();
    > > > pathlist.add(7);
    > > > int seven = pathlist.get(0);
    > > >
    > > > The warning that your compiler is giving you has a meaning.
    > > >
    > > > --
    > > > Tony Morris
    > > >
    > > > JTiger Unit Test Framework for J2SE 1.5
    > > > http://www.jtiger.org/
    > > > Java Q&A (FAQ, Trivia)
    > > > http://qa.jtiger.org/
    > > > http://xdweb.net/~dibblego/

    > >
    > > I didn't say ArrayList<Integer> pathlist = new

    ArrayList<Integer>();
    > > I said Collection<Integer> pathlist = new ArrayList<Integer>();
    > >
    > > You can compile it with 1.4 by using:
    > > Collection pathlist = new ArrayList();
    > > pathlist.add(new Integer(7));
    > > int seven = ((Integer)pathlist.get(0)).intValue();
    > >
    > > --
    > > Tony Morris
    > > Software Engineer, IBM Australia.
    > > BInfTech, SCJP 1.4, SCJP 5.0, SCJD
    > >
    > > http://www.jtiger.org/ JTiger Unit Test Framework for Java
    > > http://qa.jtiger.org/ Java Q&A (FAQ, Trivia)
    > > http://xdweb.net/~dibblego/
    > >
    > >
    > >

    >
    > My bad.
    > Change 'Collection' to 'List' (*not* ArrayList).
    >
    > --
    > Tony Morris
    > Software Engineer, IBM Australia.
    > BInfTech, SCJP 1.4, SCJP 5.0, SCJD
    >
    > http://www.jtiger.org/ JTiger Unit Test Framework for Java
    > http://qa.jtiger.org/ Java Q&A (FAQ, Trivia)
    > http://xdweb.net/~dibblego/
     
    paul, May 15, 2005
    #10
  11. > From:
    > I am trying to recover an integer from an arraylist:


    And an arraylist is declared to have Object not Integer as its type of
    element, right? The same thing happens with an Enumeration. What I
    would do is a two-step process. First downcast the Object to be an
    Integer (compiler assumes it's unsafe so includes type-checking in
    compiled code, which at runtime throws exception if it's not valid
    cast), then in the Integer class there's a method for converting to a
    primitive-type int.

    Somebody else posted what looks like the right code, but didn't explain
    what was really being done conceptually, so for benefit of beginners I
    thought I'd explain the solution in English.
     
    Robert Maas, see http://tinyurl.com/uh3t, May 15, 2005
    #11
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Homa
    Replies:
    4
    Views:
    9,918
  2. Schnoffos
    Replies:
    2
    Views:
    1,225
    Martien Verbruggen
    Jun 27, 2003
  3. Hal Styli
    Replies:
    14
    Views:
    1,650
    Old Wolf
    Jan 20, 2004
  4. arun
    Replies:
    8
    Views:
    460
    Dave Thompson
    Jul 31, 2006
  5. aling
    Replies:
    8
    Views:
    959
    Jim Langston
    Oct 20, 2005
Loading...

Share This Page