-4

I am working on a project which will read data into a list. I have a question in the for loop. How does the Feature city : cities work? I understand that cities is the list, but what does city means? city is not predefined, how does java understand city?

List<Feature> cities = GeoJSONReader.loadData(this, cityFile);
    cityMarkers = new ArrayList<Marker>();
    for(Feature city : cities) {
      cityMarkers.add(new CityMarker(city));
    }

3 Answers3

0

This is a for each loop.what it does is this: For each city in you city list it show perform what ever that is within the cury brace.the city is each city as you iterate over your city list

suulisin
  • 1,405
  • 1
  • 10
  • 16
0

The compiler translates this into something that makes more sense. It probably comes out to something like:

ListIterator<Feature> cityIterator = cities.listIterator();
while(cityIterator.hasNext()) {
 cityMarkers.add(new CityMarker(cityIterator.next());
}

The enhanced for loop syntax using : just makes it easier to write and read the above code.

nhouser9
  • 6,692
  • 3
  • 20
  • 40
0

In the loop your are doing a iteration on cities. city is a element of your list citie. In each iteration, the current element of the list is city.

Youssouf Maiga
  • 5,211
  • 7
  • 23
  • 37