-2

public class Hello {

 static int[] findMinMax(int arr[], int n) {
     int min=arr[0];
     int max=arr[0];
     int [] temp = new int[2];
     for(int i=1; i<n; i++) {
         if(arr[i]<min)
             min = arr[i];
         else if(arr[i]>max)
             max = arr[i];
     }
       temp[0]= min;
       temp[1]= max;
       return temp;
 }
 
 public static void main(String[] args) {
     int[] arr = {22, 3, 5, 7, 9, 16};
     int[] result = findMinMax(arr, 6);
     for(int i=0; i<2; i++)
         System.out.print(result);
 }
   
}   

it gives output as [I@36baf30c[I@36baf30c

help me understand where I am wrong

Bill Hileman
  • 2,798
  • 2
  • 17
  • 24
  • System.out.print(result) will print the result object. Use System.out.print(result[i]); to use the contents. – Rajeev Sreedharan Feb 02 '21 at 08:26
  • 1
    Does this answer your question? [What's the simplest way to print a Java array?](https://stackoverflow.com/questions/409784/whats-the-simplest-way-to-print-a-java-array) – maloomeister Feb 02 '21 at 08:26

1 Answers1

0

You display array memory address, if you want to display particular array value just write: result[index]. Correct code below:

public class Hello {

static int[] findMinMax(int arr[], int n) {
    int min=arr[0];
    int max=arr[0];
    int [] temp = new int[2];
    for(int i=1; i<n; i++) {
        if(arr[i]<min)
            min = arr[i];
        else if(arr[i]>max)
            max = arr[i];
    }
    temp[0]= min;
    temp[1]= max;
    return temp;
}

public static void main(String[] args) {
    int[] arr = {22, 3, 5, 7, 9, 16};
    int[] result = findMinMax(arr, 6);
    for(int i=0; i<2; i++)
        System.out.println(result[i]);
}}
arch2be
  • 306
  • 2
  • 8