【发布时间】:2012-02-26 16:13:48
【问题描述】:
鉴于以下情况:
// get the list of the players , in order to start the game
ArrayList<String> players = this.m_maze.getPlayers();
// human side
String humanPlayer = iterator.next();
String computerPlayer = null;
// define iterator for the players
Iterator<String> iterator = players.iterator();
boolean humanSide = true ,computerSide = false; // assume the human player is starting the game
// controller - start a game between the players , at least two players are playing
while (this.m_rounds > 0)
{
if (humanSide == false && computerSide == true) // then this is the turn of the human side
{
if (iterator.hasNext() == false)
{
// reinitialize the iterator
Iterator<String> iterator = players.iterator();
}
while (iterator.hasNext())
// more code
我尝试重用迭代器,但出现“重复的局部变量迭代器”编译错误。我怎样才能重用那个迭代器? 谢谢,罗恩
编辑:
if (iterator.hasNext() == false)
{
// reinitialize the iterator
iterator = players.iterator();
}
while (iterator.hasNext())
{
computerPlayer = iterator.next();
// computer decides what would be his next move , between 1 - 3
【问题讨论】:
-
听起来你真正想做的是循环迭代。您是否考虑过实际使用循环数据结构?
-
这是个好主意,我会考虑的,谢谢!