How to cast java Object to java Object[] or Integer[] Error(I cannot be cast to [Ljava.lang.Object;);
public class LongestAscendingSumSequence {
public static void main(String[] ar) {
int[] arr = { 1, 2, 3, 20, 3, 25, 30, 31, 32, 33, 34 };
System.out.println(getSum(arr));
float[] brr = { 1.3f, 2.3f };
System.out.println(getSum(brr));
int[] crr = { 1, 2, 3, 20, 3, 25, 30, 31, 32, 33, 34, -2 };
System.out.println(getSum(crr));
}
public static String getSum(Object obj) {
Object[] objects = (Object[]) obj;
System.out.println(objects.length);
return null;
}
}
Getting error for this
Exception in thread "main" java.lang.ClassCastException: [I cannot be cast to [Ljava.lang.Object;
at jan25.LongestAscendingSumSequence.getSum(LongestAscendingSumSequence.java:16)
at jan25.LongestAscendingSumSequence.main(LongestAscendingSumSequence.java:7)
ìnt[]is not anObject[], thus the exception. Even covariance does not help in this case. If you were usingInteger[]instead ofint[], it would be working.Arrays.sorthave a bunch of overloads for primitive arrays.