【问题标题】:Re-initialization of iterator of ArrayList does not workArrayList 的迭代器重新初始化不起作用
【发布时间】:2012-02-26 16:13:48
【问题描述】:

鉴于以下情况:

    // 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 

【问题讨论】:

  • 听起来你真正想做的是循环迭代。您是否考虑过实际使用循环数据结构?
  • 这是个好主意,我会考虑的,谢谢!

标签: java arraylist iterator


【解决方案1】:

不要重新声明变量;分配它。

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

您应该小心嵌套循环行为。您是否真正打算拥有以下区块

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

真的检查这种情况吗?

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

【讨论】:

    【解决方案2】:

    您已将Iterator&lt;String&gt; 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())
    

    【讨论】:

    • 这将抛出一个NullPointerException,因为iteratorhasNext() 的调用之前没有赋值。
    • @cheeken 如果是这种情况,那么根据他在 hasNext() 之前没有为迭代器分配任何值的代码,它已经抛出异常 bcoz
    【解决方案3】:

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

    像这样使用它:

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

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

    【讨论】:

      【解决方案4】:

      不要使用这样的迭代器,它会搞砸,就用老方法,我的意思是使用著名的迭代器先生“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.
      
      }
      

      【讨论】:

        【解决方案5】:

        好的,只需删除 Iterator&lt;String&gt; 即可,意思是,在重用该迭代器时只需编写:iterator = players.iterator();

        谢谢大家!!

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2017-06-29
          • 1970-01-01
          • 2016-09-12
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多