Tehreem Tehreem - 1 month ago 4x
Java Question

Java: How to get address of array?

I am trying to convert a C++ code to Java.
The C++ code snippet is:

findMedian(array+left+i*5, 5);

The parameter passed is the array address which changes according to the value of i. How can I modify this code so that it works in Java?The method is as below:

public int findMedian(int arr[], int n)
return arr[n/2]; // Return middle element


You can use the other overload of Arrays.sort to sort only part of the array:

public static int findMedian(int arr[], int from, int n) {
    Arrays.sort(arr, from, from + n);
    return arr[from + (n / 2)]; // Return middle element

Then call like:

findMedian(array, left + i * 5, 5);