For Programmers: Free Programming Magazines  


Home > Archive > Java Help > February 2007 > ArrayList.get help ?









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author ArrayList.get help ?
TonyB

2007-02-24, 7:07 pm

Hi,
Further to my earlier questions on ArrayLists, I have now created an
ArrayList containing a number of Tool objects, and now I'm trying to do
some tests on each list items.
So I find the size of the list, run a for loop, and for each iteration of
the loop try to load the object in that position in the list, and then do
some tests on that object.
by using something like
private ArrayList toolList ;
private Tool currentTool ;
.....
numberOfElements = toolList.size() ;
for(i=0; i<numberOfElements; i++){
currentTool = toolList.get(i) ;
currentTool.method()
.....
The compiler complains for the line containing toolList.get(i) "incompatible
types - found java.lang.Object but expected Tool"
Now the ArrayList.get(int i) returns a reference to the object at that
position, so I thought that reference could be used to directly refer to
that object.
I want currentTool to contain a copy of the Tool object in that position in
the list, so I can test that copy. Or even better I want to
directly run a method on the object referenced in that list position without
copying the object.
Can someone point in the right direction to get the syntax for this ?
Tony


Liz

2007-02-24, 7:07 pm

On Sat, 24 Feb 2007 11:23:35 GMT, TonyB wrote:
<><
> numberOfElements = toolList.size() ;
> for(i=0; i<numberOfElements; i++){
> currentTool = toolList.get(i) ;
> currentTool.method()
> .....
>The compiler complains for the line containing toolList.get(i) "incompatible
>types - found java.lang.Object but expected Tool"
>Now the ArrayList.get(int i) returns a reference to the object at that
>position, so I thought that reference could be used to directly refer to
>that object.
>I want currentTool to contain a copy of the Tool object in that position in
>the list, so I can test that copy. Or even better I want to
>directly run a method on the object referenced in that list position without
>copying the object.
>Can someone point in the right direction to get the syntax for this ?
>Tony
>


You need to investigate "generics" if you are using 1.5. If you are
using an earlier version of java, then you will need to "cast" the
object which comes back from your list into the type you expect.
currentTool = (Tool) toolList.get(i);

If using java 1.5 look at
http://java.sun.com/developer/techn.../J2SE/generics/

RedGrittyBrick

2007-02-24, 7:07 pm

TonyB wrote:
> Further to my earlier questions on ArrayLists, I have now created an
> ArrayList containing a number of Tool objects, and now I'm trying to do
> some tests on each list items.
> So I find the size of the list, run a for loop, and for each iteration of
> the loop try to load the object in that position in the list, and then do
> some tests on that object.
> by using something like
> private ArrayList toolList ;
> private Tool currentTool ;
> .....
> numberOfElements = toolList.size() ;
> for(i=0; i<numberOfElements; i++){
> currentTool = toolList.get(i) ;
> currentTool.method()
> .....
> The compiler complains for the line containing toolList.get(i) "incompatible
> types - found java.lang.Object but expected Tool"


private ArrayList<Tool> toolList ;
TonyB

2007-02-25, 4:12 am

Hi Liz and other poster,
Thanks for your help. That was really helpful.
Tony


Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com