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

发布于 2025-01-02 18:11:23 字数 1714 浏览 1 评论 0原文

给出下一个代码:

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

        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() ...?

非常感谢 罗恩

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

乞讨 2025-01-09 18:11:23

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

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

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.

套路撩心 2025-01-09 18:11:23

一个简单的迭代器对此毫无用处,因为它会卡在最后一个元素上。您需要 ListIterator,以便可以将其移回到开头。

编辑:可能最好不要尝试此操作,因为您将无法修改列表(如果这样做,您将收到并发修改异常)

A simple iterator would be useless for that since it would be stuck at the final element. You want ListIterator so you can move it back to the start.

Edit: probably better not to try this however, as you will be unable to modify the list (if you do you will get a concurrent modification exception)

梦里人 2025-01-09 18:11:23

使用数组Player[]。除非您需要能够方便地增大和缩小列表的大小,否则使用数组可以使随时访问任何元素变得简单且可读。

此外,对于数组,您仍然可以使用 foreach 语法:

Player[] players = new Player[9];
...
for (Player player : players) {
    // do something
}

Use an array Player[]. Unless you need to be able to conveniently make your list grow and shrink in size, using an array makes accessing any element at any time simple and readable.

Also, with an array you can still use the foreach syntax:

Player[] players = new Player[9];
...
for (Player player : players) {
    // do something
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文