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
Post a Comment