2013-02-25 35 views
1

我試圖以降序輸出名稱和相應的分數。有一個字符串數組和另一個整數數組,我試圖將兩個數組關聯起來。我用Arrays.sort並嘗試獲取索引。然後使用索引將名稱排列在與相應分數類似的位置。我有這個代碼,但我得到運行時錯誤說不幸的是,你的應用程序已停止。任何人都可以請幫助我做些什麼來實現我的目標?非常感謝!按降序對整數數組進行排序並將其與其對應的字符串數組相關聯

  int score[]= new int[4]; 
      score[0]= 10; 
      score[1]= 50;    
      score[2]= 20; 
      score[3]= 60; 
      String names[] = new String[4]; 
      names[0]= "player1"; 
      names[1]= "player2"; 
      names[2]= "player3"; 
      names[3]= "player4"; 

      Arrays.sort(score); 
      int index_array[] = new int[4]; 
      int m = 0; 
      for(m = 0; m <4; m++){ 
       index_array[m]=Arrays.binarySearch(score ,score[m]); 
       l = index_array[0]; 


      } 
      for(int i = 0; i<4; i++){ 
       if(l == score[i]){ 
        j = i; 
       } 
      } 
      String name = names[m]; 
      show.setText(name + " " + Integer.toString(l)); 
+1

post stacktrace – moDev 2013-02-25 19:28:16

+0

請發佈您的LogCat輸出 – h4ck3d 2013-02-25 19:28:31

+0

我相信這被稱爲「索引排序」。 – 2013-02-25 19:30:52

回答

1

創建持有玩家的名字播放器模型和評分,然後用比較的玩家按分數排序。

播放器型號:

class Player { 

private String name; 

private int score; 

public Player(String name, int score) { 
    this.name = name; 
    this.score = score; 
} 

public String getName() { 
    return name; 
} 

public int getScore() { 
    return score; 
} 

public String toString() { 
    return "name=" + name + "; score=" + score; 
} 

}

比較:

class PlayerComparator implements Comparator<Player> { 
public int compare(Player p1, Player p2) { 
    return p1.getScore() < p2.getScore() ? -1 
      : p1.getScore() > p2.getScore() ? 1 : 0; 
} 

}

和使用情況的一個例子:

public class PlayerTest { 

public static void main(String[] args) { 
    int score[]= new int[4]; 
    score[0]= 10; 
    score[1]= 50; 
    score[2]= 20; 
    score[3]= 60; 

    String names[] = new String[4]; 
    names[0]= "player1"; 
    names[1]= "player2"; 
    names[2]= "player3"; 
    names[3]= "player4"; 

    Player[] players = new Player[names.length]; 
    for(int i = 0; i < names.length; i++) { 
     players[i] = new Player(names[i], score[i]); 
    } 

    Arrays.sort(players, new PlayerComparator()); 

} 

}

0

這是一種設計的氣味。你不應該有兩個平行的數組。相反,你應該有一個Player對象的數組,其中每個Player都有一個名字和一個分數。

然後通過名稱或分數存儲球員陣容將非常簡單。

Java是OO語言。使用對象。

public class Player 
    private final String name; 
    private int score; 

    public Player(String name, int score) { 
     this.name = name; 
     this.score = score; 
    } 

    public String getName() { 
     return name; 
    } 

    public int getScore() { 
     return score; 
    } 

    public void setScore(int score) { 
     this.score = score; 
    } 
} 

... 
Player[] players = new Player[4]; 
players[0] = new Player("player1", 10); 
... 
+0

是的,我可以這樣做,但如果我將整數保存爲一個字符串,我怎麼能夠根據分數對它進行排序?我仍然可以獲取字符串行的數字部分,並根據轉換爲字符串的數字排列所有行嗎?你能告訴我一個例子嗎?謝謝@JB Nizet – chArm 2013-02-25 19:38:29

+0

你爲什麼要把它作爲一個字符串存儲?將其存儲爲int。我會編輯我的答案。 – 2013-02-25 19:44:13

2

您需要關聯分數和用戶名。目前,您正在通過數組索引關聯它們。當您對分數進行排序時,分數的指數會發生變化。

嘗試是這樣的:

class Score implements Comparable<Score> 
{ 
    int score; 
    String player; 

    public Score(int theScore, String thePlayer) 
    { 
    score = theScore; 
    player = thePlayer; 
    } 

    public int compareTo(Score) 
    { 
    ... compare based on the int score value ... 
    } 

    ... getters. setters optional ... 
} 

List<Score> scoreList = new ArrayList<Score>(); 

... fill scoreList with Score objects. ... 

Collections.sort(scoreList);