- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
我正在尝试制作一个概率结果模拟器,其中的信息取自 .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);
}
}
最佳答案
我不会使用两个独立的数据结构,而是将它们组合成一个表示数据潜在含义的简单 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
接口(interface),您可以使用 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);
)
假设这个 compare-to 实现为较小优先,我认为是这样(但我想不起来),我们的列表将按如下顺序排列:
[(Baltimore, 1.0), (Denver, 13.0), (NY, 22.0), (Detroit, 22.0), (Atlanta, 25.0), (Michigan, 25.0)]
请注意,由于我们的 compareTo
方法仅考虑置信度,因此置信度内没有排序;所以纽约和底特律将相邻,但不能保证纽约始终排在底特律之前。
根据下面的评论,最好的模型如下:
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)]
关于java - 对两个对应的数组进行排序时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34147441/
我正在尝试对每个条目有多个值的关联数组进行排序。 例如 [0] => stdClass Object ( [type] => node [sid] => 158 [score] => 0.059600
我在 mysql 中有“日期”列以这种格式保存日期 2014 年 9 月 17 日(日-月-年) 我需要对它们进行升序排序,所以我使用了这个命令: SELECT * FROM table ORDER
我目前正在将 MySQL 存储过程重写为 MS SQL 存储过程,但遇到了问题。 在 MySQL 存储过程中,有一个游标,它根据最近的日期 (effdate) 选择一个值并将其放入变量 (thestt
我想要 gwt r.QuestionId- 排序。但是我得到未排序的 QuestionId 尽管我提到了 QuestionId ASC 的顺序。 SELECT r.QuestionId,
我有一个关于在 scandir 函数中排序的基本问题。到目前为止,我阅读了 POSIX readdir 的手册页,但没有找到有关订购保证的具体信息。 但是当我遍历大目录(无法更改,只读)时,我在多个系
基本上我必须从 SQL 数据库中构建项目列表,但是用户可以选择对 7 个过滤器的任意组合进行过滤,也可以选择要排序的列以及按方向排序。 正如您可以想象的那样,这会以大量不同的组合进行编码,并且数据集非
我有两张 table 。想象第一个是一个目录,包含很多文件(第二个表)。 第二个表(文件)包含修改日期。 现在,我想选择所有目录并按修改日期 ASC 对它们进行排序(因此,最新的修改最上面)。我不想显
我想先根据用户的状态然后根据用户名来排序我的 sql 请求。该状态由 user_type 列设置: 1=活跃,2=不活跃,3=创始人。 我会使用此请求来执行此操作,但它不起作用,因为我想在“活跃”成员
在 C++ 中,我必须实现一个“类似 Excel/Access”(引用)的查询生成器,以允许对数据集进行自定义排序。如果您在 Excel 中使用查询构建器或 SQL 中的“ORDER BY a, b,
我面临这样的挑战: 检索按字段 A 排序的文档 如果字段 B 存在/不为空 . 否则 按字段排序 C. 在 SQL 世界中,我会做两个查询并创建一个 UNION SELECT,但我不知道如何从 Mon
我想对源列表执行以下操作: map 列表 排序 折叠 排序 展开 列表 其中一些方法(例如map和toList)是可链接的,因为它们返回非空对象。但是,sort 方法返回 void,因为它对 List
我制作了一个用于分析 Windows 日志消息编号的脚本。 uniq -c 数字的输出很难预测,因为根据数字的大小会有不同的空白。此时,我手动删除了空白。 这是对消息进行排序和计数的命令: cat n
我有以下词典: mydict1 = {1: 11, 2: 4, 5: 1, 6: 1} mydict2 = {1: 1, 5: 1} 对于它们中的每一个,我想首先按值(降序)排序,然后按键(升序)排序
我刚刚开始使用泛型,目前在对多个字段进行排序时遇到问题。 案例: 我有一个 PeopleList 作为 TObjectList我希望能够通过一次选择一个排序字段,但尽可能保留以前的排序来制作类似 Ex
有没有办法在 sql 中组合 ORDER BY 和 IS NULL 以便我可以在列不为空时按列排序,但如果它为null,按另一列排序? 最佳答案 类似于: ORDER BY CASE WHEN
我有一个包含 2 列“id”和“name”的表。 id 是常规的自动增量索引,name 只是 varchar。 id name 1 john 2 mary 3 pop 4 mary 5 j
场景 网站页面有一个带有分页、过滤、排序功能的表格 View 。 表中的数据是从REST API服务器获取的,数据包含数百万条记录。 数据库 REST API 服务器 Web 服务器 浏览器 问
假设我有一本字典,其中的键(单词)和值(分数)如下: GOD 8 DONG 16 DOG 8 XI 21 我想创建一个字典键(单词)的 NSArray,首先按分数排序,然后按字
如何在 sphinx 上通过 sql 命令选择前 20 行按标题 WEIGHT 排序,接下来 20 行按标题 ASC 排序(总共 40 个结果),但不要给出重复的标题输出。 我尝试了这个 sql 命令
我有一个奇怪的问题,当从 SQLite 数据库中选择信息并根据日期排序时,返回的结果无效。 我的SQL语句是这样的: Select pk from usersDates order by dateti
我是一名优秀的程序员,十分优秀!