The returned iterator implements the standard

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: be used to read the objects that are stored by an ArrayList-based array. The returned iterator implements the standard interface Iterator. Iterators can be used instead of index variables when ArrayList objects are processed. For example, the loop inside method print_array() of program could be rewritten by using an iterator in the following way: Iterator element_to_print = given_array.iterator() ; while ( element_to_print.hasNext() == true ) { System.out.printf( "%5s", ) ; } An iterator is a kind of pointer or a special reference to the objects of an array. In the above loop, the iterator element_to_print points to the objects of an ArrayList-based array. You can think that after the creation of the iterator, it points to a position that is one position behind the first object in the array. When the Iterator method next() is called, the iterator is advanced to the next object in the array, and a reference to the object is returned. The first call to next() makes the iterator point to the first object of the array. With the Iterator method hasNext() it is possible to check whether the array has more elements, i.e., whether a call to the next() method will be successful. By comparing the above loop to the corresponding loop in program, you can see that using an iterator can simplify loops, or at least make a loop shorter. Instead of iterators, however, it is better to use "foreach" loops which are shorter than traditional loops. The above program lines can be replaced, for example, with the following "foreach" loop: for ( Object element_in_array : given_array ) { System.out.printf( "%5s", element_in_array ) ; } Invented by Samuel Morse in the U.S. in 1844, Morse codes were the first widely-used method for transmitting textual information. Each letter of the alphabet is coded with a sequence of signals. A signal can be either short or long. If two communicating parties know the Morse codes, they can communicate, for example, with a flashlight. To transmit letter L, for example, you first show the light for a short time, then once for a longer time, and finally you show it twice for a shorter time. Before telephones and computers became popular, Morse codes were widely used to send textual messages through electric lines and radio waves. Although these codes have less importance these days, they remain an important invention in the history of information processing. (The "code" for the space character is my invention in this program.) sedoCesroM avaj>3selifavaj\:D nenitiaL iraK :eman ruoy ni epyT :si sedoc esroM ni eman ruoY .- . .- .. - .. -. ..-. .. .-. -. -.- - X. The string "Kari Laitinen" written with Morse codes. 15.1 ArrayList class // With the static method addAll() of class Collections, all elements of a conventional Java array are added to the end of an empty ArrayList array. The array referenced by array_of_morse_codes is an array of strings, and...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online