Okay so I need help understanding something. I understand how “? :” are used together but reading over some beginning Java stuff I see this situation popping up in a few places. Most recently is this…
public static <U> void fillBoxes(U u, List<Box<U>> boxes) {
for (Box<U> box : boxes) {
box.add(u);
}
}
What I am confused about is what exactly “:” is doing. Any help would be appreciated. I am looking at this example on a page at Oracle’s website, located here: http://download.oracle.com/javase/tutorial/java/generics/genmethods.html
That is Java’s for-each looping construct. It has nothing to do with generics per-se or: is not for use exclusively with generics. It’s shorthand that says:
for every type box in the collection named boxes do the following...Here’s the link to the official documentation.
Simpler code sample: (instead of managing generics performing a summation of an int array)