在java中对相关文本文件进行排序创建公共索引

发布于 2024-10-21 16:38:58 字数 1915 浏览 3 评论 0 原文

大家好 我想对 2 个相关文件进行排序,创建一个可以对它们进行排序的唯一索引

我有一个格式如下示例的 .txt 文件:

File1

Houston
Chicago
Seattle
Cleveland

另一个文件的格式如下:

File2

44
33
55
22

我想要以下输出:

Seattle 55
Houston 44
Chicago 33
Cleveland 22

我创建了 2 个数组.txt 文件中的对象,因此由于“< 或 >”,我无法使用冒泡排序操作员。我使用 .sort 函数对分数进行排序,但通过这种方式,我不会创建索引来对团队进行排序。我该如何解决这个问题?

这是我的代码

 private void visualizzaClassificaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                           
    // TODO add your handling code here:
    List<String> teams = new ArrayList<String>();
    List<Float> scores = new ArrayList<Float>();

    try {
        FileInputStream fstream = new FileInputStream("C:/Users/Fra....../file1");
        FileInputStream fstream2 = new FileInputStream("C:/Users/Fra...../file2");
        DataInputStream in = new DataInputStream(fstream);
        DataInputStream in2 = new DataInputStream(fstream2);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
        String team = null;
        String score = null;

        while ((score = br.readLine()) != null && (team = br2.readLine()) !=null)   {
            teams.add(team);
            scores.add(Float.parseFloat(score));
        }

        Object[] squadreTutte = teams.toArray();
        Object[] punteggiTutti = scores.toArray();
        Arrays.sort(punteggiTutti, Collections.reverseOrder());
        //????????????????????????????????
        for(int index=0; index<punteggiTutti.length; index++){
            System.out.println(punteggiTutti[index]);
            System.out.println(squadreTutte[index]);
        }
        in.close();
    } catch (Exception e) {
    }

}

请帮忙

Hi all
I want to sort 2 related files, creating an unique index which can sort both of them

I have a .txt file formatted as the following sample:

File1

Houston
Chicago
Seattle
Cleveland

The other file is formatted like this:

File2

44
33
55
22

I want the following output:

Seattle 55
Houston 44
Chicago 33
Cleveland 22

I've created 2 array of objects from the .txt files, so I can't use bubblesort because of the "< or >" operator. I've used .sort function to sort the scores but in this way I don't create an index to sort the teams. How can I solve the problem?

That's my code

 private void visualizzaClassificaButtonActionPerformed(java.awt.event.ActionEvent evt) {                                                           
    // TODO add your handling code here:
    List<String> teams = new ArrayList<String>();
    List<Float> scores = new ArrayList<Float>();

    try {
        FileInputStream fstream = new FileInputStream("C:/Users/Fra....../file1");
        FileInputStream fstream2 = new FileInputStream("C:/Users/Fra...../file2");
        DataInputStream in = new DataInputStream(fstream);
        DataInputStream in2 = new DataInputStream(fstream2);
        BufferedReader br = new BufferedReader(new InputStreamReader(in));
        BufferedReader br2 = new BufferedReader(new InputStreamReader(in2));
        String team = null;
        String score = null;

        while ((score = br.readLine()) != null && (team = br2.readLine()) !=null)   {
            teams.add(team);
            scores.add(Float.parseFloat(score));
        }

        Object[] squadreTutte = teams.toArray();
        Object[] punteggiTutti = scores.toArray();
        Arrays.sort(punteggiTutti, Collections.reverseOrder());
        //????????????????????????????????
        for(int index=0; index<punteggiTutti.length; index++){
            System.out.println(punteggiTutti[index]);
            System.out.println(squadreTutte[index]);
        }
        in.close();
    } catch (Exception e) {
    }

}

Please help

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

原野 2024-10-28 16:38:58

几乎总是当有人想要对并行数组进行排序时,就会发生 拒绝对象

您应该做的是创建一个对象来保存名称,分数对:

public class Team implements Comparable<Team> {
  private final String name;
  private final int score;

  public Team(final String name, final int score) {
    this.name=name;
    this.score=score;
  }

  // add getters

  public int compareTo(Team other) {
    if (this.score != other.score) {
      return Integer.valueOf(this.score).compareTo(other.score);
    } else {
      return this.name.compareTo(other.name);
    }
  }
}

然后,在读取文件时,为文件中的每一行创建一个 Team 对象,将它们放入 收集并排序。您需要实现 Comparable 在你的班级中(就像我上面所做的那样)或提供 Comparator 对它们进行排序。

Almost always when someone wants to sort parallel arrays, it's a case of object denial.

What you should be doing is to create an object to hold the name,score pair:

public class Team implements Comparable<Team> {
  private final String name;
  private final int score;

  public Team(final String name, final int score) {
    this.name=name;
    this.score=score;
  }

  // add getters

  public int compareTo(Team other) {
    if (this.score != other.score) {
      return Integer.valueOf(this.score).compareTo(other.score);
    } else {
      return this.name.compareTo(other.name);
    }
  }
}

Then, when reading the files, create a Team object for each line in the files, put them into a Collection and sort that. You either need to implement Comparable in your class (as I did above) or provide a Comparator to sort them.

一腔孤↑勇 2024-10-28 16:38:58

在自然语言中对球队和得分进行排序并以相反的顺序读取集合要简单得多。顺便说一句,除非有充分的理由,否则不要使用 float 或 Float。使用 Long 或 Double 代替。

基于@Joachim 的团队课程

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

BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
BufferedReader br2=new BufferedReader(new FileReader("C:/Users/Fra..../file2"));

String name = null;
String score = null;
while ((score = br.readLine()) != null && (name = br2.readLine()) != null) 
    teams.add(new Team(name, Double.parseDouble(score)));
br.close();
br2.close();

// reverse sort.
Collections.sort(teams, new Comparator<Team>() {
    public int compare(Team t1, Team t2) {
        return Double.compare(t2.score, t1.score);
    }
});


for(Team t: teams)
   System.out.println(t.name + ' ' + t.score);


public class Team {
  final String name;
  final double score;

  public Team(String name, double score) {
    this.name=name;
    this.score=score;
  }
}

It is a lot simpler to sort the team and score in natrual worder and read the collection is reverse order. BTW Don't use float or Float unless there is a really good reason to do so. Using Long or Double instead.

Based on @Joachim's Team class

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

BufferedReader br=new BufferedReader(new FileReader("C:/Users/Fra..../file1"));
BufferedReader br2=new BufferedReader(new FileReader("C:/Users/Fra..../file2"));

String name = null;
String score = null;
while ((score = br.readLine()) != null && (name = br2.readLine()) != null) 
    teams.add(new Team(name, Double.parseDouble(score)));
br.close();
br2.close();

// reverse sort.
Collections.sort(teams, new Comparator<Team>() {
    public int compare(Team t1, Team t2) {
        return Double.compare(t2.score, t1.score);
    }
});


for(Team t: teams)
   System.out.println(t.name + ' ' + t.score);


public class Team {
  final String name;
  final double score;

  public Team(String name, double score) {
    this.name=name;
    this.score=score;
  }
}
太阳公公是暖光 2024-10-28 16:38:58

目标是第一个列表中的球队按第二个列表中的分数排序?

您的问题是两个列表的排序不相关。您应该尝试对情侣进行“团队+得分”排序。也许使用 List> 之类的东西可以通过定义自定义比较器来完成您想要的操作......

The goal is that the teams in the first list are sorted by score from the second list ?

Your problem is that the sorting of both lists are unrelated. You should try instead to sort the couples "team+score". Maybe using something like a List<Entry<Double, String>> could do what you want by defining a custom comparator...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文