重新初始化ArrayList的迭代器不起作用

发布于 2025-01-01 23:34:40 字数 1452 浏览 0 评论 0原文

鉴于以下情况:

    // 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

洋洋洒洒 2025-01-08 23:34:40

不要重新声明变量;只需分配它即可。

if (iterator.hasNext() == false) {
    iterator = players.iterator();
}

您应该小心嵌套循环行为。您的真正意图是让以下块

while (iterator.hasNext()) { ... }

实际检查这种情况吗?

while (iterator.hasNext() && (this.m_rounds > 0)) { ... }

Don't re-declare the variable; just assign it.

if (iterator.hasNext() == false) {
    iterator = players.iterator();
}

You should be careful about nested loop behavior. Is your real intent to have have the following block

while (iterator.hasNext()) { ... }

actually check for this condition?

while (iterator.hasNext() && (this.m_rounds > 0)) { ... }
惟欲睡 2025-01-08 23:34:40

您已将 Iterator; iterator =players.iterator(); 在你的循环中。

所以每次它尝试创建名为iterator的变量。

只需将其声明放在循环之外...就像

 Iterator<String> iterator;     //here ****
 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 = players.iterator();

        }
        while (iterator.hasNext())

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

 Iterator<String> iterator;     //here ****
 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 = players.iterator();

        }
        while (iterator.hasNext())
梅窗月明清似水 2025-01-08 23:34:40

我认为 google guava 几乎可以满足您的需求 Iterators#cycle

像这样使用它:

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

......你永远不会用完玩家。

I think google guava has pretty much what you want with Iterators#cycle.

Use it like this:

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

...and you will never run out of players.

好多鱼好多余 2025-01-08 23:34:40

不要使用这样的迭代器,它会把事情搞砸,就用老方法吧,我的意思是使用著名的迭代器先生“i”。而且代码看起来会更合理。

    while(m_rounds > 0){

        if(i == players.size()) {
            i = 0;
        }

        currentPlayer = players.get(i);

        //Do what you want to do with the current player...

        ...

        //Next
        i++;


    }

一个小建议,您真的需要两个标志吗,我的意思是 humanSide 和 ComputerSide?只用一个就够了吗?你的 if-else 块看起来会更加简单和清晰:

if(humanSide) {

   //Hope this move wouldn't crush your logic.

} else {

  //Algorithm based awesome move.

}

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.

    while(m_rounds > 0){

        if(i == players.size()) {
            i = 0;
        }

        currentPlayer = players.get(i);

        //Do what you want to do with the current player...

        ...

        //Next
        i++;


    }

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:

if(humanSide) {

   //Hope this move wouldn't crush your logic.

} else {

  //Algorithm based awesome move.

}
亚希 2025-01-08 23:34:40

好的,只需删除 Iterator ,意思是,当重用该迭代器时只需编写: iterator =players.iterator();

谢谢大家!

Okay just remove the Iterator<String> , meaning is , when reusing that iterator just write : iterator = players.iterator();

Thank you all !!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文