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 5 of 5

Thread: Debugging with Print Statement (on List of Object Class)

  1. #1
    Member
    Join Date
    Oct 2021
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Debugging with Print Statement (on List of Object Class)

    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]]

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,792
    Thanks
    57
    Thanked 2,687 Times in 2,637 Posts

    Default Re: Debugging with Print Statement (on List of Object Class)

    What you see printed: Interval.Interval@7de26db8 is the String returned by the Object class's toString method.
    If you want to see something different, add a toString method to the Interval class and have it return the desired String.
    See the API doc for the Object class's toString method for more details.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Oct 2021
    Posts
    42
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Debugging with Print Statement (on List of Object Class)

    Alright, understood.

    Also, I have a quick question about the behavior of the following line - You can find this line inside the first for-each loop of the program.
    ll.add(new Interval(temp[0], temp[1]));

    With the following entry :
    int[][] intervalNumbers = { { 1, 3 }, { 2, 6 }, { 8, 10 }, { 15, 18 } };

    Does that mean that temp[0] and temp[1] are being incremented by one each time via the loop?
    What do index 0 and index 1 mean for the intervals array here?

    If the entry was
    int[][] intervalNumbers = { { 1, 3, 11 }, { 2, 6, 8}, { 8, 10, 9 }, { 15, 18, 10 } };

    How would we modify that line?

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    24,792
    Thanks
    57
    Thanked 2,687 Times in 2,637 Posts

    Default Re: Debugging with Print Statement (on List of Object Class)

    Does that mean that temp[0] and temp[1] are being incremented by one each time via the loop?
    No, there values would not be changed.

    What do index 0 and index 1 mean for the intervals array here?
    0 references the first element and 1 references the second element

    How would we modify that line?
    I do not understand the question.
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Jul 2019
    Posts
    36
    Thanks
    2
    Thanked 4 Times in 4 Posts

    Default Re: Debugging with Print Statement (on List of Object Class)

    int[][] intervalNumbers = { { 1, 3 }, { 2, 6 }, { 8, 10 } };
     
    for (int[] temp : intervals) { 
      ll.add(new Interval(temp[0], temp[1]));
     
    means, get inner arrays in loop:
      temp =  { 1, 3 }
      temp =  { 2, 6 }
      temp =  { 8, 10 }
     
    and add objects with (this numbers as parameters)
      Interval( 1, 3 ) 
      Interval( 2, 6 ) 
      Interval( 8, 10 )

  6. The Following User Says Thank You to zemiak For This Useful Post:

    siid14 (June 27th, 2022)

Similar Threads

  1. How to print out class parameters properly
    By SamJava_the_Hut in forum Object Oriented Programming
    Replies: 2
    Last Post: August 29th, 2019, 07:13 PM
  2. Cannot print boolean value from class!
    By adnan.alvee in forum What's Wrong With My Code?
    Replies: 8
    Last Post: March 15th, 2013, 06:12 AM
  3. [SOLVED] Coin Loop Selective Print Statement Problem
    By sternfox in forum Loops & Control Statements
    Replies: 1
    Last Post: January 30th, 2013, 12:30 AM
  4. How do I print item facilities in another class?
    By dunnage888 in forum What's Wrong With My Code?
    Replies: 0
    Last Post: November 22nd, 2012, 10:43 AM
  5. Replies: 2
    Last Post: November 15th, 2012, 02:58 PM

Tags for this Thread