java - 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;
}
}
}
解决方案
假设您指的是您的自定义类Interval
,您可以简单地覆盖它的toString()
方法来打印调试信息,例如您的实例变量开始/结束:
public String toString() {
return "Start : " + start + " -- End : " + end;
}
推荐阅读
- javascript - 当前时间和当前时间 + 10 分钟数
- leaflet - Leaflet geoman setLatLngs 未将对象恢复到 getLatLngs 位置
- mongodb - MongoDB中具有不同计数的双重聚合
- python - 在 PyCharm 中安装 python-snappy 时出错
- c# - 为生成的按钮 WinForms 添加 _Click 功能
- reactjs - 带有导入的 React 组件的无效挂钩调用
- regex - 名称字段中允许的字符的正则表达式
- angular - 角度预选选择选项
- excel - 删除循环中的隐藏行仅在第一次运行时有效
- soap - SOAP asciihex 编码