What is difference between Iterator and for loop

Answer Posted / a.jyotsna

The Iterator is not implemented as an
indexed loop, at least not in Sun Java. The collection
always has a
getter armed with the iterator's next value. Much of the
collection's
internal implementation is done via the iterator. But the
pieces of
ArrayList that are optimized for speed, skip bounds
checking and use int
array offsets to do their work.
I would like to know what is the difference between these 2.
>
> ArrayList list = new ArrayList
>
> for(int i=0; i<list.size(); i++){
> //do stuff
> }
>
> Iterator i = list.iterator();
>
> while(i.hasNext(){
> do stuff
> }

Is This Answer Correct ?    6 Yes 6 No



Post New Answer       View All Answers


Please Help Members By Posting Answers For Below Questions

Explain which of the following methods releases the lock when yield(), join(),sleep(),wait(),notify(), notifyall() methods are executed?

837


hr interview how many minutes asking question

1797


Which collection is thread safe in java?

727


What is indexof?

722


What is a boolean field?

718


What are the differences between string and stringbuffer?

824


What is the difference between the Reader/Writer class hierarchy and the InputStream/OutputStream class hierarchy?

858


Define how can we find the actual size of an object on the heap?

888


What is composition in java?

938


Can a hashset contain duplicates java?

694


What is classes in java?

697


How many types of keywords are there?

749


can rmi and corba based applications interact ?

841


What is array length?

709


What is charat ()?

712