Java使用Comparable解決排序問題
本文實(shí)例講述了Java使用Comparable解決排序問題的方法。分享給大家供大家參考。具體實(shí)現(xiàn)方法如下:
一次舉重競(jìng)賽的比賽規(guī)則是:選手的成績以成功舉起的總重量來排序,舉起總重量多的排在前面;當(dāng)舉起總重量相同時(shí),按照體重來排序,體重輕的排在前面;要求程序讀取數(shù)據(jù)文件作為輸入,并按照上述規(guī)則排序后,打印出選手編號(hào);數(shù)據(jù)文件說明如下:現(xiàn)有5名選手,其選手編號(hào)、成功舉起的總重量及其體重如數(shù)據(jù)文件data4.txt,樣例內(nèi)容為:
<p> <no>1</no> <lw>140</lw> <bw>54</bw> </p> <p> <no>2</no> <lw>155</lw> <bw>53</bw> </p> <p> <no>3</no> <lw>140</lw> <bw>42</bw> </p> <p> <no>4</no> <lw>140</lw> <bw>55</bw> </p> <p> <no>5</no> <lw>130</lw> <bw>46</bw> </p>
首先我要解決的是文件解析的問題:
如何把文件內(nèi)容解析成想要的數(shù)據(jù):即提取出每個(gè)選手的編號(hào),成績和體重
我用一個(gè)實(shí)體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<Person> list=new ArrayList<Person>();
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().trim();
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 (IOException e) {
e.printStackTrace();
}
Person[] plist=new Person[list.size()];
list.toArray(plist);
Arrays.sort(plist);
for(int i=0;i<plist.length;i++)
{
System.out.print(plist[i].getNum()+". " +plist[i].getScore()+" "+plist[i].getWeight()+"\n\r");
}
}
}
class Person implements Comparable<Person>{
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<other.score) return 1;
else
return this.weight>other.weight?1:-1;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public int getWeight() {
return weight;
}
public void setWeight(int weight) {
this.weight = weight;
}
public int getScore() {
return score;
}
public void setScore(int score) {
this.score = score;
}
}
希望本文所述對(duì)大家的java程序設(shè)計(jì)有所幫助。
相關(guān)文章
Java超詳細(xì)講解設(shè)計(jì)模式中的命令模式
命令模式是將一個(gè)請(qǐng)求封裝為一個(gè)對(duì)象,從而可用不同的請(qǐng)求對(duì)客戶進(jìn)行參數(shù)化,對(duì)請(qǐng)求排隊(duì)或者對(duì)請(qǐng)求做日志記錄,以及可以支持撤銷的操作2022-04-04
SpringCloud Gateway加載斷言predicates與過濾器filters的源碼分析
這篇文章主要介紹了SpringCloud Gateway加載斷言predicates與過濾器filters的詳細(xì)過程,本文通過源碼給大家解析的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2021-05-05
Mybatis代碼生成器Mybatis Generator(MBG)實(shí)戰(zhàn)詳解
本文我們主要實(shí)戰(zhàn)Mybatis官方的代碼生成器:Mybatis Generator(MBG),掌握它以后,可以簡(jiǎn)化大部分手寫代碼,我們只需要寫復(fù)雜邏輯代碼,需要的朋友可以參考下2023-05-05
java實(shí)現(xiàn)簡(jiǎn)易的五子棋游戲
這篇文章主要為大家詳細(xì)介紹了java實(shí)現(xiàn)簡(jiǎn)易的五子棋游戲,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-06-06
JavaWeb 實(shí)現(xiàn)驗(yàn)證碼功能(demo)
在 WEB-APP 中一般應(yīng)用于:登錄、注冊(cè)、買某票、秒殺等場(chǎng)景,大家都接觸過這個(gè)驗(yàn)證碼操作,今天小編通過實(shí)例代碼給大家講解javaweb實(shí)現(xiàn)驗(yàn)證碼功能,需要的朋友參考下2017-02-02
SpringBoot2之PUT請(qǐng)求接收不了參數(shù)的解決方案
這篇文章主要介紹了SpringBoot2之PUT請(qǐng)求接收不了參數(shù)的解決方案,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-07-07

