10.常用类API

1. 正则:regex

\r\n : 回车+ 换行

\.: 匹配点

\t: 制表符tab

\b: 英文的 单词分隔

\d: 数字m

\D: 非数字 [^0-9]

\w: 字母数字 下划线 [0-9A-Za-z_]

\W: 非字母数字 下划线[^0-9A-Za-z_]

[]: 字符簇: 中括号中的一个字符

{m, n}: 次数 出现m次到n次之间。 {m} m次 {m,} m到∞

? : {0,1} 0次或1次

+: {1,} 1次到多次

*: {0, } 0到多次

*?: 贪婪 最少匹配次数

. : 除了换行符\n外所有的字符

^: 开始符号

$: 结束符

		
		// 模式:样本:通过静态方法实例化的
		Pattern p = Pattern.compile("\\d+");
		
		// 匹配器
		Matcher m = p.matcher("adjfaoj1233`4j3214kl124j32 4j 321j4 231j423l14 ");
		
		// 正则是否匹配
		boolean b1 = m.matches(); 
		System.out.println(b1);
		
		// 找到匹配的正则
		while (m.find()) { // 找下一个
			String str = m.group(); // 匹配的内容
			System.out.println(str + "\"position:" + m.start() + "\"end:" + m.end()); //[起始下标, 结束下标 )
		}
		
		System.out.println(Pattern.matches("\\d+", "a1111")); // "^\\d+$"
		System.out.println("a111".matches("^a\\d+")); // "^\\d+$"

2。StringBuffer / StringBuilder

目的: 提高 字符串拼接速度。

StringBuffer:由于有线程同步锁,慢,安全

StringBuilder: 没有锁, 快, 不安全

  • append(); 字符串拼接
  • insert(index, str): 插入
  • reverse(); 反转
	
		String a = new String("a");
		long start = System.currentTimeMillis();
		for (int i = 0; i < 10000; i++) {
			a = a + "a";
		}
		long end = System.currentTimeMillis();
		System.out.println(end - start);
		
		StringBuilder sb1 = new StringBuilder();
		sb1.append("hello");
		sb1.append(" ");
		sb1.append("world");
		System.out.println(sb1.toString());
		
		// 没有锁,快
		StringBuilder sb = new StringBuilder("a");
		start = System.currentTimeMillis();
		for (int i = 0; i < 10000000; i++) {
			sb.append("a");
		}
		end = System.currentTimeMillis();
		System.out.println(end - start);
		
		// 有同步锁, 慢,但是安全
		StringBuffer sb2 = new StringBuffer("a");
		start = System.currentTimeMillis();
		for (int i = 0; i < 10000000; i++) {
			sb2.append("a");
		}
		end = System.currentTimeMillis();
		System.out.println(end - start);
		
	

3. Math:

  • abs() ; 绝对值:
  • ceil(); 天花板 在上 。 数轴取右
  • floor(); 地板 在下。 数轴取左
  • pow(n, m); n的m次幂
  • random(); [0.0, 1.0) 内随机小数
  • round(); 四舍五入
		System.out.println(Math.abs(-1));
		
		System.out.println(Math.ceil(2.1)); // 3.0
		System.out.println(Math.ceil(-2.1)); // -2.0
		
		System.out.println(Math.floor(2.1)); // 2.0
		System.out.println(Math.floor(-2.1)); // -3.0
		
		System.out.println(Math.pow(10, 2)); // 100.0
		
		for (int i = 0; i < 100; i++) {
			System.out.println(Math.random()); // 底层Random [0.0, 1.0)   [32, 55]
		}
		// 随机32-55之间的数字
		System.out.println("--------------------------");
		for (int i = 0; i < 100; i++) {
			System.out.println((int) (Math.random() * 24 + 32)); // 底层Random [0.0, 1.0) * 24 + 32 ==  [32, 56)
		}
		
		System.out.println(Math.round(3.1415)); // 3
		System.out.println(Math.round(-3.1415)); // -3

		// 作一个五舍六入
		System.out.println(Math.floor(Math.PI + 0.4));
	

4. Object类

  • getClass(); 获取反射类对象

  • int hashCode(): hash算法 ,可以理解存储地址(不是绝对的),保证new 的hashCode值是不重复的

  • boolean equals(): 内容的比较。

    • ※必须和hashCode统一 : 如果覆盖equals()方法一定要覆盖hashCode方法
  • clone() : 克隆对象: 慎用

    • ※必须实现Cloneable接口
    • jdk 2个标记型接口: Cloneable ,Serializable 没有任何方法
  • toString(); 用于打印的多态

  • notify|notifyAll() : 用于线程的唤醒(waiting pool —> runable)

  • wait() : 线程进入 waiting pool

  • finalize() : GC回调: 慎用。

    5. GC: 垃圾回收

    针对堆的回收。

    GC: 是我们控制不了的。

    System.gc(); 只是通知,不是直接GC。

    
    	
    		Person p1 = new Person("tom", 10);
    		Person p2 = new Person("tom", 10);
    		
    		System.out.println(p1 == p2);
    		System.out.println(p1.equals(p2));
    		System.out.println(p1);
    		System.out.println(p2);
    		System.out.println("tom".hashCode());
    
    		Person p3 = p1;
    		p3.setAge(20);
    		System.out.println(p1.getAge());
    		
    		int a = 1;
    		int b = a;
    		b = 10;
    		System.out.println(a);
    		
    		Person p4 = (Person) p1.clone();
    		p4.setAge(30);
    		System.out.println(p4);
    		System.out.println(p4.getAge());
    		System.out.println(p1.getAge());
    		
    		
    		p3 = null;
    		p1 = null;
    		System.out.println("--------------");
    		System.gc(); // 通知jvm 在不忙的可以进行回收了		
    		System.out.println("--------------");
    		while (true) {
    			
    		}
    	
    
posted @ 2021-04-10 12:30  剑心空明  阅读(0)  评论(0编辑  收藏  举报  来源