昆仑山:眼中无形心中有穴之穴人合一

夫君子之行,静以修身,俭以养德;非澹泊无以明志,非宁静无以致远。夫学须静也,才须学也;非学无以广才,非志无以成学。怠慢则不能励精,险躁则不能冶性。年与时驰,意与岁去,遂成枯落,多不接世。悲守穷庐,将复何及!

 

java接口进阶版:Comparable 接口和Comparator 接口

Comparable 接口

Circle

public class Circle implements Comparable<Circle> {
    private double radius;

    public Circle() {
        this(0.0);
    }

    public Circle(double radius) {
        this.radius = radius;
    }

    public double getRadius() {
        return radius;
    }

    public void setRadius(double radius) {
        this.radius = radius;
    }

//    求周长
    public double getPerimeter(){
        return 2*Math.PI*getRadius();
    }
//    求面积
    public double getArea(){
        return Math.pow(getRadius(),2)*Math.PI;
    }

    @Override
    public int compareTo(Circle o) {
        if (getArea()>o.getArea()){
            return 1;
        }else if(getArea()<o.getArea()){
            return -1;
        }else {
            return 0;
        }
    }
}


Comparable接口测试

Circle circle=new Circle(10);
        System.out.println(circle.getPerimeter());
        System.out.println(circle.getArea());
        System.out.println("************************************************");
        Circle[]circles=new Circle[]{
                new Circle(3.2),
                new Circle(1.6),
                new Circle(5.1),
        };
        for (Circle c : circles) {
            System.out.println(c.getArea());
        }
        for (int i = 0; i < 20; i++) {
            System.out.print("*\t");
        }
        System.out.println();

        System.out.println(circles[0].compareTo(circles[1]));
//       对circles数组中3个Circle对象排序
        Arrays.sort(circles);

        for (Circle c : circles) {
            System.out.println(c.getArea());
        }

Comparator 接口

LengthComparator

import java.util.Comparator;

// 按照长度比较字符串
public class LengthComparator implements Comparator<String> {
    @Override
    public int compare(String first, String second) {
        return first.length() - second.length();
    }
}

Compatator接口测试

import sun.rmi.runtime.Log;

import java.util.*;

public class Driver {
    public static void main(String[] args) {


        String motto="What I saw make me very sad";
        StringTokenizer parser=new StringTokenizer(motto);//字符解析器创建对象
/*
        while (parser.hasMoreElements()){
            System.out.print("\t"+parser.nextToken());
        }
*/


        String[] arrays=new String[parser.countTokens()];//创建一个数组
        // 将分割的单词 保存到数组里
        int i=0;
        while (parser.hasMoreTokens()){
            arrays[i++]=parser.nextToken();
        }

        //打印数组里面元素
        for (String array : arrays) {
            System.out.print("\t"+array);
        }
/*********************************************************************/
        //方法1
/*        Arrays.sort(arrays,new LengthComparator()); //按照长度比较字符串

        System.out.println(); //换行

        for (String array : arrays) {
            System.out.print("\t"+array);
        }*/

        //方法2
        Arrays.sort(arrays, new Comparator<String>() {
            @Override
            public int compare(String first, String second) {
                return first.length()-second.length();  // 按照长度比较字符串
            }
        });
        System.out.println();
        for (String array : arrays) {
            System.out.print("\t"+array);
        }

        //方法2 演化版
        Arrays.sort(arrays, (String first,String second)->{
            return first.length()-second.length(); // 按照长度比较字符串
        });

        System.out.println();
        for (String array : arrays) {
            System.out.print("\t"+array);
        }

        //方法3 Lamda表达式

        Arrays.sort(arrays,(String first,String second)->first.length()-second.length());
        System.out.println();
        for (String array : arrays) {
            System.out.print("\t"+array);
        }


    }
}


posted on 2019-05-24 11:34  Indian_Mysore  阅读(154)  评论(0编辑  收藏  举报

导航