2010-10-08 44 views
-1

嘿,夥計們。 thanx爲我的障礙提供重要幫助。 這次我的問題是,我如何排序在我的代碼中提供的數組列表基本上不知道WhatI需要添加在下面提供的代碼中,只是爲了簡單它我得到了3個ArrayList,我想讓它們變成一個數組列表,所以他們可以被排序在猜測和嘗試(如果2球員有相同的猜測,然後時間應該確定)。 很難解釋它,但我盡我所能.............最好的辦法是運行它,然後你會知道什麼是什麼問題是什麼?如何對數組列表進行排序?

下面是代碼:

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(); 
    } 
} 

回答

1

排序,你可以使用Collections.sort()方法一個ArrayList;要麼只有一個列表Comparable元素或Comparator

這是最簡單的方法。

在你的情況下Collections.sort(scores)會做的事情。

+0

但我應該如何開始 – Benjamin 2010-10-08 12:54:45

+0

請告訴我。 – Benjamin 2010-10-08 12:55:14

+2

只是'Collections.sort(分數);'在你的代碼 – 2010-10-08 12:59:54

0

要對列表進行排序,請使用java.util.Collections.sort(list)

+0

,然後我該怎麼辦 – Benjamin 2010-10-08 12:53:16

0

要創建三個列表中的一個,您可以使用alist.addAll(anotherList)。然後根據Collin Hebert的建議分類aList

4

使用Collections.sort(),並確保您的類實現Comparable或使用這樣的:

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

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

我幫你以前的帖子來回答這個問題。不過,我也會在這裏添加它。我想你可能想要一個List而不是一個String列表。這將允許您以自然的方式對最終用戶試圖嘗試的次數進行排序。除此之外,最簡單的方法是撥打Collections.sort(list)

此外,toString()方法用於調試目的或提供人類可讀信息。它不應該被用來以你使用它的方式創建對象。只是我的.02

2

您可以使用類似input.nextInt()的內容讀取下一個整數而不是下一個字符串。我看不出爲什麼你要在代碼中存儲字符串而不是整數。然後,您可以使用其他人建議的方式來組合列表和/或對其進行排序。

我也認爲你應該自己做一些更多的閱讀。 Java文檔回答了您在評論中提出的一些問題。 當有人發佈建議時,至少在API中查找並嘗試理解它。人們不會在這裏給你所有的代碼,他們只會給你提示你需要遵循。

0

如果您絕對需要一個列表進行排序,請不要使用ArrayList,請使用PriorityQueue。它可以構建爲使用內容的自然排序順序(即通過每個對象上的Comparable接口)或通過比較器。 PriorityQueue的優勢在於,即使在添加新項目後,它也總是被排序。