【问题标题】:Is there any possibility to check the previous and next element in foreach loop? [duplicate]是否有可能检查 foreach 循环中的上一个和下一个元素? [复制]
【发布时间】:2019-10-16 12:17:30
【问题描述】:

在 java 中使用 for each 如何检查前一个和下一个元素。 是否有可能检查每个循环中的上一个和下一个元素

【问题讨论】:

  • 当然。发布您的代码
  • 最好选择带索引的老式for loop
  • 您可以将前一个元素存储在一个变量中。但是下一个元素只有在下一次迭代时才能知道。为什么不使用标准 for 循环?
  • 你也可以使用 listIterator 因为它同时拥有 next 和 previous 方法。
  • 如果您喜欢任何答案,您当然可以投赞成票。但除非完全错误,否则请不要投反对票。谢谢 :) 干杯!

标签: java


【解决方案1】:

没有。

“for each”构造在下面使用Iterator,它只有next()hasNext() 函数。没有办法得到previous()

Lists 有一个ListIterator,它允许查看前一个元素,但“for each”不知道如何使用它。因此,唯一的解决方案是在单独的变量中记住前一个元素,或者只使用这样的简单计数循环:for(int i=0;i< foo.length();i++)

【讨论】:

    【解决方案2】:

    这样做的目的是什么?

    您可能应该使用for 循环和索引来代替foreach

    for(int i=0;i<lenght;i++) {
        list[i-1].dostuff //previous
        list[i].dostuff //current
        list[i+1].dostuff //next item
    }
    

    别忘了检查下一个和上一个是否存在。

    【讨论】:

    • 您不检查要检索的元素是否存在(我知道您提到检查下一个项目是否存在,但仍然存在)并且您不使用大括号进行 for 循环。
    • 您的回答会导致数组在第一次通过时越界并且在语法上是错误的
    • 是的,我知道这不是完美的代码,我只是在想象它是如何完成的
    【解决方案3】:

    使用“for each”只有next()和hasNext()方法,所以没有提供反向遍历或提取元素的方法。

    考虑到你有一个字符串的ArrayList,你可以使用java.util.ListIterator,它提供了hasPrevious()previous()等方法

    下面是如何使用它的示例。 ** 阅读代码中的 cmets,因为其中包含使用这些方法的重要细节。**

            ArrayList<String> mylist = new ArrayList<>();
    
            ListIterator<String> myListIterator = mylist.listIterator();
    
            myListIterator.hasNext(); // Returns: true if list has next element
            myListIterator.next(); // Returns: next element , Throws:NoSuchElementException - if the iteration has no next element
            myListIterator.hasPrevious(); // Returns: true if list has previous element
            myListIterator.previous(); //Returns: the previous element in the list , Throws: NoSuchElementException - if the iteration has no previous element
    

    希望这会有所帮助。

    PS:您应该已经发布了到目前为止所做的代码, 在stackoverflow上发布不包含任何表明您的努力的问题真的很糟糕。

    【讨论】:

      【解决方案4】:

      for Each 循环的一些限制。可以阅读整篇文章 清晰的概览 https://www.geeksforgeeks.org/for-each-loop-in-java/

      另外,这可以是一种方式:

          public class Main
      {
          public static void main(String[] args) {
          String[] arr={"a","b"};
          String next="";
          String current="";
          String prev="";
      
          for(int i=0;i<arr.length;i++){
      
              //previousElement
              if(i>0){ 
                  prev=arr[i-1] ;
                  System.out.println("previous: "+prev);
             }else{
                 prev="none";
                System.out.println("previous: "+prev);
                     }
      
               //currentElement      
              current=arr[i];
             System.out.println(" current: "+current);
      
             //nextElement
            if(i<arr.length-1){
                next=arr[i+1];
                System.out.println(" next: "+next);
                    }else{
                        next="none";
                        System.out.println(" next: "+next);
                                    }
                      }
        }
      }
      

      附加输出:

      附注可以查看https://www.onlinegdb.com/online_java_compiler上的代码

      【讨论】:

      • 如果有时String[] arr={"a","b"};
      • @manikantgautam 更新了解决方案。如果满意请采纳
      猜你喜欢
      • 2011-04-02
      • 1970-01-01
      • 2016-03-06
      • 2016-04-01
      • 2011-07-03
      • 2014-09-12
      • 2012-10-11
      • 2021-12-12
      • 1970-01-01
      相关资源
      最近更新 更多