Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 2 of 2

Thread: Printing Array

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Printing Array

    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?
    Last edited by siid14; June 17th, 2022 at 04:46 AM.

  2. #2
    Member
    Join Date
    Oct 2021
    Posts
    63
    Thanks
    10
    Thanked 0 Times in 0 Posts

    Default Re: Printing Array

    I correct
    System.out.println(Arrays.toString(merge(intervalNumbers)));

    to
    System.out.println(Arrays.deepToString(merge(intervalNumbers)));
    .
    And it work! Thanks to this resource : https://www.educba.com/print-array-in-java/

Similar Threads

  1. [SOLVED] Game Combat: Fighting one enemy at a time? (Issue with static/non-static?)
    By Rexoa in forum Java Theory & Questions
    Replies: 6
    Last Post: March 2nd, 2014, 07:34 AM
  2. topic related to static final variable and static block !!!!
    By Arnab Kundu in forum What's Wrong With My Code?
    Replies: 0
    Last Post: July 18th, 2013, 12:12 PM
  3. Replies: 6
    Last Post: May 3rd, 2013, 04:25 PM
  4. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM

Tags for this Thread