java - For-Each Loop Alternative - Clarification -
i working through lessons in java, , have not been able understand going on in following lesson.
purpose: purpose of lesson explain how for-each loop works creating alternative simulates how for-each loop work.
in previous lesson, explanation of for-each loop simple:
import java.util.arraylist //imports arraylist class public class foreachloops { public static void main(string[] args) { arraylist<integer> numbers = new arraylist<integer>(); // ^ creates arraylist object & calls constructor for(int = 1; < 11; ++i) numbers.add(i); //just adding numbers 1-10 arraylist. for(int n : numbers) //for-each loop system.out.println(n); //returns value each arraylist element } }
in next lesson, instructor showing how create alternative/simulator of for-each loop. works follows:
import java.util.arraylist; //importing arraylist class package import java.util.iterator; // question 1 (see below) public class chap16part9 { public static void main(string[] args) { arraylist<integer> numbers = new arraylist<integer>(); // ^ creating object of arraylist & calling constructor for(int = 1; < 11; ++i) numbers.add(i); //just adding numbers 1-10 arraylist object for(iterator<integer> n = numbers.iterator(); n.hasnext();) { // ^ question 2 (see below) int num = n.next(); //points next element assign value. system.out.println(num); //prints current value of "num." } } }
question 1: after reading java documentation, "iterator" listed interface. still same importing class? example, in java documentation, "java.util.scanner" considered class, , not interface.
question 2: before asking question, loop not need "++i" type of control implemented because hasnext() method of iterator checks see if there element check. if not, stop. it's boolean value. beginning of loop, however, loop-control variable (lcv) "iterator<integer> n = numbers.iterator();
" , not quite understand meant this. in eyes, see object of iterator class? being set equal arraylist iterator() method. because given "java.util.interator;" considered interface (basically contract has declared methods must implemented if implemented class), arraylist class completes set of statements of declared iterator() methods in arraylist class and, therefore, needs called way instead of constructor traditional classes called (the arraylist, example)? bit confused this, , hoping can shed light on this. thank you!
java reference types include classes, interfaces , enums (as arrays, bit different). of them pieces of code define behavior, , therefore need imported program know methods available in them, etc.
so interface such iterator
has code says methods , constants defined in it, parameters methods accept, types return, , on. compiler must know able check correctness of program , generate proper calls methods in byte code creates.
the method iterator()
in list
returns iterator on items in list. is, object class implements iterator
. don't know class , how it's implemented - information encapsulated in particular list's code - know implements iterator
, therefore has methods declared in iterator
, implements them according iterator
contract.
for reason, can assign reference object implements interface x
variable type x
, though it's interface type. variable points actual object class, program interested in fact implements x
.
iterator<integer> n = numbers.iterator();
does that. says "declare n
variable of interface type iterator<integer>
. ask numbers
list iterator, , assign n
. "loop variable" iterator
object. because know of type iterator
- implements iterator
contract - know can use hasnext()
, next()
on it. , that's in loop condition check , in following line.
Comments
Post a Comment