简体   繁体   中英

Why i have error when write buble sort in ArrayList?

I have ArrayList with some words. I need to sort words by way of repetition. Thanks.

ArrayList<String> words = new ArrayList<String>();
            words.addAll(Arrays.asList("returns", "such", "containing", "created", "maintain", "such", "discipline", "specified", "these", "such", "which", "such", "larger",
                    "deceptively", "difficult", "returns", "element", "care", "returns", "collection"));
        int i = 0, j = 0;
        String sortTemp;
        for (j = 0; j < words.size(); j++) {
            for (i = 0; i < words.size()-j; i++) {
                if (words.get(i).compareTo(words.get(i+1))>0) {
                    sortTemp = words.get(i);
                    words.set(i, words.get(i+1));
                    words.set(i+1, sortTemp);
                }
            }
        }
        for(i=0;i<words.size(); i++) {
            Log.e("Sorted Array", words.get(i));
        }

01-13 06:09:31.065 740-740/? E/AndroidRuntime: FATAL EXCEPTION: main Process: com.egoriku.task4_02, PID: 740 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.egoriku.task4_02/com.egoriku.task4_02.MainActivity}: java.lang.IndexOutOfBoundsException: Invalid index 20, size is 20 at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2184) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.Nativ eStart.main(Native Method) Caused by: java.lang.IndexOutOfBoundsException: Invalid index 20, size is 20 at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:255) at java.util.ArrayList.get(ArrayList.java:308) at com.egoriku.task4_02.WorkWithArrayList.TaskThreeSortArrayList(MainActivity.java:51) at com.egoriku.task4_02.MainActivity.onCreate(MainActivity.java:89) at android.app.Activity.performCreate(Activity.java:5231) at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087) at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2148) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2233) at android.app.ActivityThread.access$800(ActivityThread.java:135) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) at android.os.Handler.dispatchMessage(Handler.java:102) at android.os.Looper.loop(Looper.java:136) at android.app.ActivityThread.main(ActivityThread.java:5001) at java.lang.reflect.Met hod.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:515) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601) at dalvik.system.NativeStart.main(Native Method)

There is no need to implement bubble-sort. You can just use the sort method

ArrayList<String> words = new ArrayList<String>();
words.addAll(Arrays.asList("returns", "such", "containing", "created", "maintain", "such", "discipline", "specified", "these", "such", "which", "such", "larger",
                "deceptively", "difficult", "returns", "element", "care", "returns", "collection"));
Collections.sort(words):

After this code fragment your ArrayList will be sorted

As for the error you are getting, you can clearly see that it is an IndexOutOfBounds Exception, which means the index of your arraylist elements is greater than it's size

for(int i = 0; i < words.size(); i++) {
    for(int j = 0; j < words.size()-i-1; j++) { // you make the size smaller here
        if(words.get(j).compareTo(words.get(j+1)) > 0) {
            //....
        }
    }
}

Try this,

 ArrayList<String> words = new ArrayList<String>();
        words.addAll(Arrays.asList("returns", "such", "containing", "created", "maintain", "such", "discipline", "specified", "these", "such", "which", "such", "larger",
                "deceptively", "difficult", "returns", "element", "care", "returns", "collection"));
        String tempString;
        for(int i = 0; i < words.size(); i++) {
            for(int j = 0; j < words.size() - 1 -i; j++) {
                if(words.get(j).compareTo(words.get(j+1)) > 0) {
                    tempString = words.get(j);
                    words.set(j,words.get(j+1));
                    words.set(j+1,tempString);
                }
            }
        }

        for (int index = 0 ; index < words.size(); index ++){
            Log.d("MainActivity","element @ "+index+" : "+words.get(index));
        }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM