Java is showing the error on lines 11, and 13. Incompatible types: E[] cannot be converted to int.


package holdClasses;

public class sortMethods<E> {
    public static <E extends Comparable<E>> void mergeSort(E[]list){
        if (list.length > 1){
            E[] firstHalf = (E[]) new Object[list.length / 2];
            System.arraycopy(list, 0, firstHalf, 0, list.length /2);
            mergeSort(firstHalf);
            
            E[] secondHalfLength = (E[]) new Object[list.length - list.length / 2];
            E[] secondHalf = new [secondHalfLength];
            System.arraycopy(list, list.length / 2,
                    secondHalf, 0, secondHalfLength);
            mergeSort(secondHalf);
        }
    }

I tried converting it to an Object because you cannot create a generic array in java but converting it to the Object created this new problem where it says its being converted into an integer.

0

There are 0 best solutions below