//Construct a list for this example
List<String> colors = new ArrayList<String>(Arrays.asList("Yellow", "Cyan", "Magenta", "Green"));
String first, last;
//Get the first and last element
first = colors.get(0);
last = colors.get(colors.size() - 1);
List<String> colors = new ArrayList<String>(Arrays.asList("Yellow", "Cyan", "Magenta", "Green"));
//Getting the first three elements in the list
//We want to get a subList from index1=0 to index2=2
//NOTE WELL: We use "3" for second param (index2+1)
List<String> threeColors = colors.subList(0,3);
//Print results
println("colors: " + Arrays.toString(colors.toArray()));
println("threeColors: " + Arrays.toString(threeColors.toArray()));
public Country countryNamed(String countryName)
{
//Return the matching country (or null if none found)
for (Country eachCountry: this.getCountries())
if (eachCountry.getName().equalsIgnoreCase(countryName))
return eachCountry;
//None found, so return null (meaning nothing)
return null;
}
public Country countryWithCapital(String capitalName)
{
//Return the matching country that has capital matching method param
for (Country eachCountry: this.getCountries())
if (eachCountry.getCapital().equalsIgnoreCase(capitalName))
return eachCountry;
return null;
}
public List<Country> countriesNamed(List<String> countryNames)
{
//Return the countries that have names that match any of the
//names in the method parameter "countryNames"
List<Country> matches = new ArrayList<Country>();
for (Country eachCountry: this.getCountries())
if (countryNames.contains(eachCountry.getName()))
matches.add(eachCountry);
return matches;
}
public List<Country> selectWarmerThan(int thresholdTemperatureF)
{
//Return a list of all countries warmer then method param
List<Country> matches = new ArrayList<Country>();
for (Country eachCountry: this.getCountries())
if (eachCountry.getAveTemperature() > thresholdTemperatureF)
matches.add(eachCountry);
return matches;
}