码迷,mamicode.com
首页 > Windows程序 > 详细

记录一些工作学习中碰到的API,小技巧

时间:2018-08-16 16:35:03      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:意思   tip   img   inf   表达   eve   size   str   color   

  平时学习中碰到过一些很有意思的代码写法。记录下来,提升下Level。当然由于我是个新手,可能有少见多怪之嫌。


 

使用Pattern类  匹配正则表达式规则

int num = 1;
String REG = "[0-9]+";//填入需要的正则表达式规则
Pattern pattern = Pattern.compile(REG); boolean YN=pattern.matcher(String.valueOf(num)).matches(); System.out.println(YN);

比较器  定义

Comparator<Integer> cp = (x, y) -> x > y ? 1 : (x < y ? -1 : 0);

swap(arr,a,b) 不使用临时变量写法

/**
 * 交换数组元素
 * @param arr
 * @param a
 * @param b
 */
public void swap(int[] arr, int a, int b) {
    arr[a] ^= arr[b];
    arr[b] ^= arr[a];
    arr[a] ^= arr[b];
}

String.format

String.format("multiplier=%4d, minHash=%11d, maxHash=%10d, conflictNum=%6d, conflictRate=%.4f%%", multiplier, minHash, maxHash, conflictNum, conflictRate * 100)

可以按设置好的格式输出数据,结果显示如下:

技术分享图片

 

记录一些工作学习中碰到的API,小技巧

标签:意思   tip   img   inf   表达   eve   size   str   color   

原文地址:https://www.cnblogs.com/hungryforknowedge/p/9487821.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!