Java Reference
In-Depth Information
public static void FormatBADescending(Team toFormat){
TreeSet<Player> battingSort =
new TreeSet<Player>(new BattingComparator());
float avg = 0.0f;
for (Player p = battingSort.last();
null != p;
if (!p.hasRole(Roles.Batter))
try {
avg = p.asBatter().getAverage();
if (avg == 0.0)
} catch (NotEnoughAtBatsException e){
System.out.println(p.getName() + "\t"
+ avg);
This will do what we want, printing out the members of a team in batting average order with
the best average printed first.
Random Access
So far, all of the collection interfaces and classes have allowed us to iterate through the objects
in our collection one at a time and do something to those objects. Although this is useful,
there are times when we would like to just find a particular member of a collection and do
something with it. We currently have sequential access to all of the objects in a collection, but
it would be nice to have some mechanism for random access as well.
Suppose, for example, we would like to get the statistics for a particular player on a team.
We could just step through the Set returned by the getRoster() method until we found the
Player object in which we are interested. But while the Java collections mean that we don't
have to implement all of the data structures we learned about in our programming classes, we
still learned something of value in those classes. One thing we learned was that linear search
like this is a bad way to implement random access. It wouldn't be terrible in this case (since
Search WWH ::

Custom Search