Displaying city names
Managing sets of locations is a common task in applications like travel planning, navigation systems, or user-generated location sets. In this exercise, you will practice using an enhanced for-loop to display a set of cities. This approach ensures you can easily retrieve and process city names from a collection.
The HashSet class has been imported for you.
Deze oefening maakt deel uit van de cursus
Input/Output and Streams in Java
Oefeninstructies
- Use an enhanced
for-loopto travel through the created setcities. - Print each city inside the loop.
Praktische interactieve oefening
Probeer deze oefening eens door deze voorbeeldcode in te vullen.
public class CityDisplay {
public static void main(String[] args) {
HashSet cities = new HashSet<>();
cities.add("New York");
cities.add("Los Angeles");
cities.add("Chicago");
// Use enhanced for-loop
for (____ city: ____) {
// Print each city name inside the set
System.out.println(____);
}
}
}