您的位置:首页 > 编程语言 > Java开发

Java API一些注意的零碎不定期整理

2017-06-20 09:03 316 查看

Arrays.binarySearch()

二分搜索是建立在有序数组上的,所以搜索前排序,否则得到的结果不可行。

指定from, to的时候,包括from,不包括to

如果查找到返回对应的位置,没有查找到返回第一个比他大的位置,值为负数,转换后为(-res -1)

不管指定区间与否,3都成立。

将集合元素反序

Collections.reverse()


运行时间在线性范围,但是需要开辟额外的空间。

将数组转成字符串,并指定分隔符

API

static String   join(CharSequence delimiter, CharSequence... elements)
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.
static String   join(CharSequence delimiter, Iterable<? extends CharSequence> elements)
Returns a new String composed of copies of the CharSequence elements joined together with a copy of the specified delimiter.


注意这里只能处理字符串数组,对于其他类型的数组不行

对于其他类型的,还没发现好用的,目前只能用Arrays.toString(),再用正则进一步处理

String[] strs = {"1","2","3","4"};
System.out.println(String.join(",", strs));
//1,2,3,4
int[] nums = {1,2,3,4,6,7,8,9};
System.out.println(Arrays.toString(nums).replaceAll("[\\[\\]]", ""));
//1, 2, 3, 4, 6, 7, 8, 9


replace replaceAll replaceFirst区别

replace没有用到正则表达式

repaceAll和replaceFirst都用到正则表达式

replaceAll是贪心的用法

replaceFirst是懒惰的
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  java api