Java使用Comparable解决排序问题

网友投稿 203 2023-08-02


Java使用Comparable解决排序问题

本文实例讲述了java使用Comparable解决排序问题的方法。分享给大家供大家参考。具体实现方法如下:

一次举重竞赛的比赛规则是:选手的成绩以成功举起的总重量来排序,举起总重量多的排在前面;当举起总重量相同时,按照体重来排序,体重轻的排在前面;要求程序读取数据文件作为输入,并按照上述规则排序后,打印出选手编号;数据文件说明如下:现有5名选手,其选手编号、成功举起的总重量及其体重如数据文件data4.txt,样例内容为:

1

140

54

2

155

53

3

140

42

4

140

55

5

130

46

首先我要解决的是文件解析的问题:

如何把文件内容解析成想要的数据:即提取出每个选手的编号,成绩和体重

我用一个实体Person来封装这些属性

整体代码:

import java.io.BufferedReader;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.IOException;

import java.util.ArrayList;

import java.util.Arrays;

public class forth {

public static void main(String[] args) {

ArrayList list=new ArrayList();

try {

FileReader fr=new FileReader("c:\\data.txt");

BufferedReader br=new BufferedReader(fr);

String str=null;

int num=0;

int score=0;

int weight=0;

int i=0;

while((str=br.readLine())!=null)

{

i++;

if(i%5==2)

{

str=str.trim().substring(4,str.length()-5);

num=Integer.parseInt(str);

str=br.readLine().tDQlWhZrim();

str=str.substring(4,str.length()-5);

score=Integer.parseInt(str);

i++;

str=br.readLine().trim();

str=str.substring(4,str.length()-5);

weight=Integer.parseInt(str);

i++;

Person p=new Person(num,score,weight);

list.add(p);

}

else

continue;

}

} catch (FileNotFoundException e) {

e.printStackTrace();

}

catch (IOExceDQlWhZption e) {

e.printStackTrace();

}

Person[] plist=new Person[list.size()];

list.toArray(plist);

Arrays.sort(plist);

for(int i=0;i

{

System.out.print(plist[i].getNum()+". " +plist[i].getScore()+" "+plist[i].getWeight()+"\n\r");

}

}

}

class Person implements Comparable{

private int num;

private int weight;

private int score;

public Person(int num,int score,int weight){

this.num=num;

this.score=score;

this.weight=weight;

}

@Override

public int compareTo(Person other) {

if(this.score>other.score)return -1;

else if(this.score

else

return this.weight>other.weight?1:-1;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public int getWeight() {

rhttp://eturn weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

}

希望本文所述对大家的java程序设计有所帮助。

{

System.out.print(plist[i].getNum()+". " +plist[i].getScore()+" "+plist[i].getWeight()+"\n\r");

}

}

}

class Person implements Comparable{

private int num;

private int weight;

private int score;

public Person(int num,int score,int weight){

this.num=num;

this.score=score;

this.weight=weight;

}

@Override

public int compareTo(Person other) {

if(this.score>other.score)return -1;

else if(this.score

else

return this.weight>other.weight?1:-1;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public int getWeight() {

rhttp://eturn weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

}

希望本文所述对大家的java程序设计有所帮助。

else

return this.weight>other.weight?1:-1;

}

public int getNum() {

return num;

}

public void setNum(int num) {

this.num = num;

}

public int getWeight() {

rhttp://eturn weight;

}

public void setWeight(int weight) {

this.weight = weight;

}

public int getScore() {

return score;

}

public void setScore(int score) {

this.score = score;

}

}

希望本文所述对大家的java程序设计有所帮助。


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:java多线程解决生产者消费者问题
下一篇:java使用归并删除法删除二叉树中节点的方法
相关文章

 发表评论

暂时没有评论,来抢沙发吧~