Obtaining an int element from an arraylist?

  • Thread starter nooneinparticular314159
  • Start date
N

nooneinparticular314159

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!
 
K

Kevin McMurtrie

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.
 
J

Jon Nall

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.
 
J

James Korman

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
 
T

Tony Morris

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/
 
P

paul

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 寫é“:
 
T

Tony Morris

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 ??:
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/
 
T

Tony Morris

Tony Morris said:
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 ??:

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/
 
R

Robert Maas, see http://tinyurl.com/uh3t

From: (e-mail address removed)
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top