Re: collection of a class



"ros" <ros...@xxxxxxxxx> wrote:
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?

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
.



Relevant Pages

  • Re: collection of a class
    ... Since you admit that it's homework, I am happy to provide hints. ... and API documentation ... both the tutorials and the API docs give a good introduction: ... coll.add(someTypeThing); ...
    (comp.lang.java.programmer)
  • Re: ruby suggestion: officially sanctioned tutorials/howtos
    ... And what if documentation were the functionality of this code? ... IMHO it is completely open if there would be code or only rdoc ... Having said that it is probably a good idea to include tutorials in the ... probably using Textile markup or something similar. ...
    (comp.lang.ruby)
  • Re: Installation von Emacs 22.1 und Gnus unter Windows
    ... Weggelassen habe ich alle Beschreibungen zu Sonderbedienweisen zum ... welche Tutorials ich durchgearbeitet habe.) ... Hat mir in anderen Texteingabeumgebungen gefehlt. ... Documentation> Search Documentation String C-h d", ...
    (de.comp.editoren)
  • Re: Id RTFM if they actually GAVE me a FM!!
    ... >> documentation, and that's quite often true for Borland's docs as well. ... principle early in tutorials. ... advanced components is "most of the basics but few specialized terms and the ... You took my suggestion to Google for a tutorial far too personally and far ...
    (alt.comp.lang.borland-delphi)
  • Re: Shortage of qualified Java programmers
    ... > to the API documentation. ... > of referring to the documentation poses a potential problem. ... my selection criteria favors those who are not *dependent* on the API ... and allowing them to refer to the Javadocs for Comparator, Calendar, ...
    (comp.lang.java.programmer)