File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Creating my class Generic. Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of The Mikado Method this week in the Agile and other Processes forum!
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "Creating my class Generic." Watch "Creating my class Generic." New topic
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
    
  15

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.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Creating my class Generic.
 
Similar Threads
Niko Question
Generic Method
how to extend from a GENERIC Junit Test class
How many servlets should I use?
jaxb be converting bean to xml