//ArrayListofDates.java //example of user-defined objects in an ArrayList. //create list of random Dates. iterate over them. sort them. /* An Iterator's next() method returns a reference of the built-in class Object. If you want to use that reference to access a member of your class, you must cast that return value to your class: Date thisDate = (Date)(it.next()); then you can access methods of the Date object: thisDate.getMonth() Failure to cast will be an "incompatible types" compile error: //Date thisDate = it.next(); //invalid statement Directly trying to use the it.next() to access a Date method is a "cannot find symbol method" compile error: //it.next().getMonth(); //invalid statement as the Object class does not have a getMonth member. How can System.out.println be given an Object reference: System.out.println(it.next()); and the Date toString method is called? Object is the superclass of all other classes. Date, for example, is a subclass of Object, it extends Object automatically, without any need of the extends clause. Object is the ancestor of all classes, and so all classes inherit its methods such as toString. println is (indirectly) calling the toString method of Object but if a class has overridden the toString method inherited from Object and the reference is actually to an object of that class, then the overridden method is called. This is the deep magic of polymorphism: a superclass reference like it.next() can be used to access subclass methods. More on this later. */ import java.util.*; import javax.swing.*; public class ArrayListofDates { public static void main(String[] args) { String input; int size; ArrayList myDatesList = new ArrayList(); input = JOptionPane.showInputDialog( "Enter number of random dates" ); size = Integer.parseInt( input ); //**array list of random Dates for (int i=1; i<=size; i++) { myDatesList.add(generateRandomDate()); //**pseudo data... } //iterate over them Iterator it=myDatesList.iterator(); System.out.println(); System.out.println("Here are your dates: "); Date thisDate; while (it.hasNext()) { //reference to the Date in the list. //***next() returns Object, must cast to Date: thisDate = (Date)(it.next()); //thisDate = (it.next()); //**failure to cast would be compile error System.out.print(thisDate); //***access a method of the Date object: System.out.println(" Month is: " + thisDate.getMonth()); } System.out.println(); //what does sorting do to Dates? //Collections.sort(myDatesList); //***would be runtime error. //***there is no "natural ordering" for user-defined classes. //***must specify how dates are compared with a Comparator. //DateComparator is defined in another file. Collections.sort(myDatesList,new DateComparator()); //*** it = myDatesList.iterator(); System.out.println("Here are your dates in sorted order: "); while (it.hasNext()) { System.out.println(it.next()); } System.out.println(); //***now sort by julian day using a different comparator //DateComparatorJulian is defined in another file. Collections.sort(myDatesList,new DateComparatorJulian()); //*** it = myDatesList.iterator(); System.out.println("Here are your dates in julian sorted order: "); while (it.hasNext()) { System.out.println(it.next()); } System.out.println(); //min and max using Comparator System.out.println("Min value in array list: " + Collections.min(myDatesList, new DateComparator())); System.out.println("Max value in array list: " + Collections.max(myDatesList, new DateComparator())); System.exit(0); } //generate a random Date. Note this is not a method of the Date class. //it creates and returns a Date object. public static Date generateRandomDate() { int year, month, day=0; year = (int)(Math.random()*20+1995); // +/- 10 years from 2005 month = (int)(Math.random()*12+1); switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: day = (int)(Math.random()*31+1); break; case 4: case 6: case 9: case 11: day = (int)(Math.random()*30+1); break; case 2: if (year%4==0 && year%100!=0 || year%400==0) //leap year test day = (int)(Math.random()*29+1); else day = (int)(Math.random()*28+1); break; } //instantiate a Date object using the 3-arg constructor, //then return the object to caller. return new Date(month,day,year); } }