package Medium;
import java.util.ArrayList;
import java.util.Scanner;
public class Demo5 {
public static void main(String[] args) {
System.out.println("input end when you want to stop:");
ArrayList<Double> arr = new ArrayList<Double>();
while(true){
Scanner in = new Scanner(System.in);
//String d = in.nextLine();
double d = in.nextDouble();
arr.add(d);
if (d==0) {
break;
}
}
Double[] array =(Double[]) arr.toArray(); //I hava already change the type to double
int outter,inner,max;
for(outter=0;outter<array.length-1;outter++){
max = outter;
for(inner=outter+1;inner<array.length;inner++){
if (array[inner]>array[max]) {
inner = max;
}
}
double temp =array[outter];
array[outter] =array[inner];
array[inner]=temp;
}
System.out.println(array);
}
}
Description:Find K-th largest element in an array.
I want to input a arrayList and change it into array,then use selectSort.However,prints out ClassCastException
on"Double[] array =(Double[]) arr.toArray();"
,what's wrong?thank you for your time.
Exception:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Double;
at Medium.Demo5.main(Demo5.java:31)