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:
Java code
Public class Mystring {
Public Static voidMain (string[] args) {
String []a={"Morning","Noon","Afternoon","Evening","Evening"};
Person[] b={ newperson ("small white",newperson ("Black",19 ),
New Person ("Little Red",+),newperson ("Little Purple")};
Arrays.sort (a);
for (inti=0; i<a.length;i++)
System.out.print (A[i]);
System.out.print ("\ r \ n");
Arrays.sort (b);
for (inti=0; i<b.length;i++)
System.out.println (B[i]);
}
}
Custom Classes
class person {
Public String name;
Public intage;
Public Person (String name,intage) {
this. name=name;
this. age=age;
}
}
Program Run Result:
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
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:
Java code
Public class Mystring {
Public Static voidMain (string[] args) {
String []a={"Morning","Noon","Afternoon","Evening","Evening"};
Person[] b={ newperson ("small white",newperson ("Black",19 ),
New Person ("Little Red",+),newperson ("Little Purple")};
Arrays.sort (a);
for (inti=0; i<a.length;i++)
System.out.print (A[i]);
System.out.print ("\ r \ n");
Arrays.sort (b);
for (inti=0; i<b.length;i++)
System.out.println (B[i]);
}
}
Custom Classes
class Person implementscomparable<person>{
Public String name;
Public intage;
Public Person (String name,intage) {
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 intcompareTo (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 + "]";
}
}
Operation Result:
To implement the comparer comparable interface for the custom person, you can call Arrays.sort () to sort.
there is another way to achieve Comparator interface.
Java code
Public class Mystring {
Public Static voidMain (string[] args) {
String []a={"Morning","Noon","Afternoon","Evening","Evening"};
Person[] b={ newperson ("small white","newPerson" ("small Black"),
New Person ("Little Red",+),newperson ("Little Purple")};
Arrays.sort (a);
for (inti=0; i<a.length;i++)
System.out.print (A[i]);
System.out.print ("\ r \ n");
Arrays.sort (b,newpersoncomparator ());
for (inti=0; i<b.length;i++)
System.out.println (B[i]);
}
}
Custom Classes
class person {
Public String name;
Public intage;
Public Person (String name,intage) {
this. name=name;
this. age=age;
}
@Override
Public String toString () {
return "person [name="+ name + ", age="+ Age + "]";
}
}
class Personcomparator implementscomparator<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 intCompare (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;
}
}
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
Java code
Custom class person implementation cloneable interface
class Person implementscloneable {
Public String name;
Public intage;
Public Person (String name,intage) {
this. name=name;
this. age=age;
}
Overriding the Clone method of object
@Override
protected Object Clone () throwsClonenotsupportedexception
{
return Super. Clone ();
}
}
Cloned calls:
Java code
-
Span>public static void main (string[] args) {
-
-
person temp= new person ( "Wang Nima" , + );
-
-
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 ();
-
}
-
-
}
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 better.
technology sharing: www.kaige123.com
Java object comparer and cloning