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);
}
}
説明配列内のK番目に大きい要素を検索します。 arrayListを入力して配列に変更し、selectSort.Howeverを使用して、ClassCastException
を"Double[] array =(Double[]) arr.toArray();"
に出力しています。間違っていますか?ありがとうございます。java calassCastExceptionを持つ単純なもの
例外:
Exception in thread "main" java.lang.ClassCastException: [Ljava.lang.Object; cannot be cast to [Ljava.lang.Double;
at Medium.Demo5.main(Demo5.java:31)
'Object []!= Double []' –
ありがとうございました。どのように正確に変更する必要がありますか? – Lucy
このリンクには非常に良い答えがあります:http://stackoverflow.com/questions/5690351/java-stringlist-toarray-gives-classcastexception –