C S 314 Lecture Notes - Lecture 6: Parsing, Coffee Filter, Carl Linnaeus
Document Summary
The java collection interface (in package java. util) provides a common way for various kinds of collections to be used. This may make it possible to change the kind of collection used by an application. The toarray method allows any collection to be converted easily to an array. Two iterator patterns can be used with collections. The first is a simple iteration through all elements: for ( anytype item : coll ) The second form allows more complicated processing, including removal of an item. Iterator itr = coll. iterator(); while ( itr. hasnext() ) { anytype item = itr. next(); // process item if ( ) itr. remove(); } In this pattern, an iterator object itr is created. This object can be queried to test whether any items remain using itr. hasnext(). If there are objects, itr. next() gets the next one. itr. remove()removes the last object returned by itr. next() and can only be used once until there is another call to itr. next() .