Java中如何使用冒泡排序对多维数组进行排序

这是我根据运动创建的一个测验,它提出了一系列问题,用户每次尝试 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();


UYOU
浏览 111回答 2
2回答

翻阅古今

如果我理解正确,你有一个像这样的二维数组结构:{name,score}{name,score}{name,score}{name,score}并且您想根据第二列进行排序:分数。与其在二维数组中实现它,不如创建一个名为 Player 的对象Player 有一个实现:public class Player{&nbsp; &nbsp; private String name;&nbsp; &nbsp; private int score;&nbsp; &nbsp; &nbsp; &nbsp;&nbsp;&nbsp; &nbsp; Player(String name){&nbsp; &nbsp; &nbsp; &nbsp; this.name = name;&nbsp; &nbsp; }&nbsp; &nbsp; public void setScore(int score){&nbsp; &nbsp; &nbsp; &nbsp; this.score = score;&nbsp; &nbsp; }&nbsp; &nbsp; public int getScore(){&nbsp; &nbsp; &nbsp; &nbsp; return score;&nbsp; &nbsp; }}现在您的记分牌现在可以实现为一个一维数组,如下所示:Player[] scoreboard = new Player[playerSize];更容易理解和阅读。现在要对该数组进行排序,您可以实现一个自定义类,该类允许您比较两个 Player 类型的对象class comparePlayer implements Comparator<Player>{&nbsp; &nbsp; public int compare(Player a, Player b) {&nbsp; &nbsp; &nbsp; &nbsp; if (a.getScore() < b.getScore()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return -1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if (a.getScore() == b.getScore()){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 0;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return 1;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}现在你可以像这样按分数排序 ->Arrays.sort(scoreboard,new comparePlayer());或者如果你真的想使用冒泡排序,那么你可以像这样实现它:int length = scoreboard.length;&nbsp;for (int i = 0; i < length-1; i++){&nbsp;&nbsp; &nbsp; for (int j = 0; j < length-i-1; j++){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; if (scoreboard[j].getScore() > scoreboard[j+1].getScore()){&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Player temp = scoreboard[j];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scoreboard[j] = scoreboard[j+1];&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scoreboard[j+1] = temp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp;&nbsp; &nbsp; }}

白板的微信

在这种情况下,您可以通过修改来实现冒泡排序以比较重要的值。例子:static void bubbleSort(String[][] arr) {&nbsp; &nbsp; &nbsp; &nbsp; int arrayLength = arr.length;&nbsp; &nbsp; &nbsp; &nbsp; for (int i = 0; i < arrayLength; i++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for (int j = 1; j < (arrayLength - i); j++) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String nameTemp, scoreTemp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int leftValue, rightValue;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; leftValue = Integer.valueOf(arr[j - 1][1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; rightValue = Integer.valueOf(arr[j][1]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (leftValue > rightValue) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //swap elements&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; nameTemp = arr[j - 1][0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scoreTemp = arr[j - 1][1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j - 1][0] = arr[j][0];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j - 1][1] = arr[j][1];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j][0] = nameTemp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; arr[j][1] = scoreTemp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }}然后好吧,你想要数组的最后一个索引,因为它是升序排序的。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java