重新初始化ArrayList的迭代器不起作用
鉴于以下情况:
// 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
Given the following :
// 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
I try to reuse the iterator but I get a "Duplicate local variable iterator" compilation error. How can I reuse that iterator ?
Thanks ,Ron
EDIT :
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要重新声明变量;只需分配它即可。
您应该小心嵌套循环行为。您的真正意图是让以下块
实际检查这种情况吗?
Don't re-declare the variable; just assign it.
You should be careful about nested loop behavior. Is your real intent to have have the following block
actually check for this condition?
您已将
Iterator; iterator =players.iterator();
在你的循环中。所以每次它尝试创建名为
iterator
的变量。只需将其声明放在循环之外...就像
You have put
Iterator<String> iterator = players.iterator();
in your loop.So each time it tries to create variable with name
iterator
.Just put it's declaration out of the loop... like
我认为 google guava 几乎可以满足您的需求
Iterators#cycle
。像这样使用它:
......你永远不会用完玩家。
I think google guava has pretty much what you want with
Iterators#cycle
.Use it like this:
...and you will never run out of players.
不要使用这样的迭代器,它会把事情搞砸,就用老方法吧,我的意思是使用著名的迭代器先生“i”。而且代码看起来会更合理。
一个小建议,您真的需要两个标志吗,我的意思是 humanSide 和 ComputerSide?只用一个就够了吗?你的 if-else 块看起来会更加简单和清晰:
Don't use iterators like this, it can mess things up, just do it the old way, I mean using the famous Mr. Iterator "i". Moreover the code would look more sensible.
A small suggestion, do you really need both the flags, i mean the humanSide and computerSide? Won't using just one suffice? Your if-else block would look a lot more simpler and clear:
好的,只需删除
Iterator
,意思是,当重用该迭代器时只需编写:iterator =players.iterator();
谢谢大家!
Okay just remove the
Iterator<String>
, meaning is , when reusing that iterator just write :iterator = players.iterator();
Thank you all !!