Skip to main content

Generics example - fill me in, please...

2 replies [Last post]
Joined: 2009-03-09

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,

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?)


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

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.
Joined: 2004-05-06

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>();

Joined: 2012-05-14

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