Comparable 接口 常用类--基本数据类型包装类,字符串类,时间类,Math类,File类,Random类,枚举,递归目录(典型)

Comparable 接口

java 中排序算法的底层也依赖 Comparable 接口 Comparable 接口中只有一个方法: public int
compareTo(Object obj) obj 为要比较的对象 方法中,将当前对象和 obj 这个对象进行比较
如果大于返回1,
等于返回 0,
小于返回-1.
(此 处的 1 也可以是正整数,-1 也可以是负整数)。
compareTo 方法的代码也比较固定:

public int compareTo(Object o) {
        //对象强转
        People man = (People)o;
        if(man.age>this.age){
            return -1;
        }
        if(man.age<this.age){
            return 1;
        }
        return 0;
    }

常用类

在这里插入图片描述
基本数据类型–包装类
在这里插入图片描述
以Integer为例

public class TestInteger {
    public static void main(String[] args) {
        //已经被废弃,不推荐使用
//        Integer int1 = new Integer(10);
        
        // 基本类型转化成 Integer 对象
        Integer num = Integer.valueOf(10);

        System.out.println(num.intValue());

        // 字符串转化成 Integer 对象
        Integer num2 = Integer.parseInt("65");
        System.out.println(num2.intValue());

        // Integer 对象转化成字符串
        String str1 = num2.toString();
        System.out.println(str1);

        // 一些常见 int 类型相关的常量
        System.out.println("int类型数据的最大值:"+Integer.MAX_VALUE);

        //测试自动装箱
        /**相当于编译器做了 Integer x = Integer.valueOf(100);*/
        Integer x = 100;
        //自动拆箱
        /**相当于编译器做了 int y = x.intValue()*/
        int y = x;

        Integer x1 = 100;
        Integer x2 = 100;
        Integer x3 = 1000;
        Integer x4 = 1000;
        //缓存cache[]  自动装箱-128~127之间的数
        System.out.println(x1==x2);
        System.out.println(x3==x4);
        System.out.println(x1.equals(x2));
        System.out.println(x3.equals(x4));
    }
}

手写包装类

public class MyInteger {
    private int value;
    private static final int LOW = -128;
    private static final int HIGH = 127;
    private static MyInteger cache[] = new MyInteger[HIGH-LOW+1];
    //静态方法块
    static {
        for(int i = LOW;i<=HIGH;i++){
            cache[i-LOW] = new MyInteger(i);
        }
    }

    //重写toString()方法
    @Override
    public String toString() {
        return value +"";
    }

    //重写equals()方法
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyInteger myInteger = (MyInteger) o;
        return value == myInteger.value;
    }

    @Override
    public int hashCode() {
        return Objects.hash(value);
    }

    //构造器
    public MyInteger (int i){
        this.value = i;
    }

    /**
     * 静态方法  返回MyInteger对象
     * @return
     */
    public static MyInteger valueOf(int i){
        if(i>=LOW&&i<=HIGH){
            return cache[i-LOW];
        }
        return new MyInteger(i);
    }

    /**
     * 用来返回对象的value值
     * @return
     */
    public int intValue(){
        return value;
    }

    public static void main(String[] args) {
        MyInteger x = MyInteger.valueOf(100);
        System.out.println(x);
        int y = x.intValue();
        System.out.println(y);

        MyInteger x1 = MyInteger.valueOf(100);
        MyInteger x2 = MyInteger.valueOf(100);
        MyInteger x3 = MyInteger.valueOf(1000);
        MyInteger x4 = MyInteger.valueOf(1000);
        System.out.println(x1==x2);
        System.out.println(x3==x4);
        System.out.println(x1.equals(x2));
        System.out.println(x3.equals(x4));
    }
}

字符串类

String 类对象代表不可变的 Unicode 字符序列,指的是对象内部的成员变量的值无法再改变
StringBuffer 和 StringBuilder 都是可变的字符序列。
 StringBuffer 线程安全,做线程同步检查, 效率较低。
 StringBuilder 线程不安全,不做线程同步检查,因此效率较高。建议采用该类。

StringBuffer 和 StringBuilder用法几乎一致,有很多同样的方法供使用

public class TestString {
    public static void main(String[] args) {
        //不可变字符 final修饰
        String str = "123";
//        StringBuffer sb1 = new StringBuffer();
//        StringBuilder sb2 = new StringBuilder();

        /**StringBuilder 整个过程中没有产生新的字符串*/
        StringBuilder sb2 = new StringBuilder();
        for (int i = 0; i < 7; i++) {
            //追加单个字符
            sb2.append((char) ('a'+i));
        }
        //方法返回的是StringBuilder类型 可以直接追加
        sb2.append("1").append("2").append("3").append(",I can say English");
        //转化成String输出
        System.out.println(sb2.toString());

        /**StringBuffer 整个过程中没有产生新的字符串 下面方法同样适用于StringBuilder*/
        StringBuffer sb1 = new StringBuffer("说英语,因为英语很广泛");
        //插入字符串
        sb1.insert(0,"爱").insert(0,"我");
        System.out.println(sb1);
        //删除子字符串 删除从 start 开始到 end-1 为止的一段字符序列
        sb1.delete(0,2);
        System.out.println(sb1);
        //删除某个字符  此序列指定位置上的 char
        sb1.deleteCharAt(0).deleteCharAt(0);
        System.out.println(sb1);
        //获取某个位置上的字符
        System.out.println(sb1.charAt(1));

        StringBuilder sb3 = new StringBuilder("泛广很语英为因");
        //字符串逆序
        System.out.println(sb3.reverse());
    }
}

时间处理相关的类

在这里插入图片描述
在这里插入图片描述
DateFormat为例

public class TestDateFormat {
    public static void main(String[] args) throws ParseException {
        //定义格式化时间的格式
        DateFormat format = new SimpleDateFormat("yyyy-MM-dd hh:mm:ss");
        //java.text.ParseException 格式必须和你自己定义的一样
        String str = "1997-4-8 10:10:10";
        //抛异常 需要处理一下
        Date birth  = format.parse(str);
        //获得毫秒数
        System.out.println(birth.getTime());
        System.out.println(birth);

        DateFormat format2 = new SimpleDateFormat("yyyy-MM-dd hh时mm分ss秒");
        Date date2 = new Date(2222393172812L);
        String date2Str = format2.format(date2);
        System.out.println(date2Str);

        Date date3 = new Date();
        //公历计算天数和周
        DateFormat format3 = new SimpleDateFormat("今年的第D天,第w周");
        String str3 = format3.format(date3);
        System.out.println(str3);

    }

}

在这里插入图片描述

Math类

java.lang.Math提供了一系列静态方法用于科学计算:

在这里插入图片描述

Random 类

专门用来生成随机数的

public class TestRandom {
    public static void main(String[ ] args) {
        Random rand = new Random();
        //随机生成[0,1)之间的 double 类型的数据
        System.out.println(rand.nextDouble());
        //随机生成 int 类型允许范围之内的整型数据
        System.out.println(rand.nextInt());
        //随机生成[0,1)之间的 float 类型的数据
        System.out.println(rand.nextFloat());
        //随机生成 false 或者 true
        System.out.println(rand.nextBoolean());
        //随机生成[0,10)之间的 int 类型的数据
        System.out.print(rand.nextInt(10));
        //随机生成[20,30)之间的 int 类型的数据
        System.out.print(20 + rand.nextInt(10));
    }
}

File类

1.java.io.File 类:代表文件和目录,用于:读取文件、创建文件、删除文件、修改文件。

2.通过 File 对象可以访问文件的属性:
在这里插入图片描述

		//E:\code\studyTest 默认项目文件
        System.out.println(System.getProperty("user.dir"));
        //相对路径,放在默认路径下面
        File file = new File("a.txt");
        //创建文件
        file.createNewFile();
        //绝对路径 放在指定目录下面
        File file2 = new File("e:/b.txt");
        file2.createNewFile();

        System.out.println("file2 是否存在:"+file2.exists());
        System.out.println("file2 是否是目录:"+file2.isDirectory());
        System.out.println("file2 是否是文件:"+file2.isFile());
        System.out.println("file2 最后修改时间:"+new Date(file2.lastModified()));
        System.out.println("file2 的大小:"+file2.length());
        System.out.println("file2 的文件名:"+file2.getName());
        System.out.println("file2 的目录路径:"+file2.getAbsolutePath());
        //可以显示相对路径,相对路径没有找到,显示绝对路径
        System.out.println("File 的绝对路径:"+file.getPath());

3.通过 File 对象创建空文件或目录(在该对象所指的文件或目录不存在的情况下)
在这里插入图片描述

 		File f = new File("e:/c.txt");
        // 会在 d 盘下面生成 c.txt 文件
        f.createNewFile();
        // 将该文件或目录从硬盘上删除
        f.delete();

//        File f2 = new File("e:/电影/华语/大陆");
//        //目录结构中有一个不存在,则不会创建整个目录树
//        boolean flag = f2.mkdir();
//        //创建失败
//        System.out.println(flag);

        //目录结构中有一个不存在也没关系;创建整个目录 树
        File f3 = new File("e:/电影/华语/内陆");
        boolean flag2 = f3.mkdirs();
        //创建成功
        System.out.println(flag2);

枚举

1.JDK1.5 引入了枚举类型。枚举类型的定义包括枚举声明和枚举体
2.枚举的结构

enum 枚举名 { 枚举体(常量列表) }

3.枚举体就是放置一些常量

public class TestEnum {
    public static void main(String[] args) {
        System.out.println(Season.SPRING);
        //枚举元素转为数组
        System.out.println(Arrays.toString(Season.values()));

        int a = (int)(Math.random()*4);
        switch (Season.values()[a]){
            case SPRING:
                System.out.println(Season.SPRING+":春天");
                break;
            case AUTUMN:
                System.out.println(Season.AUTUMN+":夏天");
                break;
            case SUMMER:
                System.out.println(Season.SUMMER+":秋天");
                break;
            case WINTER:
                System.out.println(Season.WINTER+":冬天");
                break;
            default:
        }
    }
}

/**
 * 有关季节的枚举
 *
 */
enum Season{
    //季节
    SPRING,AUTUMN,SUMMER,WINTER
}

递归遍历目录结构和树状展现

public class PrintFileTree {
    public static void main(String[] args) {
        File file = new File(System.getProperty("user.dir"));
        printFile(file,0);

    }

    /**
     * 打印目录的方法
     * @param file 要打印的文件
     * @param level 文件的层数 打印的时候用-区分
     */
    public static void printFile(File file,int level){
        //从0开始,只有file名字,之后的递归按照层数+1
        for (int i = 0; i < level; i++) {
            System.out.print("-");
        }
        //返回当前文件名
        System.out.println(file.getName());
        if(file.isDirectory()){
            //列出所有的file子文件或者目录
            File[] files = file.listFiles();
            //循环遍历file数组
            for (File file1 : files) {
                printFile(file1,level+1);
            }
        }
    }
}

在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值