-1

Is there any short enhanced form of for loop which we can use for primitives. I have do the legacy code for looping on to primitives, like this

for(int i=0; i<10; i++){
}

Although for objects, we have ehanced for loop like

for(MyObject m : myObjectList){
}

Is there any similar option for primitives also?

Tunaki
  • 125,519
  • 44
  • 317
  • 399
KayV
  • 11,523
  • 10
  • 84
  • 128

1 Answers1

-2

Found the solution, IntStreams in java 8 can replace the regular for-loop utilizing IntStream.range()

IntStream.range(1, 4)
.forEach(System.out::println);
KayV
  • 11,523
  • 10
  • 84
  • 128
  • 12
    That is not an "enhanced for loop". This is using a completely different API. – Tunaki Nov 23 '16 at 13:10
  • @Tunaki the duplicate you marked seems _a completely different_ question about same topic... maybe this fits better http://stackoverflow.com/questions/266570/modern-for-loop-for-primitive-array but.... – Jordi Castilla Nov 23 '16 at 13:14