Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Obtaining an int element from an arraylist?

Reply
Thread Tools

Obtaining an int element from an arraylist?

 
 
nooneinparticular314159@yahoo.com
Guest
Posts: n/a
 
      04-17-2005
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!

 
Reply With Quote
 
 
 
 
Kevin McMurtrie
Guest
Posts: n/a
 
      04-17-2005
In article <(E-Mail Removed). com>,
http://www.velocityreviews.com/forums/(E-Mail Removed) 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.
 
Reply With Quote
 
 
 
 
Jon Nall
Guest
Posts: n/a
 
      04-18-2005
On 2005-04-17 17:54:02 -0500, (E-Mail Removed) 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/...utoboxing.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.


 
Reply With Quote
 
James Korman
Guest
Posts: n/a
 
      04-18-2005
On Mon, 18 Apr 2005 01:04:44 GMT, Jon Nall <(E-Mail Removed)> wrote:

>On 2005-04-17 17:54:02 -0500, (E-Mail Removed) 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/...utoboxing.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
 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      04-18-2005

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ups.com...
> 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/


 
Reply With Quote
 
paul
Guest
Posts: n/a
 
      05-14-2005
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 寫道:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > 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/


 
Reply With Quote
 
anzime
Guest
Posts: n/a
 
      05-14-2005
x = ((Integer)pathlist.get(counter)).intValue();
---anzime

 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-14-2005

"paul" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) oups.com...
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 ??:
> <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) ups.com...
> > 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/



 
Reply With Quote
 
Tony Morris
Guest
Posts: n/a
 
      05-14-2005

"Tony Morris" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
>
> "paul" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed) oups.com...
> 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 ??:
> > <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) ups.com...
> > > 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/


 
Reply With Quote
 
paul
Guest
Posts: n/a
 
      05-15-2005

thanks . and the problem solved.

paul

Tony Morris 寫道:
> "Tony Morris" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> >
> > "paul" <(E-Mail Removed)> wrote in message
> > news:(E-Mail Removed) oups.com...
> > 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 ??:
> > > <(E-Mail Removed)> wrote in message
> > > news:(E-Mail Removed) ups.com...
> > > > 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/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
why is int a[0] not allowed, but int* a = new int[0] is? haijin.biz@gmail.com C++ 9 04-17-2007 09:01 AM
Difference between int i, j; and int i; int j; arun C Programming 8 07-31-2006 05:11 AM
int a[10]; int* p=(int*)((&a)+1); But why p isn't equal to ((&a)+1)? aling C++ 8 10-20-2005 02:42 PM
int main(int argc, char *argv[] ) vs int main(int argc, char **argv ) Hal Styli C Programming 14 01-20-2004 10:00 PM
dirty stuff: f(int,int) cast to f(struct{int,int}) Schnoffos C Programming 2 06-27-2003 03:13 AM



Advertisments