• Post Reply Bookmark Topic Watch Topic
  • New Topic

Creating my class Generic.  RSS feed

 
ellie spring
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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.
 
Rob Spoor
Sheriff
Posts: 21131
87
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Make the class implement Iterable<Item>, and give it an iterator() method that returns an Iterator<Item>.
 
ellie spring
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 3184
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I would suggest you to check out this Generics FAQ and other FAQs in the page.
 
ellie spring
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I see.
Well that explaines it.

Thanks.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!