java - Why Can't I Sort this ArrayList? -
i copying off of illustration textbook , yet refuses compile. did create typo somewhere? reason, on client code, collections.sort(words) not allow programme compile. help appreciated. code copied "building java programs" 2nd edition stuart reges , marty stepp. i'm trying understand copying it.
the programme supposed crate calendardate object set arraylist. implementing comparable interface calendardate, can utilize collections.sort sort birthdays in order in arraylist. however, that's not working b/c collections.sort(dates) not run.
the client code(contains issue):
import java.util.*; // short programme creates list of birthdays of // first 5 u.s. presidents , puts them sorted order. // can utilize collections.sort arraylist<calendardate> b/c calendardate implements comparable interface. public class calendardatetest { public static void main(string[] args) { arraylist<calendardate> dates = new arraylist<calendardate>(); // creates new arraylist of 'calendardate' object type. // adds new calendardate object month = 2 , day = 22 element of arraylist dates, , etc. dates.add(new calendardate(2, 22)); // washington dates.add(new calendardate(10, 30)); //adams dates.add(new calendardate(4, 13)); // jefferson dates.add(new calendardate(3, 16)); // madison dates.add(new calendardate(4, 28)); // monroe system.out.println("birthdays = " + dates); // before sorting collections.sort(dates); // why won't work? system.out.println("birthdays = " + dates); // after sorting } }
the calendardate object class:
public class calendardate implements comparable<calendardate> { private int month; private int day; // constructor public calendardate(int month, int day) { this.month = month; this.day = day; } // compares calendar date date // dates compared month , day public int compareto(calendardate other) { if (month != other.month) { // if different months homecoming month - other.month; //negative, positive, or 0 } else { // if same months; compare days instead homecoming day - other.day; // negative, positive, or 0 } } // accessor month (b/c month private) public int getmonth() { homecoming this.month; } // accessor day (b/c day private) public int getday() { homecoming this.day; } // tostring method public string tostring() { homecoming month + "/" + day; } }
the comparable interface:
public interface comparable<t> { // t generic type (a placeholder when other classes implement this) public int compareto(t other); // placeholder implemented; need more specific version class implementing this. }
compiler error:
exception in thread "main" java.lang.error: unresolved compilation problem: bound mismatch: generic method sort(list<t>) of type collections not applicable arguments (arraylist<calendardate>). inferred type calendardate not valid substitute bounded parameter <t extends comparable<? super t>> @ calendardateexample.calendardatetest.main(calendardatetest.java:21)
don't define own comparable
interface. need implement
java.lang.comparable
.
java sorting arraylist interface comparable
No comments:
Post a Comment