#printing a 2d array in proper format

1 messages · Page 1 of 1 (latest)

turbid plover
#
    public static int[][] merge(int[][] intervals) {

        if (intervals.length <= 1) return intervals;
        Arrays.sort(intervals, Comparator.comparingInt(a -> a[0]));
        List<int[]> result = new ArrayList<>();

        int[] newInterval = intervals[0];
        result.add(newInterval);

        for (int[] interval : intervals) {
            if (interval[1] >= newInterval[0]) newInterval[1] = Math.max(interval[1], newInterval[1]);
            else {
                newInterval = interval;
                result.add(newInterval);
            }
        }

        return result.toArray(new int[result.size()][]);

    }

    public static void main(String[] args) {
        int[][] intervals = {{1, 3}, {2, 6}, {8, 10}, {15, 18}};
        System.out.println(merge(intervals));

    }

Hello, I am trying to test out my code / debug it a bit.. when I run it this is my output : [[I@24d46ca6 how do i avoid this and get a 2d array output?

burnt flintBOT
# turbid plover ```java public static int[][] merge(int[][] intervals) { if (interv...

Detected code, here are some useful tools:

Formatted code
public static int [] [] merge(int [] [] intervals) {
  if (intervals.length <= 1) return intervals;
  Arrays.sort(intervals, Comparator.comparingInt(a -> a[0] ));
  List < int []  > result = new ArrayList<>();
  int [] newInterval = intervals[0] ;
  result.add(newInterval);
  for (int [] interval : intervals) {
    if (interval[1]  >= newInterval[0] ) newInterval[1]  = Math.max(interval[1] , newInterval[1] );
    else {
      newInterval = interval;
      result.add(newInterval);
    }
  }
  return result.toArray(new int [result.size() ] [] );
}
public static void main(String[] args) {
  int [] [] intervals = {
      {
      1, 3}
    , {
      2, 6}
    , {
      8, 10}
    , {
      15, 18}
  };
  System.out.println(merge(intervals));
}
#

<@&987246399047479336> please have a look, thanks.

burnt flintBOT
#

While you are waiting for getting help, here are some tips to improve your experience:

Code is much easier to read if posted with syntax highlighting and proper formatting.

If nobody is calling back, that usually means that your question was not well asked and hence nobody feels confident enough answering. Try to use your time to elaborate, provide details, context, more code, examples and maybe some screenshots. With enough info, someone knows the answer for sure.

Don't forget to close your thread using the command </help-thread close:1027500463647621170> when your question has been answered, thanks.

turbid plover
#

I tried .toString()... but it doesnt let me since thats not the return type of my method

#

System.out.println(Arrays.toString(merge(intervals)));

also gives me the same weird output

quartz zenith
#

Since its an array that you're returning back, youll want to print the contents out.

public void printArray(int array[][]){
for(int x=0; x<array.length; x++){
for(int y=0; y<array[x].length; y++){
System.out.println(array[x][y]);
}
}
}

then you'll want to call this by doing

printArray(merge(intervals));

instead of the System.out.println(merge(intervals)) that you have

burnt flintBOT
turbid plover
#

ahhh i see

#

thank you!

quartz zenith
#

Youll want to do System.out.print(array[x][y]) then a System.out.println() after the nested loop is completed

#

that way you can see everything in a more 2d array looking way... the above code will print each number from left to right on its own line

smoky tusk
#

if you dont mind it looking kinda ugly there is also Arrays.deepToString(your2dArray)

#

you dont need any of ur own code for that