gpt4 book ai didi

java - 如何一次又一次地使用同一个迭代器?

转载 作者:行者123 更新时间:2023-12-02 05:14:37 27 4
gpt4 key购买 nike

给出下一个代码:

//这是某个大方法的一部分//

        ArrayList<String> players = this.m_maze.getPlayers();

// define the first node to be the human player , and pop him from the list
// the rest of the nodes are the computer side

Iterator<String> iterator = players.iterator();

// human side
String humanPlayer = iterator.next();


// controller - start a game between the players , at least two players are playing
while (this.m_rounds > 0)
{


String[] choices = this.m_view.getChoiceFromUser();

int usersChoice = Integer.parseInt(choices[0]);

switch (usersChoice)
{
case 1: // then user chose to stay put
{

}

case 2: // then take the next step
{
// let the user make his move

this.m_maze = this.m_model.makeSomeMove(choices[1],humanPlayer,true);

// print out the maze for visualization
this.m_view.drawMaze(m_maze);

// controller - reduce the number of current rounds for the current game
this.m_rounds--;
}

case 31: // then user asked for the closest treasure
{
// put some code here later on
}


case 32: // then user asked for the nearest room
{
// put some code here later on
}

} // end switch case

} // end while

(1).每次调用 makeSomeMove 后,如何将 ArrayList 的第一个元素放入 humanPlayer 中?

(2).是否可以重用迭代器?因为我使用 hasnext()next() ...?

非常感谢罗恩

最佳答案

如果你想重用迭代器,你必须重新初始化它。

您必须执行Iterator<String> iterator = players.iterator();每当你想重用迭代器时。

关于java - 如何一次又一次地使用同一个迭代器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56305705/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com