This code is probably completely wrong, but that's what I was able to do.
Message: "Be sure that the sort(Integer[] array) method correctly sorts the data in an array with an even number of elements."
When testing, this is the message of the Exception:

**Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Integer; at com.codegym.task.task26.task2601.Solution.sort(Solution.java:57) at com.codegym.task.task26.task2601.Solution.main(Solution.java:16)**Any help?package com.codegym.task.task26.task2601;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
/*
Read online about the median of a sample
*/
public class Solution {
public static void main(String[] args) {
}
public static Integer[] sort(Integer[] array) {
int totalElements = array.length;
double median;
double medianIndex;
int closestNumber = Integer.MAX_VALUE;
List<Integer> list = new ArrayList<>();
//sort array to obtain median
Arrays.sort(array);
//median calculation
if(totalElements % 2 != 0) { //if odd
median = (Integer) array[totalElements / 2];
medianIndex = (int) totalElements / 2;
}
else { //if even
int sumOfMiddleElements = array[totalElements / 2] + array[(totalElements / 2) - 1];
median = (Integer) sumOfMiddleElements / 2;
medianIndex = ((totalElements / 2) + (totalElements / 2) - 1) / 2;
}
//add median into list index 0
list.add((int) median);
//calculate distance between number and median, assign it to closestNumber
for(int i = 0; i < totalElements; i++) {
int number = (int) Array.get(array, i);
number -= median;
int absoluteNumber = Math.abs(number);
if(absoluteNumber < closestNumber) {
closestNumber = absoluteNumber;
} else if(absoluteNumber == closestNumber) { //if several numbers are equally distant, then sort them in ascending order.
if(number < closestNumber)
closestNumber = number;
}
//add number to list
list.add(closestNumber);
}
//convert list to array
Integer[] newArray = (Integer[]) list.toArray();
return newArray;
}
}