java - Iterator and regular for loop -


i understand how iteration work may need more knowledge it. can 1 please show me main difference between these 2 statements:

while (scanner.hasnext()) {         tokenizer = new stringtokenizer(scanner.nextline());         numberofitems = integer.parseint(tokenizer.nexttoken());         int[] numbers = new int[numberofitems];         (int i:numbers) {             numbers[i] = integer.parseint(tokenizer.nexttoken());         }         system.out.println(isjolly(numbers));     }  while (scanner.hasnext()) {         tokenizer = new stringtokenizer(scanner.nextline());         numberofitems = integer.parseint(tokenizer.nexttoken());         int[] numbers = new int[numberofitems];         (int = 0; < numberofitems; i++) {             numbers[i] = integer.parseint(tokenizer.nexttoken());         }         system.out.println(isjolly(numbers));     } 

why these giving me 2 different output?

you have created empty array (array filled zeroes).

    int[] numbers = new int[numberofitems]; 

in case of

 ( int = 0; < numbers.length; i++ ) ... 

i starts 0 , on each iteration incremented (i++). iterations finished when became equals or more numbers.length (aka numberofitems). sequence of values 0,1,2,3,4,5,...

in case of

for (int i:numbers) {  

you iterate on each value taken array , sequence of zeroes ( 0,0,0,0,0, ...).

and yours number[i] = update number[0] element of resulting array.


Comments

Popular posts from this blog

html - Outlook 2010 Anchor (url/address/link) -

javascript - Why does running this loop 9 times take 100x longer than running it 8 times? -

Getting gateway time-out Rails app with Nginx + Puma running on Digital Ocean -