Wednesday, February 29, 2012

1.10

An index based loop is limited in its use with an array in that extra work must be done to ensure it does not attempt to access data outside bounds of the array. This can be accomplished by using a variable which is updated alongside the array which tracks its length(for a dynamic array) or a constant which specifies its length(static array). This makes an index based loop highly sensitive to changes in the array. In contrast, an iterator should include checks to ensure it does not leave the bounds of the array(such as .hasNext). An iterator is overall more predictable than an index based loop.

No comments:

Post a Comment