【发布时间】:2011-09-10 22:59:05
【问题描述】:
我一直在阅读有关 foreach 和列表迭代的信息,并且我了解如何设置它。
我在整个研究过程中未能找到的是如何进行多次迭代。为了更好地解释,这里是我的具体例子:
我正在创建一个 A* 路径请求管理器,它将给定数量的搜索周期分配给每个请求。因此它需要遍历其LinkedList 的请求,为每个请求分配一个搜索周期。鉴于它以 1000 个周期开始,不用说,它需要多次检查其请求列表才能平均分配其所有周期。
如何确保列表将继续迭代?对于while 或for 循环,这会是一个更好的任务吗?或者有没有办法通过foreach 做到这一点?
如果它可以帮助任何人进一步理解我的问题,这是我现在拥有的代码:
//Method: UpdateSearches()
//Purpose: to distribute all available search cycles
// between all active searches
// if a search completes or fails during this
// update, this method will notify the pathfinder
// who made the request
//Parameters: none
//Returns: nothing
public void UpdateSearches()
{
//start off with a full number of cycles
int cyclesRemaining = this.searchCyclesPerUpdate;
//cycle through all active requests
foreach (Pathfinder request in this.searchRequests)
{
while ((cyclesRemaining > 0) &&
(this.searchRequests.Count != 0))
{
//one search cycle
SearchStatus result = request.OneCycle();
//if the search completes (success or failure)
if (result == SearchStatus.targetFound ||
result == SearchStatus.targetNotFound)
{
//remove this request from the queue
this.searchRequests.Remove(request);
} //end if
//move on to the next request
cyclesRemaining--;
} //end while
} //end foreach
} //end method
【问题讨论】:
标签: c# list foreach linked-list iteration