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);
}
}
Beschreibung: Finden Sie K-ten größten Element in einem Array. Ich möchte eine ArrayList eingeben und ändern sie in Array, dann verwenden Sie SelectSort.Ausgewählt jedoch ClassCastException
auf "Double[] array =(Double[]) arr.toArray();"
, was ist falsch? Vielen Dank für Ihre Zeit.ein einfacher mit Java kalasCastException
Ausnahme:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Double;
at Medium.Demo5.main(Demo5.java:31)
'Objekt []! = Double []' –
danke..so, wie genau sollte ich es geändert haben? – Lucy
Es gibt eine sehr gute Antwort hier in diesem Link: http://stackoverflow.com/questions/5690351/java-stringlist-toarray-gives-classcastexception –