Thread View: aus.computers.java
5 messages
5 total messages
Started by someguy
Mon, 22 May 2000 00:00
working with arraylist
Author: someguy
Date: Mon, 22 May 2000 00:00
Date: Mon, 22 May 2000 00:00
14 lines
1504 bytes
1504 bytes
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.
Re: working with arraylist
Author: fungus
Date: Mon, 22 May 2000 00:00
Date: Mon, 22 May 2000 00:00
28 lines
1685 bytes
1685 bytes
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. MyClass m = (MyClass)myArrayList.get(index); m.someMethod(); -- <\___/> / O O \ \_____/ FTB.
Re: working with arraylist
Author: James Moey
Date: Tue, 20 Jun 2000 00:00
Date: Tue, 20 Jun 2000 00:00
23 lines
1545 bytes
1545 bytes
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. from James
Re: working with arraylist
Author: Brad Long
Date: Tue, 20 Jun 2000 00:00
Date: Tue, 20 Jun 2000 00:00
36 lines
1848 bytes
1848 bytes
MyClass obj = new MyClass(); ArrayList a = new ArrayList(); a.add(obj); ((MyClass)a.get(0)).myMethod(); James Moey wrote in message <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. > > >from >James >
Re: working with arraylist
Author: 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
Thread Navigation
This is a paginated view of messages in the thread with full content displayed inline.
Messages are displayed in chronological order, with the original post highlighted in green.
Use pagination controls to navigate through all messages in large threads.
Back to All Threads