I'm trying to debug a program with print statement. However, when I try to print the LinkedList of the program, it does not return the value. Instead, it does return me the memory address of those values I believe. How to fix it, in a way I can see the actual value. Note : I'm trying to print list containing multiple object of Interval Class.
Here is my code :
Interval Class
package Interval; public class Interval { public int start; public int end; public Interval(int s, int e) { this.start = s; this.end = e; } }
App (main) Class
import java.util.*; import Interval.Interval; public class App { public static int[][] merge(int[][] intervals) { if (intervals.length <= 1) { // if only one interval return intervals; } // add intervals to linked list LinkedList<Interval> ll = new LinkedList<>(); for (int[] temp : intervals) { ll.add(new Interval(temp[0], temp[1])); // add each interval object with each interval from intervals array System.out.println("Adding new Interval object to list ll : "); System.out.println(ll); } System.out.println(); // sorting list on start time Collections.sort(ll, new Comparator<Interval>() { public int compare(Interval a, Interval b) { return a.start - b.start; } }); System.out.println("List ll after sorting: "); System.out.println(ll); System.out.println(); LinkedList<Interval> result = new LinkedList<>(); for (Interval curr : ll) { if (result.isEmpty() || result.getLast().end < curr.start) { result.add(curr); System.out.println(result); System.out.println(); } else { result.getLast().end = Math.max(curr.end, result.getLast().end); System.out.println(result); } } int[][] res = new int[result.size()][2]; int count = 0; for (Interval temp : result) { res[count][0] = temp.start; res[count][1] = temp.end; count++; } return res; } public static void main(String[] args) throws Exception { int[][] intervalNumbers = { { 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } }; int[][] intervalNumbers2 = { { 1, 3 } }; System.out.println(Arrays.deepToString(merge(intervalNumbers))); // System.out.println(Arrays.deepToString(merge(intervalNumbers2))); } }
Here is the output I got when I try to debug it.
Adding new Interval object to list ll : [Interval.Interval@7de26db8] Adding new Interval object to list ll : [Interval.Interval@7de26db8, Interval.Interval@1175e2db] Adding new Interval object to list ll : [Interval.Interval@7de26db8, Interval.Interval@1175e2db, Interval.Interval@36aa7bc2] Adding new Interval object to list ll : [Interval.Interval@7de26db8, Interval.Interval@1175e2db, Interval.Interval@36aa7bc2, Interval.Interval@76ccd017] List ll after sorting: [Interval.Interval@7de26db8, Interval.Interval@1175e2db, Interval.Interval@36aa7bc2, Interval.Interval@76ccd017] [Interval.Interval@7de26db8] [Interval.Interval@7de26db8] [Interval.Interval@7de26db8, Interval.Interval@36aa7bc2] [Interval.Interval@7de26db8, Interval.Interval@36aa7bc2, Interval.Interval@76ccd017] [[1, 6], [8, 10], [15, 18]]