0

我需要对数组项进行排序并将旧索引存储在新数组中,所以我尝试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
4

1 回答 1

2

代替

    String s[] = (String[])indices.toArray(new String[0]);

    Integer s[] = (Integer[])indices.toArray(new Integer[0]);
于 2012-04-26T10:41:48.307 回答