简体   繁体   中英

How would I get this debug call to print out the actual values of the object?

I understand that Arrays.sort() has a void return type, and that toArray() has return type is an object, so I did an override by passing in the object that I wanted to see.

How can I see the contents of my Time object?

Sorry if this is a duplicate.

/**
 * Definition of Interval:
 * public classs Interval {
 *     int start, end;
 *     Interval(int start, int end) {
 *         this.start = start;
 *         this.end = end;
 *     }
 * }
 */

public class Solution {
    /**
     * @param intervals: an array of meeting time intervals
     * @return: the minimum number of conference rooms required
     */
    public int minMeetingRooms(List<Interval> intervals) {
        // Write your code here
        if (intervals == null || intervals.size() == 0) {
            return  0;
        }

        Arrays.sort(intervals.toArray(new Interval[intervals.size()]), new StartComparator());
        int num = 1;
        for(int i = 0; i < intervals.size() - 1 ; i++) {
            System.out.print(intervals.get(i).toString());
            if(intervals.get(i).end >= intervals.get(i + 1).end) {
                num++;
            }
        }
        return num;
    }

    private class StartComparator implements Comparator<Interval> {
            @Override
            public int compare(Interval a, Interval b) {
                return a.start - b.start;
            }
        }
}

Assuming you are referring to your custom class named Interval , you could simply override its toString() method to print debugging info , for example your instance variables start / end :

public String toString() {
    return "Start : " + start + "  -- End : " + end;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM