将两个对应数组排序时出现错误

3

我正在尝试制作一个概率结果模拟器,其中信息来自于一个.csv文件。创建了两个ArrayList,然后将信息和结果放入这些ArrayList中。Double ArrayList包含String ArrayList中每个相应的两个团队的置信度评级。

Example: 
Double ArrayList:  25, 22, 50
String ArrayList: Atlanta, Michigan, NY, Detroit

Atlanta and Michigan would correspond to 25, NY and Detroit would correspond to 22.

我已经编写了程序,置信度评分双倍的ArrayList被排序了,但是球队名称的String ArrayList没有被排序。 这是它们在排序之前的两个ArrayList:

[1.0, 7.0, 8.0, 1.0, 10.0, 2.0, 4.0, 3.0, 1.0, 1.0, 9.0, 1.0, 3.0, 6.0, 0.0, 16.0]
[Green Bay, [Detroit, NY Jets, [NY Giants, [St. Louis, Arizona, [Tampa Bay, Atlanta, [Minnesota, Seattle, Houston, [Buffalo, [Miami, Baltimore, Cincinnati, [Cleveland, Jacksonville, [Tennessee, SF, [Chicago, Denver, [San Diego, KC, [Oakland, Carolina, [New Orleans, [New England, Philly, [Pittsburgh, Indy, [Washington, Dallas]

在这两个列表排序后,它们看起来是这样的:
[16.0, 10.0, 9.0, 8.0, 7.0, 6.0, 4.0, 3.0, 3.0, 2.0, 1.0, 1.0, 1.0, 1.0, 1.0, 0.0]
[[NY Giants, [Cleveland, Jacksonville, Seattle, [Cleveland, [St. Louis, Houston, Arizona, [Buffalo, [NY Giants, [Cleveland, [Cleveland, Baltimore, [Miami, Atlanta, [NY Giants, Atlanta, [Tennessee, SF, [Chicago, Denver, [San Diego, KC, [Oakland, Carolina, [New Orleans, [New England, Philly, [Pittsburgh, Indy, [Washington, Dallas]

置信度评分按降序成功排序了,但是团队与其相应的得分不对应。实际上,同一个团队被复制了多次。我该如何解决这个问题并让所有团队对应他们适当的评分?(sortArrays()方法是排序操作发生的地方)。

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.layout.*;
import javafx.scene.control.*;
import javafx.stage.FileChooser;
import javafx.geometry.*;
import java.util.*;
import java.io.*;

public class POS extends Application
{
   private ArrayList<Double> confidenceList = new ArrayList<>();
   private ArrayList<String> cityList = new ArrayList<>();
   private BorderPane pane = new BorderPane();
   private Button runBtn = new Button("Run");
   @Override
   public void start(Stage stage)
   {


      VBox vBox = new VBox(20);
      vBox.setPadding(new Insets(15));
      Button selectBtn = new Button("Select File");
      selectBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");
      vBox.getChildren().add(selectBtn);

      selectBtn.setOnAction(e->
      {
         FileChooser fileChooser = new FileChooser();
         fileChooser.setTitle("Open Resource File");
         FileChooser.ExtensionFilter extFilter = 
                        new FileChooser.ExtensionFilter("TEXT files (*.csv)", "*.CSV", ".xlsv", ".XLSV");
                fileChooser.getExtensionFilters().add(extFilter);
         File file = fileChooser.showOpenDialog(stage);

            run(file);


      });

      RadioButton weekBtn = new RadioButton("Current Week");  
      RadioButton seasonBtn = new RadioButton("Entire Season");

      runBtn.setStyle("-fx-font: 22 arial; -fx-base: #b6e7c9;");


      weekBtn.setSelected(true);
      seasonBtn.setDisable(true);
      vBox.getChildren().add(weekBtn);
      vBox.getChildren().add(seasonBtn);
      vBox.getChildren().add(runBtn);

      pane.setLeft(vBox);
      Scene scene = new Scene(pane, 500, 200);
      stage.setScene(scene);
      stage.setTitle("POS");
      stage.show();
   }
   public void run(File file)
   {
      runBtn.setOnAction(e->
      {
         try
         {
            Scanner input = new Scanner(file);
            input.nextLine(); 
            sortFile(file, input);

            input.close();
         }

         catch (InputMismatchException ex)
         {
            System.out.println("Error you seem to have typed the wrong type of file");
         }
         catch(IOException ex)
         {
            System.out.println("Error, file could not be found");
         }


      });
   }
   public void sortFile(File file, Scanner input)
   {
      if (!input.hasNext())
      {
         sortArrays(confidenceList, cityList);
      }
      else
      {
      String strList = Arrays.toString(input.nextLine().split("\t"));
      String[] arrList = strList.split(",");

      int homeRank = Integer.parseInt(arrList[1]);

      int roadRank = Integer.parseInt(arrList[6]);
      Random r = new Random();

      int lowestTeamRank = Math.abs(homeRank - roadRank);

      double numForHomeTeam = 0;
      double numForRoadTeam = 0;
      if (homeRank < roadRank)
      {
         numForHomeTeam = ((r.nextInt(lowestTeamRank) - r.nextInt(2)) + (getLastGameOutcome(arrList[4])* r.nextInt(3))) - getWinPct(arrList[2], arrList[3]);

         numForRoadTeam = ((r.nextInt(roadRank) + r.nextInt(2)) + (getLastGameOutcome(arrList[9])* r.nextInt(3))) - getWinPct(arrList[7], arrList[8]);
      }

      else if (homeRank > roadRank)
      {
         numForHomeTeam = ((r.nextInt(homeRank) - r.nextInt(2)) + (getLastGameOutcome(arrList[4])* r.nextInt(3))) - getWinPct(arrList[2], arrList[3]);

         numForRoadTeam = r.nextInt(lowestTeamRank) - r.nextInt(2) + getLastGameOutcome(arrList[9])* r.nextInt(3) - getWinPct(arrList[7], arrList[8]);
      }



      double confidenceRate = Math.round(Math.abs(numForHomeTeam - numForRoadTeam));
      confidenceList.add(confidenceRate);
      if (numForHomeTeam < numForRoadTeam)
      {
          cityList.add(arrList[0]);
          cityList.add(arrList[5]);
      }
      else if (numForHomeTeam > numForRoadTeam)
      {
         cityList.add(arrList[5]);
         cityList.add(arrList[0]);
      }
      else
      {
         cityList.add(arrList[0]);
         cityList.add(arrList[5]);
      }

      sortFile(file, input);
      }
   }

   public int getLastGameOutcome(String lastGame)
   {
      if (lastGame.charAt(0) == 'W')
      {
         return (int)(Math.random() * 3);
      }

      else
      {
         return (int)(Math.random() * -3);
      }  
   }

   public double getWinPct(String wins, String losses)
   {
       double newWins = Double.parseDouble(wins);
       double newLosses = Double.parseDouble(losses);
       return newWins / (newWins + newLosses);
   } 

   public void sortArrays(ArrayList<Double> doubleArray, ArrayList<String> stringArray)
   {
      System.out.println(doubleArray);
      System.out.println(stringArray);
      for (int i = 0; i < doubleArray.size(); i++)
      {
         for (int j = 0; j < doubleArray.size(); j++)
         {
            if (doubleArray.get(j).compareTo(doubleArray.get(i)) < 1)
            {
               double tempDouble = doubleArray.get(j);
               doubleArray.set(j, doubleArray.get(i));
               doubleArray.set(i, tempDouble);

               String tempString = stringArray.get(j);
               String tempString2 = stringArray.get(j + 1);
               stringArray.set(j, stringArray.get(i));
               stringArray.set(j + 1, stringArray.get(i + 1));
               stringArray.set(i, tempString);
               stringArray.set(i + 1, tempString2);
            }
         }
      }

      System.out.println(doubleArray);
      System.out.println(stringArray);
   } 

}
1个回答

3

我将这两个独立的数据结构合并为一个List。该List包含了一个简单的Java对象,用于表示数据的基本含义。

例如,可以使用一个包含置信度和团队信息的Java对象:

public class TeamConfidence implements Comparable<TeamConfidence> {
  private String team;
  private double confidence;

  public TeamConfidence(String team, double confidence) {
    this.team = team;
    this.confidence = confidence;
  }

  @Override
  public int compareTo(TeamConfidence other) {
      if(other == this) { 
        return true;
      } else if (other == null ) {
        return false;
      } else {
        return Double.compare(confidence, other.confidence);
      }
  }

  // include getters and setters, maybe a constructor
}

由于它实现了Comparable接口,您可以使用Collections.sort调用按置信度进行排序:

List<TeamConfidence> teams = new ArrayList<>();
// populate list
Collections.sort(teams);
// list is now ordered by confidence, and still retains the relation between 
// the team name and the confidence level

这是一个简化的示例,演示如何实施该功能。

假设我们有一个最小的数据集,使用您原来的示例,并将丹佛和巴尔的摩作为一些较低的离群值:

| Team      | Confidence |
| Atlanta   | 25         |
| Michigan  | 25         |
| Detroit   | 22         |
| NY        | 22         |
| Denver    | 13         |
| Baltimore | 1          |

请注意,我以上添加了一个构造函数到TeamConfidence类,只是为了演示目的。

首先我们要创建一组TeamConfidence对象。这里我们手动创建它们作为一个例子,但你可以根据需要从文件、数据库或其他数据源进行调整。

与此同时,我们也要将这些对象添加到List中。

// declare a list to hold the TeamConfidence objects
List<TeamConfidence> teams = new ArrayList<>();

// populate the list
teams.add(new TeamConfidence("Detroit", 22.0));
teams.add(new TeamConfidence("Atlanta", 25.0));
teams.add(new TeamConfidence("Baltimore", 1.0));
teams.add(new TeamConfidence("Michigan", 25.0));
teams.add(new TeamConfidence("NY", 22.0));
teams.add(new TeamConfidence("Denver", 13.0));

目前,我们有一个团队列表。现在我们调用 sort

Collections.sort(teams);

现在我们的列表按顺序列出了我们的团队。根据您在TeamConfidence中实现compareTo方法的方式,这将导致较小的排在前面或者较大的排在前面。(要交换顺序,请乘以-1;例如:-1*Double.compare(confidence, other.confidence);)。假设这个比较方法是按照较小的排在前面实现的,我认为是这样的(但我无法立即回忆起来),我们的列表将按以下顺序排序:
[(Baltimore, 1.0), (Denver, 13.0), (NY, 22.0), (Detroit, 22.0), (Atlanta, 25.0), (Michigan, 25.0)]

请注意,由于我们的compareTo方法仅考虑置信度,因此在置信度级别内没有排序; 因此,NY和底特律将相邻,但不能保证NY始终在底特律之前。
根据下面的评论,最好将模型设置如下:
public class TeamConfidence implements Comparable<TeamConfidence> {
  private String winner;
  private String loser;
  private double confidence;

  public TeamConfidence(String winner, String loser, double confidence) {
    this.winner = winner;
    this.loser = loser;
    this.confidence = confidence;
  }

  @Override
  public String toString() {
    return "(" + confidence + ", " + winner + ", " + loser ")";
  }

  @Override
  public int compareTo(TeamConfidence other) {
      if(other == this) { 
        return true;
      } else if (other == null ) {
        return false;
      } else {
        return Double.compare(confidence, other.confidence);
      }
  }
}

现在,当您按置信度排序时,列表中的每个元素都将指示赢家和输家。
数据:
| Winner  | Loser     | Confidence |
| Atlanta | Michigan  | 25         |
| NY      | Detroit   | 22         |
| Denver  | Baltimore | 13         |

代码:

List<TeamConfidence> teams = new ArrayList<>();

// populate the list
teams.add(new TeamConfidence("Atlanta", "Michigan", 25.0));
teams.add(new TeamConfidence("NY", "Detroit", 22.0));
teams.add(new TeamConfidence("Denver", "Baltimore", 13.0));

Collections.sort(teams);

结果:

// (confidence, winner, loser)
[(13.0, Denver, Baltimore), (22.0, NY, Detroit), (25.0, Atlanta, Michigan)]

我需要添加两个构造函数才能同时添加团队(字符串)和信心评级(双精度浮点数),对吗?此外,我需要添加信心、根据胜利情况确定正确的团队顺序,然后排序。我只需要编写Collections.sort(teams); 它会根据信心进行排序吗?您能否为我提供一些详细步骤,以便根据我的程序实现这一点,因为我有一些难以理解我需要做什么才能获得结果。谢谢 :) - Bytes
嗨,@Bytes。我更新了我的答案,并附上了一个例子。希望这能澄清事情。 - Roddy of the Frozen Peas
差不多搞定了!列表以递增顺序显示,而不是递减。我想要高数字先显示,低数字最后显示,这可能吗?我想将这些数据显示为GridPane网格,其中有三列(置信度、赢家、输家)。如何在GridPane中使用for循环从置信水平递增到递减的方式来处理这些数据? - Bytes
是的,正如我之前提到的那样,改变顺序只需要将比较器取反即可。在compareTo方法中将return语句修改为-1 * Double.compare( confidence, other.confidence )。(或者可以交换参数位置,但不要同时进行两种操作。)然后当你使用Collection.sort时,它会按照你期望的顺序排序。 - Roddy of the Frozen Peas
我在你离开期间解决了它 ;) 非常感谢!你真的帮助我学习并克服了这个任务! - Bytes
显示剩余2条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接