假設我有車的列表:如何在列表的每個元素上調用方法?
public class Car {
private String brand;
private String name;
private String color;
public Car() { // ... }
public getName() { return name; }
// ...
}
// Suppose that I have already init the list of car
List<Car> cars = //...
List<String> names = new ArrayList<String>();
for (Car c : cars) {
names.add(c.getName());
}
我怎樣才能縮短上面的代碼?簡而言之,如何在列表的每個元素上調用方法?
例如,在Python:
[car.name for car in cars]
哦拉姆達,您的美麗! –
@ EM-Creations是不是她雖然... – aaiezza