亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java中如何使用冒泡排序對多維數組進行排序

Java中如何使用冒泡排序對多維數組進行排序

UYOU 2022-03-10 15:54:25
這是我根據運動創建的一個測驗,它提出了一系列問題,用戶每次嘗試 3 次。從那里它匯總每個玩家的分數并以二維數組的形式顯示,它比較分數并打印最高分數。我將如何使用冒泡排序(不是 array.sort)按第二個索引(分數)對二維數組(記分板)進行排序。import java.util.*;class miniproj{  public static void main(String[] args)  {    Questions[] questions = setQuestions(); // store array of questions using setquestions method    askQuestion(questions); // run method askquestion using questions parameter (array)  }  public static Questions[] setQuestions()  {   Questions[] questions = new Questions[4]; //create array of type questions    Questions A = new Questions(); // create new questons type called A    A.question = "What team won the world cup in 1966?";    A.options = " A. Germany\n B. France\n C. England\n D. Wales";    A.answer = "C";    questions[0] =  A; // the first question in the aray is A    Questions B = new Questions();    B.question = "Who are the current EPL title holders?";    B.options = " A. Arsenal\n B. Bournemouth\n C. Chelsea\n D. Manchester City";    B.answer = "D";    questions[1] =  B;    Questions C = new Questions();    C.question = "Who is the current Golden Boot holder 2017/18 season?";    C.options = " A. Lionel Messi\n B. Harry Kane\n C. Cristiano Ronaldo\n D. Davidson Sanchez";    C.answer = "A";    questions[2] =  C;    Questions D = new Questions();    D.question = "Which team has most goals";    D.options = " A. Arsenal\n B. Bournemouth\n C. Chelsea\n D. Manchester City";    D.answer = "A";    questions[3] =  D;    return questions; // return array of questions  }  public static void askQuestion(Questions[] array)  {    int correct = 0;    Scanner sc = new Scanner(System.in);    String[][] scoreboard = new String[4][2];    for(int m = 0; m < scoreboard.length; m++) {      correct = 0;      System.out.println("What is your name");      scoreboard[m][0] = sc.nextLine();
查看完整描述

2 回答

?
翻閱古今

TA貢獻1780條經驗 獲得超5個贊

如果我理解正確,你有一個像這樣的二維數組結構:


{name,score}

{name,score}

{name,score}

{name,score}

并且您想根據第二列進行排序:分數。


與其在二維數組中實現它,不如創建一個名為 Player 的對象


Player 有一個實現:


public class Player{


    private String name;

    private int score;        


    Player(String name){

        this.name = name;

    }


    public void setScore(int score){

        this.score = score;

    }


    public int getScore(){

        return score;

    }

}

現在您的記分牌現在可以實現為一個一維數組,如下所示:


Player[] scoreboard = new Player[playerSize];

更容易理解和閱讀。


現在要對該數組進行排序,您可以實現一個自定義類,該類允許您比較兩個 Player 類型的對象


class comparePlayer implements Comparator<Player>{


    public int compare(Player a, Player b) {

        if (a.getScore() < b.getScore()){

            return -1;

        }

        else if (a.getScore() == b.getScore()){

            return 0;

        }

        else{

            return 1;

        }

    }


}

現在你可以像這樣按分數排序 ->


Arrays.sort(scoreboard,new comparePlayer());

或者如果你真的想使用冒泡排序,那么你可以像這樣實現它:


int length = scoreboard.length; 

for (int i = 0; i < length-1; i++){ 

    for (int j = 0; j < length-i-1; j++){ 

        if (scoreboard[j].getScore() > scoreboard[j+1].getScore()){ 

            Player temp = scoreboard[j]; 

            scoreboard[j] = scoreboard[j+1]; 

            scoreboard[j+1] = temp; 

        } 

    }

}


查看完整回答
反對 回復 2022-03-10
?
白板的微信

TA貢獻1883條經驗 獲得超3個贊

在這種情況下,您可以通過修改來實現冒泡排序以比較重要的值。例子:


static void bubbleSort(String[][] arr) {

        int arrayLength = arr.length;

        for (int i = 0; i < arrayLength; i++) {

            for (int j = 1; j < (arrayLength - i); j++) {

                String nameTemp, scoreTemp;

                int leftValue, rightValue;

                leftValue = Integer.valueOf(arr[j - 1][1]);

                rightValue = Integer.valueOf(arr[j][1]);

                if (leftValue > rightValue) {

                    //swap elements

                    nameTemp = arr[j - 1][0];

                    scoreTemp = arr[j - 1][1];

                    arr[j - 1][0] = arr[j][0];

                    arr[j - 1][1] = arr[j][1];

                    arr[j][0] = nameTemp;

                    arr[j][1] = scoreTemp;

                }


            }

        }

}

然后好吧,你想要數組的最后一個索引,因為它是升序排序的。


查看完整回答
反對 回復 2022-03-10
  • 2 回答
  • 0 關注
  • 138 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號