开发者

How to sort the array list?

Hey Guys. thanx for the major help regarding my obstacles. What my problem this time is, how can I sort the array list that is provided in my code basically dont know WhatI need to add in the provied code below, just to simplyfive it I got 3 arraylist that i want to make them into one arraylist, so they can be sorted in amont of guesses and tries( if 2 players have the same guesses then the time should determine) . Pretty hard to explain it but i tried my best.............the best thing is to run it then you will figure it what whats the issue is?

Here is the code:

    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

            开发者_Python百科                 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();
    }
}


Use Collections.sort() and make sure your class implements Comparable or use something like this:

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

        public int compare(T o1, T o2) {
            return o1.compareTo(o2);
        }});


You can use something like input.nextInt() to read in the next integer instead of the next String. I can see no reason why you are storing Strings instead of ints in your code. Then you can use what everyone else has suggested to combine the lists and/or sort them.

I also think you should do some more reading by yourself. Some of the questions you've asked in your comments are answered in the Java documentation. When someone posts a suggestion, at the very least look it up in the API and try to understand it. People won't be giving you all the code here, they will just give you hints that you need to follow.


To sort an ArrayList you can use the Collections.sort() method; either with only a List of Comparable elements or with a Comparator.

This is the simplest way.

In your case Collections.sort(scores) will do the thing.


To sort a list, use java.util.Collections.sort( list ).


To make one list out of three, you can use alist.addAll(anotherList). Then sort aList as suggested by Collin Hebert.


I helped to answer this in your previous post. However, I will add it here as well. I think you may want a List instead of a list of Strings. This will allow you to sort on the number of tries the end user has attempted, in a natural way. In addition to the other answers here, the simplest way is then to make a call to Collections.sort(list)

Additionally the toString() method is used for debugging purposes, or to provide human readable information. It shouldn't be used to create objects in the way that you are utilizing it. Just my .02


If you absolutely require a list to be sorted at all times, don't use an ArrayList, use a PriorityQueue. It can be constructed to use the content's natural sort order (i.e. via the Comparable interface on each object), or via a Comparator. The advantage of PriorityQueue is it is always sorted even after new items are added.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜