Sunday, November 24, 2013

ArrayList printing Hashcode

When we try to print an ArrayList all at once we get

getClass().getName() + "@" + Integer.toHexString(hashCode())

In order to print the individual values, we need to change the toString() method in the class containing the setters/getters.

Add the following:

If your ArrayList is (snippets from my code):

ArrayList<Reservation> reservationsList = new ArrayList<Reservation>();
Reservation reservation;
reservation = new Reservation();

                // Does this work with recurring apps?
                reservation.setStartDate(new Date(cursor.getLong(3)));
                reservation.setEndDate(new Date(cursor.getLong(4)));

 //Add the overriden toString() method

    public String toString() {
        return ("Description:" + "\n" + this.getDescription() + "\n"
                + "Start Date:" + "\n" + this.getStartDate() + "\n"
                + "End Date:" + "\n" + this.getEndDate() + "\n"
                + "Location :" + "\n" + this.getLocation());

No comments:

Post a Comment