// IterableArrayDemo.java (c) Kari Laitinen // http://www.naturalprogramming.com // 2017-02-24 File created. // 2017-02-24 Last modification. /* This program ís a modified version of the example program named HistoricalEvents.java In this program the used arrays are of type IterableArray, which is defined in this file. Objects of IterableArray contain a built-in iteration mechanism. I wrote this after I started to wonder why we have such things as separate iterators, and why they are not built-in in such classes as the standard ArrayList class. */ import java.time.LocalDate ; import java.util.* ; class IterableArray extends ArrayList { Iterator iterator_for_this_array = null ; // The following method must be called always before a new iteration loop. public void setIterable() { iterator_for_this_array = iterator() ; } public boolean hasNext() { return iterator_for_this_array.hasNext() ; } public E next() { return iterator_for_this_array.next() ; } } // Objects of class Event are used to test the IterableArray class. class Event implements Comparable { protected LocalDate date_of_event ; protected String event_description ; public Event( int year_of_event, int month_of_event, int day_of_event, String given_event_description ) { date_of_event = LocalDate.of( year_of_event, month_of_event, day_of_event ) ; event_description = given_event_description ; } public String toString() { return ( date_of_event + " " + event_description ) ; } public int compareTo( Event event_to_compare_to ) { int comparison_result = 0 ; // Events of the same date. if ( date_of_event.isBefore( event_to_compare_to.date_of_event ) ) { comparison_result = -1 ; // "this" has earlier date. } else if ( date_of_event.isAfter( event_to_compare_to.date_of_event ) ) { comparison_result = 1 ; // "this" has later date. } return comparison_result ; } } class IterableArrayDemo { public static void main( String[] not_in_use ) { Event birth_of_lennon = new Event( 1940, 10, 9, "John Lennon was born.") ; Event birth_of_einstein = new Event( 1879, 3, 14, "Albert Einstein was born." ) ; IterableArray list_of_events = new IterableArray() ; list_of_events.add( birth_of_lennon ) ; list_of_events.add( birth_of_einstein ) ; list_of_events.add( new Event( 1980, 12, 8, "John Lennon was shot in New York." ) ) ; System.out.print( "\nEvents of list_of_events: \n" ) ; list_of_events.setIterable() ; while ( list_of_events.hasNext() ) { System.out.print( "\n " + list_of_events.next() ) ; } IterableArray another_event_list = new IterableArray() ; another_event_list.add( 0, new Event( 1926, 6, 1, "Marilyn Monroe was born." ) ) ; another_event_list.add( 0, new Event( 1962, 8, 5, "Marilyn Monroe died." ) ) ; another_event_list.add( new Event( 1769, 8, 15, "Napoleon Bonaparte was born." ) ) ; another_event_list.add( new Event( 1881, 10, 25, "Pablo Picasso was born." ) ) ; System.out.print( "\n\nEvents of another_event_list: \n" ) ; another_event_list.setIterable() ; while( another_event_list.hasNext() ) { System.out.print( "\n " + another_event_list.next() ); } list_of_events.sort( null ) ; another_event_list.sort( null ) ; list_of_events.addAll( 0, another_event_list ) ; System.out.print( "\n\nEvents of list_of_events: \n" ) ; list_of_events.setIterable() ; while ( list_of_events.hasNext() ) { System.out.print( "\n " + list_of_events.next() ) ; } System.out.print( "\n\n" ) ; } } /* // The following code was tested as well. IterableArray month_names = new IterableArray() ; Collections.addAll( month_names, "January", "February", "March", "April" ) ; System.out.print( "\n\nMonth names: \n" ) ; month_names.setIterable() ; while ( month_names.hasNext() ) { System.out.print( "\n " + month_names.next() ) ; } */