0

When compiling this below code I'm receiving this output. I'm not sure where I'm making the mistake as the output should be the content of the array rather than its address. Appreciate any help!!

import java.util.*;
public class insert_intervals{
    public static void main(String[] args) {
        int[][] intervals = {{1, 3} , {6 , 9}};
        int[] newInterval = {2, 5};
        List<int[]> ans = new ArrayList<>();
        int newBegin = newInterval[0];
        int newEnd = newInterval[1];

        for(int[] interval : intervals){
            int currentBegin = interval[0];
            int currentEnd = interval[1];

            if((currentBegin < newBegin && currentEnd < newEnd) || (currentBegin > newBegin && currentEnd > newEnd)){
                ans.add(interval);
            }else if(currentEnd >= newBegin){
                newBegin = Math.min(currentBegin, newBegin);
                newEnd = Math.max(currentEnd, newEnd);
                int[] arr = {newBegin, newEnd};
                ans.add(arr);
            }
        }
        for(int[] ar : ans){
            System.out.println(ar);
        }
    }
}

Output : [I@2c7b84de [I@3fee733d

I tried to get the elements of the array using for each loop but it provides me the address of the array. I have tried in online compilers as well and receiving similar output.

Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
  • This is not an address, it is the the type identifier (`[I` for an `int[]`, followed by @, followed by an identity hashcode. In theory an identity hashcode could be derived from a memory address, but in practice, Java garbage collectors move objects around in memory, so using a memory address wouldn't make sense (it either wouldn't be stable, or if the initial location was used, it would easily lead to duplicate identity hashcodes). – Mark Rotteveel Mar 18 '23 at 08:13

0 Answers0