java結構體排序


import java.util.Arrays;
import java.util.Scanner;


public class aa {
    public static class stu implements Comparable{
        int a;
        int b;
        stu(){}//無參構造
        stu(int a,int b){
            this.a = a;
            this.b = b;
        }
        @Override
        public int compareTo(Object o){
            stu s = (stu) o;
            return a - s.a;
        }
    }
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        stu[] no = new stu[4];
        no[0] = new stu(2,3);
        no[1] = new stu(5,99);
        no[2] = new stu(-43,88);
        no[3] = new stu(66,30);
        Arrays.sort(no);
        for(int i = 0;i < no.length;i++){
            System.out.println(no[i].a);
        }
        return;
    }

}

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM