Java-14.4 Java array functions (2)
Next, we will continue to introduce the array functions provided by Java.
3. Comparison of elements Comparator
package com.ray.ch14;import java.util.Arrays;import java.util.Comparator;public class Test {public static void main(String[] args) {MyClass[] myClasses = new MyClass[3];MyClass myClass1 = new MyClass();MyClass myClass2 = new MyClass();MyClass myClass3 = new MyClass();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, new MyComparator());System.out.println(Arrays.toString(myClasses));}}class MyComparator implements Comparator
{@Overridepublic int compare(MyClass myClass1, MyClass myClass2) {return myClass1.getId() > myClass2.getId() ? 1 : 0;}}class MyClass {private int id = 0;private String name = "";public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "id:" + id + "&name:" + name;}}
Output:
[Id: 1 & name: myClass3, id: 2 & name: myClass2, id: 3 & name: myClass1]
To use Comparator, you must implement the Comparator interface, which is mainly the implementation of the compare method. In this way, you can compare custom classes. We use the id comparison above, the front of a row with a small id and the back of a large row.
4. sort the array Arrays. sort
In the above example, we continue to expand the interface, mainly by sorting the Array through Array. sort. The above example also describes.
5. Search in an ordered array.
Let's modify the example above:
package com.ray.ch14;import java.util.Arrays;import java.util.Comparator;public class Test {public static void main(String[] args) {MyClass[] myClasses = new MyClass[3];MyClass myClass1 = new MyClass();MyClass myClass2 = new MyClass();MyClass myClass3 = new MyClass();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, new MyComparator());System.out.println(Arrays.toString(myClasses));System.out.println("myClass2's pos:"+ Arrays.binarySearch(myClasses, myClass2, new MyComparator()));}}class MyComparator implements Comparator
{@Overridepublic int compare(MyClass myClass1, MyClass myClass2) {return myClass1.getId() > myClass2.getId() ? 1 : 0;}}class MyClass {private int id = 0;private String name = "";public String getName() {return name;}public void setName(String name) {this.name = name;}public int getId() {return id;}public void setId(int id) {this.id = id;}@Overridepublic String toString() {return "id:" + id + "&name:" + name;}}
Output:
[Id: 1 & name: myClass3, id: 2 & name: myClass2, id: 3 & name: myClass1]
MyClass2's pos: 1
We added the binary search algorithm binarySearch to find the position of the object myClass2 in the array.
Note: binarySearch is fine for searching ordered sequences, but for searching unordered sequences, there will be inexplicable problems (mainly location errors ).
Summary: This section describes the array functions provided by Java.
This chapter is here. Thank you.