Java java-14.4 provides an understanding of the practical function of the array (2)

Label Java
632 people read comments(0) Collection Report
Classification:

And then the last chapter, we continue to introduce the Java provides an array of useful functions.

Comparison of 3 elements Comparator

Com.ray.ch14 package;

Java.util.Arrays import;
Java.util.Comparator import;

Class Test public {
Static void main public (args String[]) {
MyClasses new = MyClass[3] MyClass[];
MyClass1 MyClass = MyClass new ();
MyClass2 MyClass = MyClass new ();
MyClass3 MyClass = MyClass new ();
MyClass1.setId (3);
MyClass2.setId (2);
MyClass3.setId (1);
MyClass1.setName ("myClass1");
MyClass2.setName ("myClass2");
MyClass3.setName ("myClass3");
MyClasses[0] = myClass1;
MyClasses[1] = myClass2;
MyClasses[2] = myClass3;
Arrays.sort (myClasses, MyComparator new ());
System.out.println (Arrays.toString (myClasses));
}
}

MyComparator implements Comparator<MyClass> class {

@Override
Int compare public (myClass1 MyClass, myClass2 MyClass) {
MyClass1.getId return () > myClass2.getId () 1: 0;
}
}

MyClass class {

Int ID private = 0;

String name private ="";

String getName public () {
Name return;
}

Void setName public (name String) {
This.name = name;
}

Int getId public () {
ID return;
}

Void setId public (id int) {
This.id = id;
}

@Override
String toString public () {
Return "id:" + ID + "&name:" + name;
}
}

Output:

[id:1&name:myClass3, id:2&name:myClass2, id:3&name:myClass1]

To use the comparator, we must implement the interface of the comparator, is mainly the compare method to achieve, so that you can custom class contrast, above we used is the ID of the contrast, ID smaller front row, the row behind.


4 sort of array Arrays.sort

Examples of the upper side of the interface, we continue to expand, mainly through the Array.sort to sort the array, the above example is also described.


5 search in an ordered array.

We modify the above example:

Com.ray.ch14 package;

Java.util.Arrays import;
Java.util.Comparator import;

Class Test public {
Static void main public (args String[]) {
MyClasses new = MyClass[3] MyClass[];
MyClass1 MyClass = MyClass new ();
MyClass2 MyClass = MyClass new ();
MyClass3 MyClass = MyClass new ();
MyClass1.setId (3);
MyClass2.setId (2);
MyClass3.setId (1);
MyClass1.setName ("myClass1");
MyClass2.setName ("myClass2");
MyClass3.setName ("myClass3");
MyClasses[0] = myClass1;
MyClasses[1] = myClass2;
MyClasses[2] = myClass3;
Arrays.sort (myClasses, MyComparator new ());
System.out.println (Arrays.toString (myClasses));
System.out.println ("pos: myClass2's"
+ Arrays.binarySearch (myClasses, myClass2, MyComparator new ());
}
}

MyComparator implements Comparator<MyClass> class {

@Override
Int compare public (myClass1 MyClass, myClass2 MyClass) {
MyClass1.getId return () > myClass2.getId () 1: 0;
}
}

MyClass class {

Int ID private = 0;

String name private ="";

String getName public () {
Name return;
}

Void setName public (name String) {
This.name = name;
}

Int getId public () {
ID return;
}

Void setId public (id int) {
This.id = id;
}

@Override
String toString public () {
Return "id:" + ID + "&name:" + name;
}
}

Output:

[id:1&name:myClass3, id:2&name:myClass2, id:3&name:myClass1]
Pos:1 myClass2's

We inside joined the binarysearch this dichotomy search algorithm, use it to find myClass2 this object in an array position.

Note: binarysearch to search an ordered sequence is no problem, but search for the sequence of random, there will be baffling problem (mainly the wrong location).


Summary: in this chapter we mainly introduce the practical function of the array provided by Java.


This chapter is here, thank you.

= = = = =

Catalog



top
Zero
tread
Zero
Guess you're looking for
View comments
* the above user comments only represent their personal views, does not represent the views or position of the CSDN website
    personal data
    • Visit201332 times
    • Integral:Five thousand five hundred and fifty-one
    • Grade
    • Rank:2526th name
    • Original341
    • Reproduced:1
    • Translation:2
    • Comments:131
    Blog column
    Latest comments