I'm doing the following exercise: Write a Java program to merge all overlapping Intervals from a given collection of intervals.
I'm not getting my array printed as I want too. It gives me the address of memory instead I think.
Here is my code :
import java.util.*; import java.util.LinkedList; public class overlappingIntervals { public static class Interval { int start; int end; Interval(int s, int e) { this.start = s; this.end = e; } } public static int[][] merge(int[][] intervals) { if (intervals.length <= 1) { return intervals; } LinkedList<Interval> ll = new LinkedList<>(); for (int[] temp : intervals) { ll.add(new Interval(temp[0], temp[1])); // got the error here } Collections.sort(ll, new Comparator<Interval>() { public int compare(Interval a, Interval b) { return a.start - b.start; } }); LinkedList<Interval> result = new LinkedList<>(); for (Interval curr : ll) { if (result.isEmpty() || result.getLast().end < curr.start) { result.add(curr); } else { result.getLast().end = Math.max(curr.end, result.getLast().end); } } 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) { int[][] intervalNumbers = { { 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } }; System.out.println(Arrays.toString(merge(intervalNumbers))); // it does print [[I@156643d4, [I@123a439b, [I@7de26db8] - I don't want that, I want the actual value. } }
How to fix this?