如何一次又一次地使用相同的迭代器? [英] How to use the same iterator again and again?

查看:105
本文介绍了如何一次又一次地使用相同的迭代器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

给出下一个代码:



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

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

//将第一个节点定义为人类玩家,并从列表中弹出他
//其余节点是计算机端

Iterator< ;字符串> iterator = players.iterator();

//人类边
字符串humanPlayer = iterator.next();


//控制器 - 在玩家之间开始游戏,至少有两名玩家玩
而(this.m_rounds> 0)
{


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

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

开关(usersChoice)
{
案例1://然后用户选择保留
{

}

案例2://然后采取下一步
{
//让用户移动

this.m_maze = this.m_model.makeSomeMove(选择[1],humanPlayer,TRUE);

//打印迷宫以进行可视化
this.m_view.drawMaze(m_maze);

//控制器 - 减少当前游戏的当前回合数
this.m_rounds--;
}

案例31://然后用户要求最近的宝贝
{
//稍后在
上放一些代码}


案例32://然后用户要求最近的房间
{
//稍后在
放置一些代码}

} //结束开关盒

} //结束时

( 1)。每次调用 makeSomeMove 后,我如何在 humanPlayer 中放置ArrayList的第一个元素? / p>

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



非常感谢
Ron

解决方案

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



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


Given the next code :

// this is a part of some large method //

        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).How can I place in humanPlayer the first element of the ArrayList after each time that I invoke makeSomeMove ?

(2).Is it possible to reuse an iterator ? since I use the hasnext() and next() ... ?

Many thanks Ron

解决方案

If you want to reuse the iterator, you have to re-initialise it.

You have to execute Iterator<String> iterator = players.iterator(); whenever you want to reuse the iterator.

这篇关于如何一次又一次地使用相同的迭代器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆