| Author |
Creating my class Generic.
|
ellie spring
Greenhorn
Joined: Jun 15, 2011
Posts: 24
|
|
hi,
I've created a class as generic type. Lets say Cart class.
And i declared it to only accept Item objects.
How can i make my class object as iteratable.
Because i want it to be able to be used inside the enhanced for loop.
thanks in advance.
|
"Nothing is impossible, impossible takes a little longer".
|
 |
Rob Spoor
Sheriff
Joined: Oct 27, 2005
Posts: 19216
|
|
|
Make the class implement Iterable<Item>, and give it an iterator() method that returns an Iterator<Item>.
|
SCJP 1.4 - SCJP 6 - SCWCD 5
How To Ask Questions How To Answer Questions
|
 |
ellie spring
Greenhorn
Joined: Jun 15, 2011
Posts: 24
|
|
Thanks for the reply.
I want to know also the advantage and disadvantage of making my class as generic.
And also when to use it.
thanks.
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
|
I would suggest you to check out this Generics FAQ and other FAQs in the page.
|
Mohamed Sanaulla | My Blog
|
 |
ellie spring
Greenhorn
Joined: Jun 15, 2011
Posts: 24
|
|
Oh I see.
Well that explaines it.
Thanks.
|
 |
 |
|
|
subject: Creating my class Generic.
|
|
|