File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Infinte iteration in a list Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Infinte iteration in a list" Watch "Infinte iteration in a list" New topic
Author

Infinte iteration in a list

ragi singh
Ranch Hand

Joined: Mar 10, 2010
Posts: 198

I have a code bit (source :- me )



When i run this code my iterator iterates infinitely . I cant understand why this is happening as Arrays.asList(arrayname) returns a list of finite size so when we iterate on that list there should be four iterations but my output is iterating in an infinite loop . Can anyone explain .
ankur trapasiya
Ranch Hand

Joined: Sep 24, 2010
Posts: 160

here in this code you are not calling it.next()... that's why it is causing infinite loop


the main purpose of next method is to return the current value in the collection and move the internal pointer to the next element in the collection.. Here you are not calling next() that's why iteration never moves to the next element and it.hasNext() always returns true..
Therefore loop never ends....

i hope you got it


OCPJP(83%)
Sumit Khurana
Ranch Hand

Joined: Sep 19, 2010
Posts: 68

the main purpose of next method is to return the current value in the collection and move the internal pointer to the next element in the collection.. Here you are not calling next() that's why iteration never moves to the next element and it.hasNext() always returns true..


The next method returns the next object in the collection,and moves you forward to the element you just returned.

hasNext() method returns true if there is an element present after the current element.

so,in this loop,in each iteration you are checking the same object and not moving to the next object.
Abimaran Kugathasan
Ranch Hand

Joined: Nov 04, 2009
Posts: 2066

You should move the pointer in the iteration.


|BSc in Electronic Eng| |SCJP 6.0 91%| |SCWCD 5 92%|
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Infinte iteration in a list