I. Comparator comparable and comparator
The previous blog introduced the Tool class arrays tool class. We can call the Arrays.sort () function on an array of basic types to sort the array. Sorting operations are often used in daily development. Then can the string class and custom classes be sorted using Arrays.sort (). Let's try it as follows:
public class Mystring {
public static void Main (string[] args) {
String []a={"Morning", "noon", "Afternoon", "evening", "Evening"};
Person[] b={New Person ("small white", new Person ("black", 19),
New person ("Little Red", +), new person ("Little Violet", 20)};
Arrays.sort (a);
for (int i=0;i<a.length;i++)
System.out.print (A[i]);
System.out.print ("\ r \ n");
Arrays.sort (b);
for (int i=0;i<b.length;i++)
System.out.println (B[i]);
}
}
Custom Classes
Class Person {
public String name;
public int age;
Public person (String Name,int age) {
This.name=name;
This.age=age;
}
}
Copy Code
program run Result:
650) this.width=650; "id=" aimg_275 "src=" http://techfoxbbs.com/data/attachment/forum/201505/21/ 105254iibwc84wnhsqbjss.png "class=" Zoom "width=" "alt=" 105254iibwc84wnhsqbjss.png "/>
We find that string can call the Arrays.sort () function, and we customize the person class to make an error, looking at the wrong keyword comparable
Open String Source
650) this.width=650; "id=" aimg_276 "src=" http://techfoxbbs.com/data/attachment/forum/201505/21/ 105311pdudux7895h5j5a1.png "class=" Zoom "width=" "alt=" 105311pdudux7895h5j5a1.png "/>"
By introducing the comparator comparable, we use the comparable interface below to implement the person using Arrays.sort () to sort the ascending order by age.
The comparable interface is able to sort the objects that implement its classes, using the CompareTo function primarily for sorting. The CompareTo function returns the int type, which returns -1,0,1, which corresponds to less than, equal to, greater than.
Change the code to:
public class Mystring {
public static void Main (string[] args) {
String []a={"Morning", "noon", "Afternoon", "evening", "Evening"};
Person[] b={New Person ("small white", new Person ("black", 19),
New person ("Little Red", +), new person ("Little Violet", 20)};
Arrays.sort (a);
for (int i=0;i<a.length;i++)
System.out.print (A[i]);
System.out.print ("\ r \ n");
Arrays.sort (b);
for (int i=0;i<b.length;i++)
System.out.println (B[i]);
}
}
Custom Classes
Class Person implements comparable<person>{
public String name;
public int age;
Public person (String Name,int age) {
This.name=name;
This.age=age;
}
Comparator comparison function
/**
* Returns 1 if the current object is less than the comparison object
* Returns 0 if the current object equals the comparison object
* Returns 1 if the current object is greater than the comparison object
*/
@Override
public int compareTo (person o) {
Object is empty, throws a null pointer exception
if (o==null)
throw new NullPointerException ();
if (this.age<o.age)
return-1;
if (this.age>o.age)
return 1;
return 0;
}
Overriding the ToString Method
@Override
Public String toString () {
return "person [name=" + name + ", age=" + Age + "]";
}
}
Copy Code
650) this.width=650; "id=" aimg_277 "src=" http://techfoxbbs.com/data/attachment/forum/201505/21/ 105342evxcx50oz0x0iaiz.png "class=" Zoom "width=" 391 "alt=" 105342evxcx50oz0x0iaiz.png "/>
To implement the comparer comparable interface for the custom person, you can call Arrays.sort () to sort.
There is another way to implement the comparator interface.
public class Mystring {
public static void Main (string[] args) {
String []a={"Morning", "noon", "Afternoon", "evening", "Evening"};
Person[] b={New Person ("small white", new Person ("black", 19),
New person ("Little Red", +), new person ("Little Violet", 20)};
Arrays.sort (a);
for (int i=0;i<a.length;i++)
System.out.print (A[i]);
System.out.print ("\ r \ n");
Arrays.sort (B,new personcomparator ());
for (int i=0;i<b.length;i++)
System.out.println (B[i]);
}
}
Custom Classes
Class Person {
public String name;
public int age;
Public person (String Name,int age) {
This.name=name;
This.age=age;
}
@Override
Public String toString () {
return "person [name=" + name + ", age=" + Age + "]";
}
}
Class Personcomparator implements comparator<person>{
Comparator comparison function
/**
* Returns 1 if the current object is less than the comparison object
* Returns 0 if the current object equals the comparison object
* Returns 1 if the current object is greater than the comparison object
*/
@Override
public int Compare (person O1, person O2) {
Object is empty, throws a null pointer exception
if (o1==null| | O2==null)
throw new NullPointerException ();
if (o1.age<o2.age)
return-1;
if (o1.age>o2.age)
return 1;
return 0;
}
}
Copy Code
Two. Cloning clone of an object
Copies a copy of an object, called the object's cloning technique. Cloning an object is a two-step move.
1. Implement cloneable Tag interface
Cloneable is a markup interface that does not have any methods defined by this interface. Just as a token to the virtual machine.
2. Overriding the Clone method of object
Custom class person implementation cloneable interface
Class Person implements Cloneable {
public String name;
public int age;
Public person (String Name,int age) {
This.name=name;
This.age=age;
}
Overriding the Clone method of object
@Override
Protected Object Clone () throws Clonenotsupportedexception
{
return Super.clone ();
}
}
Copy Code
Cloned calls:
public static void Main (string[] args) {
Person Temp=new person ("Wang Nima", 18);
try {
Person ok= (person) temp.clone ();//Convert the object class to person,
System.out.println (ok.name);//Output Wang Nima
} catch (Clonenotsupportedexception e) {
E.printstacktrace ();
}
}
Copy Code
This can be done to clone an object, when we want to create a series of similar objects, you can consider the use of cloning technology can get better performance, than you create a better
"Java object comparer and cloning"