The Artima Developer Community
Sponsored Link

Java Answers Forum
get an element of an ArrayList

2 replies on 1 page. Most recent reply: Mar 12, 2003 4:28 AM by Ptite Ce

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Ptite Ce

Posts: 4
Nickname: ptitece
Registered: Feb, 2003

get an element of an ArrayList Posted: Feb 26, 2003 9:02 AM
Reply to this message Reply
Advertisement
Hello,

I created a new class of objects, say Titi.
Then I created an ArrayList of Titi, say TitiList
Ok it works.
After that, I want to retrieve each element of my TitiList,
like TitiList.get(i),
AND I want this get method to return an object of type Titi, instead of the default Object type.
How can I do that?

Thanks,
Ptite Ce.

class TitiList /*extends ArrayList*/
{
/* constructor */
...
 
/* methods */
public Titi myGet(int index) {
  returns ....???
}
} 


Andrew Matthews

Posts: 9
Nickname: taxman
Registered: Feb, 2003

Re: get an element of an ArrayList Posted: Mar 4, 2003 5:30 AM
Reply to this message Reply
This code should do it quite nicely:

Titi returntiti = (Titi)arraylist.get(i);
 


You could also use:

return (Titi)arraylist.get(i); 
 

Ptite Ce

Posts: 4
Nickname: ptitece
Registered: Feb, 2003

Re: get an element of an ArrayList Posted: Mar 12, 2003 4:28 AM
Reply to this message Reply
Thanks,

I found the answer by myself (wooo!).
People told me that usually it is better to cast the get() out from my new class (it seems to be the usual way of doing it)
What do you think about this advice?

Cheers
PtiteCe.

Flat View: This topic has 2 replies on 1 page
Topic: Using java sockets under windows xp Previous Topic   Next Topic Topic: Trouble with Java Game

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use