Replace for loop with java 8 foreach for updating values

后端 未结 3 1766
礼貌的吻别
礼貌的吻别 2021-01-15 04:49

I\'m looking to replace the following for loop with an elegant java 8 stream or lambda solution. Is there anything concise and efficient?

    public static v         


        
3条回答
  •  礼貌的吻别
    2021-01-15 05:31

    Yes you can achieve this using this line of code:

    IntStream.range(1,myList.size())
        .forEachOrdered(i->myList.set(i,(myList.get(i)+myList.get(i-1)/2)));
    System.out.println(myList);
    

提交回复
热议问题