Article View: aus.computers.java
Article #631Re: working with arraylist
From: Paul Murray
Date: Wed, 21 Jun 2000 00:00
Date: Wed, 21 Jun 2000 00:00
68 lines
3211 bytes
3211 bytes
"James Moey" <james@igelaus.com.au> wrote in message
news:394EBA81.64C3C46A@igelaus.com.au...
> someguy wrote:
>
> > greetings
> >
> > I have created an arraylist, which i have used to store objects of a
purpose
> > desinged class that holds a string and integer, and the toString returns
> > both in a string "ie return(myString +myInteger);". I would like to
know
> > how to access the methods of my class from the arraylist.
> >
> > ie myArrayList.get(index).someMethodFromMyClass
> >
> > Interstingly enough, i can create new objects and add them to my list.
I
> > can get the values of these objects, but i cant invoke methods built
into
> > them to modify their values.
> >
> > Please if you know how i can access these method i would appreciate it.
>
> I think you need to "cast" the object back to the form you need before you
can
> call any of it's method.
Yup.
((MyClass)myArrayList.get(index)).someMethodFromMyClass().
Of course, if you have things in the arraylist that are not MyClass items,
you will get a ClassCastException.
You can bulletproof it with
Object o = myArrayList.get(index);
if(o instanceof MyClass)
((MyClass)o)..someMethodFromMyClass();
else
System.err.println("Hey! Some tool stuck a " +
o.getClass().getName() + " in my list!");
If you want to do several things to the objects in the arraylist, assign
them to a temp variable og the approprate type to save all the casting:
MyClass pair = (MyClass) myArrayList.get(index);
Finally, if what you are doing is associating integer values with names,
perhaps you should use a Map. The usual Map implementation to use is a
Hashtable.
Since you can't store int values in a map, you wrap them in an Integer
object.
Map m = new Hashtable();
m.put("ValueA", new Integer(3));
m.put("ValueB", new Integer(5967));
Map provides all the functions you need for listing keys, getting values
and changing them. It's also much more efficient at finding values by their
keys than iterating through an ArrayList.
system.out.println("Value A is " + m.get("ValueA"));
--------------------------------------------------------------
http://www.users.bigpond.com/pmurray
ICQ: 26066755
Technical Details
Message-ID:
<RkL35.5036$c5.10659@newsfeeds.bigpond.com>Path:
arc1-ca.pugleaf.net!unknown!not-for-mailReferences:
<958984148.685705@brs.ihug.com.au> <394EBA81.64C3C46A@igelaus.com.au>Show Full Headers
Complete Article Headers
X-Google-Language: ENGLISH,ASCII-7-bit X-Google-Thread: fdb77,3ab74ead81e4ec2b X-Google-Attributes: gidfdb77,public X-Google-Thread: 113ae9,3ab74ead81e4ec2b X-Google-Attributes: gid113ae9,public X-Google-Thread: 10d6db,3ab74ead81e4ec2b X-Google-Attributes: gid10d6db,public From: "Paul Murray" <pmurray@nospam.bigpond.com> Subject: Re: working with arraylist Date: 2000/06/21 Message-ID: <RkL35.5036$c5.10659@newsfeeds.bigpond.com> X-Deja-AN: 636815814 References: <958984148.685705@brs.ihug.com.au> <394EBA81.64C3C46A@igelaus.com.au> X-Priority: 3 X-MimeOLE: Produced By Microsoft MimeOLE V5.00.2919.6600 X-Trace: newsfeeds.bigpond.com 961511857 139.134.123.86 (Wed, 21 Jun 2000 00:37:37 EST) Organization: Telstra BigPond Internet Services (http://www.bigpond.com) X-MSMail-Priority: Normal NNTP-Posting-Date: Wed, 21 Jun 2000 00:37:37 EST Newsgroups: aus.computers.java,comp.lang.java.advocacy,comp.lang.java.help