Java uses comparable to troubleshoot sorting problems _java

Source: Internet
Author: User
Tags arrays comparable

This article illustrates the way Java uses comparable to solve sorting problems. Share to everyone for your reference. The implementation methods are as follows:

The rules of a weightlifting competition are: The contestants ' scores are sorted by the total weight that is successfully lifted, and the total weight is raised to the front; when the total weight is lifted, the weight is sorted in the front; requires the program to read the data file as input, and after sorting by the rules above, print out the player number The data file is as follows: The number of 5 contestants, their player numbers, the total weight of their success and their weight such as data file Data4.txt, the sample content is:

<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>

The first thing I'm going to solve is file resolution issues:

How to parse the contents of the file into the desired data: that is, to extract the number, grade and weight of each contestant
I encapsulate these attributes with an entity person

Overall code:

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;
(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; Son (int num,int score,int weight) {this.num=num; this.score=score; this.weight=weight;} @Override public int compareTo (P
  Erson 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 Weig
Ht public void setweight (int weight) {this.weight = weight;} public int Getscore () {return score;} public void SetScore (int score)
{this.score = score;}
 }

I hope this article will help you with your Java programming.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.