The example of this article tells the way that Java realizes the method of giving the score array to get the corresponding ranking array. Share it for everyone for your reference. The specific implementation method is as follows:
Package test01;/** * Give the score array, get the corresponding ranking array * column if there are: score = {4,2,5,4} * then output: rank = {2,3,1,2} * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * /Import java.util.arrayList; Import Java.util.Collections; Import Java.util.List; Public Class Scorerank {// Output Public Static Void Show (int [] s) {for (int x: s) system. Out.print (x); System.out.println ();} // Get the presented public static int [] scorerank (int [] score) {int [] temp = new int [score.Length]; ArrayList (); for (int x: score) // Add element (no repeated) if (! Lis.contains (x)) lis.add (x); Collections.sort (lis); // Sort the colleg from small to large. .reverse (lis); // Sorting from large to small sort for (int i = 0; i <score.length; i ++) // The bidding starts from 0 temp [i] = lis.indexof (score [i])+ 1; // So: Normal ranking = obtaining a bidding+ 1 RETURN TEMP;} Public Static void Main (string [] args) {int [] score = {4,2,5,4}; // 3,1,2} int [] rank = scorerank (score); // Get the name System.out.print ("Original score:"); show (score); System.out.print ; show (rank);}}
The results are as follows:
Original score: 4254
Correspondence: 2312
It is hoped that this article is helpful to everyone's Java program design.