在Java中使用for循环 for(feature city : cities)

9 浏览
0 Comments

在Java中使用for循环 for(feature city : cities)

这个问题已经有答案了:

在for循环中,(int i : tall)是什么意思,其中tall是一个整数数组[duplicate]

我正在开发一个将数据读入列表的项目。在for循环中,我有一个问题。特性city:cities是如何工作的?我知道cities是一个列表,但city是什么意思?city没有预定义,Java如何理解city?

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

admin 更改状态以发布 2023年5月23日
0
0 Comments

编译器将其转换为更有意义的代码。可能转换成类似如下的代码:

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

使用:的增强型for循环语法只是让上述代码更容易编写和阅读。

0
0 Comments

这是一个foreach循环,它的作用如下:

对于你城市列表中的每个城市,它会在当前大括号内执行任何操作。城市就是遍历城市列表时的每个城市。

0