简体   繁体   English

为每个循环嵌套?

[英]Nested for each loops?

        for ( Route r : timetable.keySet())
        {
            for (Station s: r?)   //compile error here.
        }

Hi all, basically I'm trying to write a method that adds stations to a combobox from a timetable. 大家好,基本上,我正在尝试编写一种将时间表中的站点添加到组合框的方法。 The timetable is essentially a hashmap that returns a Route, List(of type service), and a route is definined by a string representing the name of the route, and 时间表本质上是一个返回路线,列表(服务类型)的哈希表,并且路线由表示路线名称的字符串定义,并且

// a list of stations visited on this route 
private ArrayList<Station> stations;

Basically I need to access the stations in the route so I'm using a foreach loop to get each route in the timetable and then trying to get all the stations in that route, but I'm not sure how I would do the second nested loop, as in which list should I be referring to as I am getting a compile error if I refer the route. 基本上,我需要访问路线中的站点,因此我使用了一个foreach循环来获取时间表中的每个路径,然后尝试获取该路径中的所有站点,但是我不确定如何进行第二个嵌套循环,当我引用路由时遇到编译错误,因此我应该参考哪个列表。

I cannot modify the Route class, as that has been provided and I need to work around that but it does have this method to return the stations 我无法修改Route类,因为已经提供了该类,因此我需要解决此问题,但是它确实具有返回站的方法

   public Station getStop(int i) {
    if (i < 1 || i > stations.size()) {
        throw new NoSuchStopException("No " + i + "th stop on route" + this);
    }
    return stations.get(i - 1);
}

You will have to modify the class Route and add a getter method that returns the ArrayList (because it's private ): 您将必须修改Route类并添加一个返回ArrayListgetter方法(因为它是private ):

public class Route {
    // ...

    public List<Station> getStations()
    {
        return stations;
    }
}

Then you can use that method in the for loop: 然后,您可以在for循环中使用该方法:

for (Route r : timetable.keySet()) {
    for (Station s : r.getStations()) {
        // ...
    }   
}

Edit: As @DavidWallace mentioned, if you don't want someone to modify the ArrayList through the getter method, you can return an unmodifiableList : 编辑:如@DavidWallace所述,如果您不希望有人通过getter方法修改ArrayList ,则可以返回一个unmodifiableList

return Collections.unmodifiableList(stations);

You could achieve a solution as follows but it doesn't use nested for-each loops since it requires catching the exception to know when there are no more stations for that route (this solution does not require any changes to the Route class): 您可以实现以下解决方案,但是它不使用嵌套的for-each循环,因为它需要捕获异常以知道何时该路由没有更多的站(此解决方案不需要对Route类进行任何更改):

for ( Route r : timetable.keySet())
{
    int stationIndex = 1;
    try {
        do {
            Station station = route.getStop(stationIndex++);
            // todo - put the station in the combo box
        } while(true);
    } catch(NoSuchStopException nsse) {
        // we have reached the index where there wasn't a stop, so move to the next route
    }
}

You could write this as 你可以这样写

for ( Route r : timetable.keySet()) {
    for (Station s: r.getStations()) {

        // ...

    }
}

provided the Route class has a method like 如果Route类具有类似

public List<Station> getStations() {
    return Collections.unmodifiableList(stations);
}

Note the use of unmodifiableList . 注意使用unmodifiableList This prevents someone from writing code that calls getStations and then modifies the result. 这样可以防止某人编写调用getStations代码,然后修改结果。 Basically, it should be impossible to use a "getter" method to modify a class; 基本上,应该不可能使用“ getter”方法来修改类。 hence using the various "unmodifiable" wrapper methods in the Collections class is best practice for a getter that returns a collection. 因此,对于返回集合的getter,在Collections类中使用各种“不可修改的”包装器方法是最佳实践。

Edit 编辑

Dealing with the new requirement of not being able to change Route , and assuming that there is a method getNumberOfStops as well as the getStop of the question, you could write 处理不能更改Route的新要求,并假设存在方法getNumberOfStops和问题的getStop ,则可以编写

for (Route r : timetable.keySet()) {
    for (int stopNumber = 1; stopNumber <= r.getNumberOfStops(); stopNumber++) {
        Station s = r.getStop(stopNumber);
        // ...
    }
}

for-each loop iterate collection or array type and Route object r is not iterable type that is why it raised compiler error . for-each循环迭代集合或数组类型,而Route对象r不是可迭代的类型,这就是为什么它引发编译器错误

for ( Route r : timetable.keySet()){
     for (Station s: r.getStations()){ // Get the station List from route object
       ...
     }   
}

Note: assumed there must be getter method of stations in Route class. 注意:假设在Route类中必须有stations吸气剂方法。

Explanation of for each loop 每个循环的说明

for(XXX a : YYY)    
YYY --- > should be any class that implements iterable  
and XXX objects should be in YYY 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM