如何对数组列表进行排序?

Ben*_*min 0 java arraylist

大家好。感谢对我的障碍的主要帮助。这次我的问题是,我如何对代码中提供的数组列表进行排序基本上不知道我需要在下面提供的代码中添加什么,只是为了简单五分我得到了 3 个数组列表,我想将它们合并为一个数组列表,所以他们可以在猜测和尝试中排序(如果两个玩家有相同的猜测,那么时间应该确定)。很难解释它,但我尽力了......最好的办法是运行它然后你会弄清楚问题是什么?

这是代码:

    import java.util.*;
import java.util.Scanner.*;
import java.util.ArrayList.*;


public class Main {

public static String ToString(int a, double b, String c)
    {
        String aS = Integer.toString(a);
        String bS = Double.toString(b);
                String scoreList = (aS + "\t\t" + bS + "\t\t" + c);

        return scoreList;
    }

private static void start() {


 int tries = 0 ;

 int guess = -1;
 String name ;
 String quit = "quit";
 String y = "yes";
 String n = "no";
 String currentGuess;


 String another = ("y") ;
 Scanner input = new Scanner (System.in);

 ArrayList<String> scores = new ArrayList<String>();
    boolean a=false;

  do {
      a=false;
     int answer = (int) (Math.random() * 1000 + 1) ;
    System.out.println( " Welcome to Guessing Game " )  ;
    System.out.print("Please enter a number between 1 and 1000 : ");
                    currentGuess = input.nextLine();
            long startTime = System.currentTimeMillis();





      do
      {


               if (currentGuess.equalsIgnoreCase(quit))
        {
            System.out.println("Leaving Us So Soon?");
            System.exit(0);
        }

               try    {
            guess = Integer.parseInt(currentGuess);
              } catch (NumberFormatException nfe) 
                        {
            System.out.println(" Dude Can You Read, Only Digits ");
                        currentGuess = input.nextLine();


            }

        if (guess < 1 || guess > 1000)
        {
            System.out.println("Stupid Guess I Wont Count That.");
                        currentGuess = input.nextLine();

        }
       if (guess < answer )
          {
            System.out.println("too low "+answer);
            currentGuess = input.nextLine();
                        tries++;
        }


    else if(guess  > answer )
        {
            System.out.println("too high "+answer);
            currentGuess = input.nextLine();
                        tries++;
        }


    else if (guess == answer)
        {       
            //stop stop watch
            long endTime = System.currentTimeMillis();
            //calculate game time
            long gameTime = endTime - startTime;
            System.out.println("You Rock Dude, Good Job!");

                        System.out.println("You guessed " + tries + " times in " + (int)(gameTime/1000) + " seconds.");
                        System.out.println("Please enter your name.");

                  name = input.nextLine();
                          //create score object
             String TOString =ToString(tries, gameTime, name);
            //add score to arrayList
            scores.add(TOString);

             boolean s = false  ; 
             while (s==false)
              {


                     System.out.print("Want to go again?(y/n).....");
                     currentGuess = input.nextLine();
                      if (currentGuess.matches("y"))
            {
                     System.out.println("HighScore: \nGuess \t  Time in milisec\tName");
                //print out high score list
                for (int i = 0;i < scores.size(); i++)
                {
                System.out.println(scores.get(i));
                }
//                                 Main.start
                     s=true;

            }

                     //if user doesn't want to play again

                             if (currentGuess.matches("n"))
            {
                System.out.println("HighScore:\nGuess \tTime in milisec\tName");
                //print out high score list
                for (int i = 0;i < scores.size(); i++)
                {
                System.out.println(scores.get(i));
                }
                                System.out.println("Thanx For Playing.");
                a=true;

                                s=true;
                                 System.exit(0);  
                    } 
       }
          }

        } while (guess != answer);

  }while(a==false);

}

public static void main(String[] args) {
//   ArrayList<Score> scores = new ArrayList<Score>();
    Main.start();
    }
}
Run Code Online (Sandbox Code Playgroud)

vir*_*m03 5

使用Collections.sort()并确保您的类实现Comparable或使用以下内容:

Collections.sort(list, new Comparator<T>() {

        public int compare(T o1, T o2) {
            return o1.compareTo(o2);
        }});
Run Code Online (Sandbox Code Playgroud)