Java8增强的工具类:Arrays
Java提供的Arrays类里包含的一些static修饰的方法可以直接操作数组,这个Arrays类里包含了如下几个static修饰的方法(Static修饰的方法可以直接通过类名调用)。
静态方法
二分法搜索
public class ArraysUsing {
private static final int[] ints= {1,3,5,10,34,56,100,233,443};
public static void main(String[] args) {
long begin = System.nanoTime();
int key = 3;
System.out.println("key所在的索引位置:"+Arrays.binarySearch(ints, key));
long end = System.nanoTime();
System.out.println("耗时:"+(end-begin)+"ns");
}
}
运行结果为:
key所在的索引位置:1
耗时:333500ns
数组复制
public class ArraysUsing_2 {
private static final int[] ints= {1,3,5,10,34,56,100,233,443};
public static void main(String[] args) {
int[] copy = Arrays.copyOf(ArraysUsing_2.ints, 12);
int[] copy1 = Arrays.copyOfRange(ArraysUsing_2.ints,1, 4);
for (int aCopy : copy) {
System.out.println(aCopy);
}
System.out.println("---------分割----------");
for (int aCopy : copy1) {
System.out.println(aCopy);
}
}
}
运行结果:
1
3
5
10
34
56
100
233
443
0
0
0
---------分割----------
3
5
10
数组比较是否相同
public class ArraysUsing_3 {
private static final int[] ints1= {1,3,5,10,34,56,100,233,443};
private static final int[] ints2= {1,3,5,10,34,56,100,233,443};
public static void main(String[] args) {
System.out.println("两个数组相比较:"+Arrays.equals(ints1,ints2));
}
}
运行结果:
两个数组相比较:true
数组内容替换
public class ArraysUsing_4 {
public static void main(String[] args) {
int[] ints1 = {1,3,5,10,34,56,100,233,443};
System.out.print("数组元素未被全部替换前:");
for (int i:ints1) {
System.out.print(i+",");
}
System.out.println("");
Arrays.fill(ints1,20);
System.out.print("数组元素被全部替换:");
for (int i:ints1) {
System.out.print(i+",");
}
//---------------------------------------------------
int[] ints2 = {1,3,5,10,34,56,100,233,443};
System.out.println("");
System.out.print("数组元素未被全部替换前:");
for (int i:ints2) {
System.out.print(i+",");
}
System.out.println("");
Arrays.fill(ints2,0,5,20);
System.out.print("数组元素被部分替换:");
for (int i:ints2) {
System.out.print(i+",");
}
}
}
运行结果:
数组元素未被全部替换前:1,3,5,10,34,56,100,233,443,
数组元素被全部替换:20,20,20,20,20,20,20,20,20,
数组元素未被全部替换前:1,3,5,10,34,56,100,233,443,
数组元素被部分替换:20,20,20,20,20,56,100,233,443,
数组内容排序
public class ArraysUsing_5 {
public static void main(String[] args) {
int[] ints1 = {1,56,100,233,443,3,5,10,34,2};
long begin = System.nanoTime();
Arrays.sort(ints1);
long end = System.nanoTime();
System.out.println("执行时间:"+(end-begin)+"ns");
System.out.print("全排序:");
for (int i:ints1) {
System.out.print(i+",");
}
// -----------------------------------------------
System.out.println("");
System.out.print("部分排序:");
int[] ints2 = {1,56,100,233,443,3,5,10,34,2};
Arrays.sort(ints2,0,6);
for (int i:ints2) {
System.out.print(i+",");
}
}
}
数组转化为字符串
public class ArraysUsing_6 {
public static void main(String[] args) {
int[] ints1 = {1,56,100,233,443,3,5,10,34,2};
String string = Arrays.toString(ints1);
System.out.println("数组转化为字符串:"+string);
}
}
运行结果:会包含[]
数组转化为字符串:[1, 56, 100, 233, 443, 3, 5, 10, 34, 2]
增强方法
数组元素相加
public class ArraysUsing_7 {
public static void main(String[] args) {
int[] ints1 = {1,56,100,233,443,3,5,10,34,2};
Arrays.parallelPrefix(ints1, new IntBinaryOperator() {
@Override
public int applyAsInt(int left, int right) {
return left+right;
}
});
System.out.print("数组从左到右前后位置相加并赋值给数组:");
for (int i:ints1) {
System.out.print(i+",");
}
}
}
运行结果:
数组从左到右前后位置相加并赋值给数组:1,57,157,390,833,836,841,851,885,887,
数组元素按指定算法替换,不一定替换为唯一值
public class ArraysUsing_8 {
public static void main(String[] args) {
int[] ints1 = {1,56,100,233,443,3,5,10,34,2};
Arrays.setAll(ints1, new IntUnaryOperator() {
@Override
public int applyAsInt(int operand) {
return ints1[operand]-operand;
}
});
System.out.print("按指定算法替换:");
for (int i:ints1){
System.out.print(i+",");
}
}
}
运行结果:
按指定算法替换:1,55,98,230,439,-2,-1,3,26,-7,