使用Java如何對復雜的數據類型排序和比大小
一.對復雜的數據類型比大小
假如我們現在有個學生類,并且我們實例化出了倆個學生對象,他們各自有各自的名字和年齡屬性,我們如何對他們進行比大小操作呢?
class Student {
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
}
public class Test{
public static void main(String[] args) {
Student student1 = new Student("張三",20);
Student student2 = new Student("李四",23);
if (student1 > student2) {
System.out.println("student1 > student2");
}else {
System.out.println("student1 < student2");
}
}
}我們可以看見編譯器的報錯提示,這是因為Java提供的運算符號只能識別操作簡單的數據類型,對于我們自定義的Student類是無法識別的

Comparable接口
在這種情況下,我們就可以使用我們之前的講解的接口的知識,我們可以調用Comparable接口,然后重寫其中的compareTo方法,在使用接口的時候需要注意聲明你需要比較的類型,也就是接口后尖括號內的內容
compareTo方法

我們在這里對接口中的compareTo方法進行重寫后,在main方法中進行調用
class Student implements Comparable <Student> {
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
@Override
public int compareTo(Student o) {
return this.name.compareTo(o.name);
}
}
public class Test{
public static void main(String[] args) {
Student student1 = new Student("張三",20);
Student student2 = new Student("李四",23);
if (student1.compareTo(student2) > 0) {
System.out.println("student1 > student2");
}else {
System.out.println("student1 <= student2");
}
}
}我們也可以根據年齡進行比大小,只需要重新重寫這個方法就可以了
@Override
public int compareTo(Student o) {
return this.age-o.age;
}二.對復雜數據類型排序
假如我們現在有一個學生類數組,我們使用Arrays.sort對他進行排序
public class Test{
public static void main(String[] args) {
Student[] students = new Student[3];
Student student1 = new Student("張三",20);
Student student2 = new Student("李四",23);
Student student3 = new Student("王五",25);
students[0] = student1;
students[1] = student2;
students[2] = student3;
Arrays.sort(students);
}
}我們會發(fā)現報錯信息如下,原因就是對于這種復雜的數據類型,如果我們要讓編譯器來排序,那我們就需要給他排序規(guī)則,很顯然這里編譯器是沒有讀取到任何的排序規(guī)則的

我們點擊錯誤信息,打開源碼觀察會發(fā)現,編譯器這里還是用到了Comparable接口

那我們還是像剛才一樣調用Comparable接口,然后我們給出明確的排序規(guī)則,再寫一個排序方法,就可以正常對復雜數據排序了
import java.util.Arrays;
class Student implements Comparable <Student> {
public String name;
public int age;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
// @Override
// public int compareTo(Student o) {
// return this.name.compareTo(o.name);
// }
@Override
public int compareTo(Student o) {
return this.age-o.age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
public static void mySort(Comparable[] comparables) {
for (int i = 0; i < comparables.length-1; i++) {
for (int j = 0; j < comparables.length-1-i; j++) {
//if(comparables[j] > comparables[j+1]) {
if(comparables[j].compareTo(comparables[j+1]) > 0) {
//交換
Comparable tmp = comparables[j];
comparables[j] = comparables[j+1];
comparables[j+1] = tmp;
}
}
}
}
}
public class Test{
public static void main(String[] args) {
Student[] students = new Student[3];
Student student1 = new Student("張三",20);
Student student2 = new Student("李四",23);
Student student3 = new Student("王五",25);
students[0] = student1;
students[1] = student2;
students[2] = student3;
mySort(students);
System.out.println(Arrays.toString(students));
}
}三.總結
當我們需要對復雜的數據類型進行排序或者比大小的時候,我們就可以使用Comparable接口,然后重寫其中的compareTo方法,然后就可以直接使用compareTo方法進行排序了,又或者是通過其他方法來調用compareTo方法來對復雜類型的數組進行排序
到此這篇關于使用Java如何對復雜的數據類型排序和比大小的文章就介紹到這了,更多相關Java數據類型排序和比大小內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
SpringBoot獲取當前運行環(huán)境三種方式小結
在使用SpringBoot過程中,我們只需要引入相關依賴,然后在main方法中調用SpringBootApplication.run(應用程序啟動類.class)方法即可,那么SpringBoot是如何獲取當前運行環(huán)境呢,接下來由小編給大家介紹一下SpringBoot獲取當前運行環(huán)境三種方式,需要的朋友可以參考下2024-01-01
java中VO PO DTO POJO BO DO對象的應用場景及使用
文章介紹了Java開發(fā)中常用的幾種對象類型及其應用場景,包括VO、PO、DTO、POJO、BO和DO等,并通過示例說明了它們在不同場景下的應用2025-01-01
ConcurrentMap.putIfAbsent(key,value)用法實例
這篇文章主要介紹了ConcurrentMap.putIfAbsent(key,value)用法實例,分享了相關代碼示例,小編覺得還是挺不錯的,具有一定借鑒價值,需要的朋友可以參考下2018-02-02

