Difference between revisions of "Harder TreeMap Tutorial"

From Progzoo
Jump to: navigation, search
(Top ten countries.)
(Dominated Regions)
Line 130: Line 130:
 
   //Print the keys and values for our TreeMap
 
   //Print the keys and values for our TreeMap
 
   for (String region : rList.keySet())
 
   for (String region : rList.keySet())
    System.out.println(region);
+
  {
 +
    System.out.println(region);
 +
  }
 
}
 
}
 
]]></prog><answer><![CDATA[
 
]]></prog><answer><![CDATA[
Line 141: Line 143:
 
   {
 
   {
 
     if (!rList.containsKey(c.region))
 
     if (!rList.containsKey(c.region))
 +
    {
 
       rList.put(c.region,0L);
 
       rList.put(c.region,0L);
 +
    }
 
     rList.put(c.region,(long)rList.get(c.region)+c.pop);
 
     rList.put(c.region,(long)rList.get(c.region)+c.pop);
 
   }
 
   }
 
   //Print the keys and values for our TreeMap
 
   //Print the keys and values for our TreeMap
 
   for (Country c : world.values())
 
   for (Country c : world.values())
 +
  {
 
     if (c.pop*2L>rList.get(c.region))
 
     if (c.pop*2L>rList.get(c.region))
 +
    {
 
       System.out.println(c.region+" "+c.name);
 
       System.out.println(c.region+" "+c.name);
 +
    }
 +
  }
 
}
 
}
 
]]></answer>
 
]]></answer>

Revision as of 20:43, 12 September 2012

These questions require you to create several structures.

Top ten countries.

Print the ten largest countries by population, in descending order.


[Font] [Default] [Show] [Resize] [History] [Profile]

Dominated Regions

A dominated region is one where a country has more than 50% of the region population. Print the name of the dominated region and the country which dominates it.

[Font] [Default] [Show] [Resize] [History] [Profile]

Colombia takes all.

Plucky Colombia takes over the whole of South America, transfer all resources to Colombia and remove every other country of South America.


[Font] [Default] [Show] [Resize] [History] [Profile]