Skip to main content

Generics example - fill me in, please...

2 replies [Last post]
coder111
Offline
Joined: 2009-03-09
Points: 0

Sorry to be such a dense wabbit, but I am too new to this code style. Can anyone explain this to me, from the Generics trail,

http://java.sun.com/docs/books/tutorial/java/generics/genmethods.html

.....
A more realistic use of generic methods might be something like the following, which defines a static method that stuffs references to a single item into multiple boxes:

In the below example, what is the code that is supposed to go in the ellipses at
"Crayon red = ...; " and List> crayonBoxes = ...; ??

Or maybe I'm supposed to know how to do that already? (tutorial writers?)
cheers,

------------------------------

public static void fillBoxes(U u, List> boxes) {
for (Box box : boxes) {
box.add(u);
}
}

To use this method, your code would look something like the following:

Crayon red = ...;
List> crayonBoxes = ...;

The complete syntax for invoking this method is:

Box.fillBoxes(red, crayonBoxes);

------------------------------

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
prunge
Offline
Joined: 2004-05-06
Points: 0

It means that you somehow need to get a reference to a Crayon object and a list of crayon boxes.

You might create a Crayon using a constructor (assuming such a constructor exists):

Crayon red = new Crayon(Color.RED);

constructing a list of crayon boxes could be done with something like:

List> crayonBoxes = new ArrayList>();

teleporter
Offline
Joined: 2012-05-14
Points: 0

Sorry for appreciating your work just now, I am a new member so please bear with me. Btw nice one!