我需要对数组项进行排序并将旧索引存储在新数组中,所以我尝试了treemap
和集合,我按照我的期望获得了集合中的完整输出,但是当我将集合转换为数组时问题就出现了。
float[] array = new float[10];
array[0] = 11;
array[1] = 10;
array[2] = 3;
array[3] = 40;
array[4] = 5;
array[5] = 60;
array[6] = 4;
array[7] = 41;
array[8] = 110;
array[9] = 8;
Map<Float, Integer> map = new TreeMap<Float, Integer>();
for (int i = 0; i < array.length; ++i) {
map.put(array[i], i);
}
Collection<Integer> indices = map.values();
System.out.println("indices"+indices);
String s[] = (String[])indices.toArray(new String[0]);
for (int i = 0, n = s.length; i < n; i++)
{
System.out.println(s[i]);
}
会出现如下错误:
04-18 14:55:13.380: ERROR/AndroidRuntime(859): FATAL EXCEPTION: main
04-18 14:55:13.380: ERROR/AndroidRuntime(859): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jcord/com.jcord.Jcord_for_Con_lat_long_to_east_nothenActivity}: java.lang.ArrayStoreException
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.os.Looper.loop(Looper.java:123)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at java.lang.reflect.Method.invoke(Method.java:521)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at dalvik.system.NativeStart.main(Native Method)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): Caused by: java.lang.ArrayStoreException
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at java.util.AbstractCollection.toArray(AbstractCollection.java:357)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at com.jcord.Jcord_for_Con_lat_long_to_east_nothenActivity.onCreate(Jcord_for_Con_lat_long_to_east_nothenActivity.java:50)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-18 14:55:13.380: ERROR/AndroidRuntime(859): ... 11 more
答案 0 :(得分:2)
替换
String s[] = (String[])indices.toArray(new String[0]);
与
Integer s[] = (Integer[])indices.toArray(new Integer[0]);