我正在尝试按ABC顺序对字符串的数组列表进行排序,而不使用预先制作的类

时间:2015-12-05 01:24:26

标签: java arrays arraylist

我无法按ABC顺序对数组列表进行排序。这就是我到目前为止,关于如何实现它的任何想法?我的输出是切换一些单词,但不是将整个数组列表按ABC顺序排列。我迷路了,你可以从最后的循环中看出= [

import java.util.ArrayList;
import java.util.Scanner;
public class ArrayListLab
{
   public static void main (String [] args)
   {
       Scanner scan = new Scanner (System.in);
       ArrayList <String> words = new ArrayList <String> ();
       for (int x = 0; x < 10; x++)
       {
           System.out.println("Pick a word, any word!");
           words.add(scan.nextLine());
        }
       for (int x = 0; x < 9; x++)
       {
         words.get(x).compareTo(words.get(x+1));
         if (words.get(x).compareTo(words.get(x+1))>0)
         {
            String temp = words.get(x+1);
            words.set((x+1),words.get(x));
            words.set(x, temp);
         }
        else
           if (words.get(x).compareTo(words.get(x+1))<0)
           {
              words.get(x);  
           }
           else
              if(words.get(x).compareTo(words.get(x+1))==0)
              {
                  words.get(x);
              }

    }
    System.out.println(words);
}
}

0 个答案:

没有答案