【问题标题】:Need help understanding how to sort info from text file using java需要帮助了解如何使用 java 对文本文件中的信息进行排序
【发布时间】:2015-07-31 02:27:54
【问题描述】:

我正在尝试构建一个从 .txt 文件中读取信息的程序,该文件包含 20 个人。每个人有四个领域,他们所属的球队,他们的打击率和本垒打总数。我需要将每个人添加到他们的团队中(每队 4 名球员),合计球队本垒打总数,并按顺序排列 5 支球队。 我能够正确地将文本读取到由每个人组成的单个数组中,但我无法弄清楚如何也使用这些数据来创建二维数组。我会使用二维数组,将球员放在正确的球队中,并添加他们的本垒打总数。我想对每个团队和每个人的本垒打总数从大到小进行排序。我已尽力寻找答案,并在其他帖子和网站上学习,但我只是对创建 2D 数组以及如何对它们进行排序的概念感到困惑。

更新说明: 这是单个数组的信息应该是这样的:

 [Team][Name][avg][Home Runs]

然后我只想对 [Home Runs] 列进行排序,从大到小,但不知道如何访问数组的那部分。

二维数组应如下所示:

  [Team] [Total Team Home Runs]

再一次,从大到小排序。

.txt 文件示例如下所示:

 Team: Name:         Avg:HR:

 MILRyan Braun       .31015
 STLMatt Adams       .28718
 PITSterling Marte   .26420
 CINJoey Votto       .30224
 CUBAnthony Rizzo    .27422
 PITAndrew McCutchen .29522
 MILAdam Lind        .28013

以下类读取 .txt 文件并将其放入数组中。

  public class ReadTxt {

static String[] teamm = new String[20];
static String[] name = new String[20];
static int[] avg = new int[20];
static double[] homeRuns = new double[20];

static String teams;
static int i;

public void Players(String[] teamm, String[] name, int[] avg, double[] homeRuns){
String[] team = new String[20];


File txtFile = new File("C:\\Users\\Users Name\\Desktop\\homerun.txt");

try{

    Scanner txtScan = new Scanner(txtFile);
    while(txtScan.hasNext()){

        for(i = 0; i < 20; i++){
            teams = txtScan.nextLine();
            team[i] = teams;
        }
    }

}
catch(FileNotFoundException e){
    System.out.println("File not found" + txtFile);
}

for (i = 0; i < team.length; i++){

    System.out.println(team[i]);
    }
}
}

下一节课是我的排序尝试:

public class Sort {

static String[] teamm = new String[20];
static String[] name = new String[20];
static int[] avg = new int[20];
static double[] homeRuns = new double[20];
private int index = 0;
private int US = 0;
static double[] homeRunArray;


public void Players(String[] teamm, String[] name, int[] avg, double[] homeRuns){
homeRunArray[index] = ReadTxt.homeRuns[index];
index++;;
US++;
}

public void selectionSort(){
    double temp;
    int min;

    for(int i = 0; i < US-2; i++){
        min = i;
        for(int j=i+1; j<= US-1; j++){
            if(min !=i){
                temp = homeRunArray[i];
                homeRunArray[i] = homeRunArray[min];
                homeRunArray[min] = temp;
            }
        }

    }
}

public void printArray(double[] homeRuns){
    for(int i = 0; i < 20; i++){

    System.out.print(homeRunArray[i]);
    }
        System.out.print("\n");
    }
}

【问题讨论】:

  • 请删除所有多余的代码,并更清楚地说明您的问题 -- 如何对二维数组进行排序

标签: java arrays file sorting multidimensional-array


【解决方案1】:

我不明白你的问题,但我认为你有点陷入二维阵列问题......

我建议您创建一个类并实现Comparable(或使用Comparator)。类似于下面的代码,或者更好的是,创建一个真正的 Player 类。这更容易理解。

public class Sorter {
    public static void main(String[] args) {
        try {
            Scanner scanner = new Scanner(new File("team"));
            List<SortableLine> lines = new ArrayList<SortableLine>();
            while(scanner.hasNext()) {
                lines.add(new SortableLine(scanner.nextLine()));
            }
            Collections.sort(lines);
            for(SortableLine line : lines) {
                System.out.println(line.line);
            }
        } catch(FileNotFoundException e) {
            System.err.println("File not found");
        }
    }

    private static class SortableLine implements Comparable<SortableLine> {
        private String sortCol;
        private String line;

        private SortableLine(String line) {
            this.line = line;
            this.sortCol = line.substring(24, 26);
        }

        public int compareTo(SortableLine other) {
            return sortCol.compareTo(other.sortCol);
        }
    }
}

【讨论】:

  • 谢谢,这绝对让这更容易理解!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-27
  • 1970-01-01
  • 1970-01-01
  • 2013-03-17
  • 2020-04-19
  • 1970-01-01
  • 2011-09-07
相关资源
最近更新 更多