The enumeration control is used to compare TreeSet content ordering (implementing interface comparator) and randomly generating a Chinese character.

Source: Internet
Author: User
Tags abs comparison getcolor int size integer tostring
Randomly generating a Chinese character/** * Randomly generating a Chinese character * @return * @throws unsupportedencodingexception/public static String Createstr () throws  unsupportedencodingexception {String str = null; int hightpos, lowpos;//define high-low Random Random = new Random (); Hightpos = (176 + Math.Abs (Random.nextint (39)))//Get high value Lowpos = (161 + math.abs (Random.nextint (93)))//Get low value byte[] B = new byte[
2];
B[0] = (new Integer (Hightpos). Bytevalue ());
B[1] = (new Integer (Lowpos). Bytevalue ());
str = new String (b, "GBk");//Convert to Chinese return str;
Sort import java.util.Comparator;
Import Java.util.Set;
     
     
Import Java.util.TreeSet; Class game{private String Materia public string Getmateria () {return materia.} public void Setmateria (String materia)
{This.materia = Materia;} public string GetColor () {return color;} public void SetColor (String color) {This.color = color,} public string Getsha PE () {return shape} is public void Setshape (String shape) {this.shape = shape;} public int getsize () {return size;} PU Blic void setSize (int size) {this.size = size;} private String color;
Private String shape;
private int size; Public Game (String materia,string color,string shape,int size) {this.materia=materia; this.color=color; this.shape=
Shape
This.size=size;
     
     
@Override public String toString () {return materia+ ">" +color+ ">" +shape+ ">" +size;} /** * Referee class is also called comparator. Enumeration for better control of comparison content * @author Lenovo * More highlights: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/ sjjg/* */class K1 implements comparator{public enum Materiaenum {//wood is better than steel better than alloy to plastic wood,steel,alloy,plastic;} public int compare (object O1, Object O2) {//Only Game type has the meaning of comparison, if it is not that type let it be equal do not add if (O1 instanceof) && (Game O2 Gam
e)) {Game g1= (Game) O1;
Game g2= (Game) O2;
int x=0; for (int i = 0; i < materiaenum.values (). length; i++) {if (G1.getmateria (). Equals (Materiaenum.values () [I].tostring ())
     
) {x=i; break;}}
int y=0; for (int i = 0; I <materiaenum.values (). length; i++) {if (G2.getmateria (). EQuals (Materiaenum.values () [I].tostring ())) {y=i; break;}}
int m=x-y; if (m!=0) {return m;} else {int c=g1.getcolor (). CompareTo (G2.getcolor ()), if (c!=0) {return C;} else {int s=g1.getsh
Ape (). CompareTo (G2.getshape ());
if (s!=0) return s;
Return G1.getsize ()-g2.getsize ();
}} return 0; } public class Treesetgame {/** * @param args/@SuppressWarnings ("unchecked") public static void Ma
In (string[] args) {Set gset=new treeset<game> (New K1 ()); Gset.add (New Game ("Wood", "Red", "rectangle", 1));
Gset.add (New Game ("Steel", "Red", "Rectangle", 2));
Gset.add (New Game ("Wood", "Red", "Rectangle", 3));
Gset.add (New Game ("Plastic", "Red", "Rectangle", 3));
System.out.println (Gset); }
     
     
}

Author: csdn Blog u010026901

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.