Java常用类库

本文介绍了Java中的关键类和接口,包括StringBuffer的操作方法、CharSequence接口的使用、AutoCloseable接口的功能、Runtime类的特性、System类的时间测量方法、Math类的随机数生成方式、大数据处理类BigInteger和BigDecimal的应用、Date类和SimpleDateFormat类的日期处理技巧、正则表达式的使用方法、以及Arrays类的数组操作功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

StringBuffer

reverse()使这个字符序列被序列的反转所取代。
insert(int offset, String str)将字符串插入到这个字符序列中。
append(String str)将指定的字符串,这个字符序列。
delete(int start, int end)删除的字符在字符串序列分析。

StringBuffer是线程安全的,因为他是用synchronized修饰的

CharSequence接口

一个描述字符串标准接口。他是String,Stringbuffer,StringBuilder的父接口,可以进行向上转型对父类的实例化

CharSequence str = "www.baidu.com";

AutoCloseable接口

结合异常处理结果在资源操作完成后实现自动释放功能->资源的自动释放

try (OutputStream out = new FileOutputStream("",true)){
            out.write("a".getBytes());
        }catch (Exception e){
            e.printStackTrace();
        }

OutputStream是AutoCloseable的子类

Runtime类

描述的是运行时的状态,每一个JVM进程中都会提供一个唯一的Runtime类实例化对象,可以通过Runtime类获取jvm有关的运行状态,但是Runtime类的构造方法是封装的了(单例设计模式),所以就必须需要用他发getRuntime()方法来获取对象

//获取对象
Runtime runtime = Runtime.getRuntime();
System.out.println("获取处理器数据:" + runtime.availableProcessors());
System.out.println("获取最大可用内存:" + runtime.maxMemory());
//运行垃圾回收
runtime.gc();
System.out.println("获取总共可用用的内存:" + runtime.totalMemory());
System.out.println("获取空闲的内存:" + runtime.freeMemory());

System类

public class Demo {
    public static void main(String[] args) {
        long start = System.currentTimeMillis(); //获取当前系统时间可以用来做定时器
        String a = "";
        for (int i = 0;i<3000;i++){
            a += i;
        }
        long end = System.currentTimeMillis();
        System.out.println("程序运行时间" + (end - start) + ".ms");
    }
}

Math类

Random 随机数通过nextInt来获取随机数

public class Demo {
    public static void main(String[] args) {
        Random random = new Random();
        random.nextInt(50);
    }
}

大数据处理类

  • 处理整数 bigInteger
  • 处理浮点数 BigDecimal

Date类

public class Demo {
    public static void main(String[] args) {
        Date date = new Date(); //获取实例化对象
        long time = date.getTime(); //转变为Long型
    }
}

SimpleDateFormat类

public class Demo {
    public static void main(String[] args) throws Exception{
        Date date = new Date();
        //y是年 M是月 d天 H小时 m分 s秒 S毫秒
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss:SS");
        //将日期格式化为字符串数据
        System.out.println(dateFormat.format(date));
        String birth = "1988-12-15 15:45:07:027";
        //将日期格式化为字符串
        System.out.println(dateFormat.parse(birth));
    }
}

正则表达式

参考文献:https://www.cnblogs.com/dreamingbaobei/p/9717234.html

String类对正则表达式的支持

  1. matches 与指定正则匹配
  2. replaceAll 替换满足指定正则的全部内容
  3. split 按满足正则切分

java.util.regex包对正则的支持

包中的核心的正则操作是Pattern(正则模式),Matcher(匹配)

  • Matcher类
matches()boolean试图将整个区域与模式匹配。
replaceAll(String replacement)String取代用替换字符串的模式匹配的输入序列,每个子序列。
find()boolean试图找到匹配模式的输入序列中的下一个序列。
  • Pattern
matches(CharSequence input)Matcher编译给定的正则表达式,并试图匹配给定的输入反对它。
split(CharSequence input)String[]将给定的输入序列拆分为这个模式的匹配。
compile(String regex)static Pattern将给定的正则表达式编译成模式。

Arrays数组操作类

Arrays.sort() //数组的排序
Arrays.binarySearch(data,key) //参用二分法进行数据的查询 data->要进行二分法的数据 获取的key
Arrays.compare(data1,data2) //比较data1和data2
Arrays.equals
Arrays.fill() //数组填充 
//int[] data = new data[10]; 
//Arrays.fill(data,3) //[3,3,3,3,3,3,3,3,3,3]

二分法的实践案例

public class binarySearch {
    public static void main(String[] args) {
        int[] b = new int[]{5,7,8,4,9,2,4,6};
        Arrays.sort(b);
        //2,4,4,5,6,7,8,9
        System.out.println(binarySearch0(b, 0, b.length, 5));
    }
    private static int binarySearch0(int[] a, int fromIndex, int toIndex, int key) {
        int start = fromIndex;
        int end = toIndex - 1;
        while (start <= end) {
            int mid = start + end >>> 1;
            int midNum = a[mid];
            if (key > midNum) {
                start = mid + 1;
            } else if (key < midNum) {
                end = mid - 1;
            } else {
                return mid;
            }
        }
        return -(end + 1);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值