collection of a class

R

ros

Hi,

I am working on this exercise where I have to add a method to a class
that returns 'collections' of another class.
Can anybody explain how I can do it?

Thanks
ros
 
A

Andrew Thompson

I am working on this exercise where I have to add a method to a class
that returns 'collections' of another class.
Can anybody explain how I can do it?

What do your textbooks say?
(They should always be your first
reference on such matters)

Andrew T.
 
L

Lew

Since you admit that it's homework, I am happy to provide hints.

First, let's be very precise about terminology. Did you want collections (no
quotes needed) of classes, or instances of classes (i.e., objects)? The first
cannot be done, but you can make collections of Class objects. All Java
collections are collections of objects.

Second, you should know that the collections classes and just about everything
else in the Java world is heavily documented, and where to find the
documentation. The Sun tutorials

<http://java.sun.com/docs/books/tutorial/index.html>

and API documentation

<http://java.sun.com/javase/6/docs/api/>

are two of your best resources, and of course, GIYF.

For collections, both the tutorials and the API docs give a good introduction:

<http://java.sun.com/docs/books/tutorial/collections/index.html>
<http://java.sun.com/javase/6/docs/technotes/guides/collections/overview.html>

For how to return something from a method, see

<http://java.sun.com/docs/books/tutorial/java/javaOO/returnvalue.html>

Now let us try something - start with a collection of some type of object:

Collection <SomeType> coll = new HashSet <SomeType> ();

This line or one similar to it might appear somewhere inside the method you
are writing.

Somewhere further down in that method you will have a line something like

coll.add( someTypeThing );

and further down still,

return coll;

You will have written the method signature to signal that the return type is

Collection <SomeType>
(it's in the tutorial how to do that)

and Bob's your uncle.

-- Lew
 
R

ros

Since you admit that it's homework, I am happy to provide hints.

First, let's be very precise about terminology. Did you want collections (no
quotes needed) of classes, or instances of classes (i.e., objects)? The first
cannot be done, but you can make collections of Class objects. All Java
collections are collections of objects.

Second, you should know that the collections classes and just about everything
else in the Java world is heavily documented, and where to find the
documentation. The Sun tutorials

<http://java.sun.com/docs/books/tutorial/index.html>

and API documentation

<http://java.sun.com/javase/6/docs/api/>

are two of your best resources, and of course, GIYF.

For collections, both the tutorials and the API docs give a good introduction:

<http://java.sun.com/docs/books/tutorial/collections/index.html>
<http://java.sun.com/javase/6/docs/technotes/guides/collections/overvi...>

For how to return something from a method, see

<http://java.sun.com/docs/books/tutorial/java/javaOO/returnvalue.html>

Now let us try something - start with a collection of some type of object:

Collection <SomeType> coll = new HashSet <SomeType> ();

This line or one similar to it might appear somewhere inside the method you
are writing.

Somewhere further down in that method you will have a line something like

coll.add( someTypeThing );

and further down still,

return coll;

You will have written the method signature to signal that the return type is

Collection <SomeType>
(it's in the tutorial how to do that)

and Bob's your uncle.

-- Lew

THANK YOU so much for the help Lew. That's what I needed: GUIDANCE!
I don't want solution the the exercise but just hints and pointers in
the right direction.
I understand what you wrote and shall attempt it and post it here.
Would be really thankful if you could check whether I am on the right
track.

Thanks
ros
 
R

ros

What do your textbooks say?
(They should always be your first
reference on such matters)

Andrew T.

Thanks Andrew, I consulted the books but got confused on the way hence
wanted some advice.
Lew has explained it well and I shall post my attempt here and would
appreciate if you could check if it is ok.
Cheers
ros
 
A

Andrew Thompson

ros wrote:
..
Thanks Andrew, I consulted the books but got confused on the way hence
wanted some advice.

Asking some specific questions will generally help with that.
Not only do the specific questions help get specific answers,
but the questions themselves help people to determine what
part of the problem you are having trouble with, or what parts
you misunderstand.
Lew has explained it well ..

Lew's explanations are quite often excellent, you are lucky
to have gained his attention and reply.
..and I shall post my attempt here and would
appreciate if you could check if it is ok.

I will be happy to help on any specific question
that I have knowledge of. (Though note that I do
sometimes jump in and offer 'general advice' like
above, on some topics that I am simply interested
in knowing more about - and am no expert on.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top