Arrays
- 用来操作数组的一个工具类
Arrays类提供的常见方法:
代码演示:
import java.util.Arrays;
import java.util.function.IntToDoubleFunction;
import java.util.function.IntUnaryOperator;public class Arrays类 {public static void main(String[] args) {int[] arr = {12,30,28,15,6,7,77};//toString 返回数组的内容System.out.println(Arrays.toString(arr));//copyOfRange 拷贝数组(指定范围,范围为左闭右开区间)int[] arr2 = Arrays.copyOfRange(arr, 3, 6);System.out.println(Arrays.toString(arr2));//copyOf 拷贝数组(指定新数组长度,若数组长度大于原数组长度,则多出来的位置补0,可用于拓宽数组)int[] arr3 = Arrays.copyOf(arr,10);System.out.println(Arrays.toString(arr3));//setAll 把数组中的原数据改为新数据Arrays.setAll(arr, new IntUnaryOperator() {@Overridepublic int applyAsInt(int operand) {return arr[operand] + 10;}});System.out.println(Arrays.toString(arr));double[] arr4 = {1.1,15.6,23.4,435.6};Arrays.setAll(arr4, new IntToDoubleFunction() {@Overridepublic double applyAsDouble(int value) {return arr4[value]*2;}});System.out.println(Arrays.toString(arr4));//sort 对数组进行排序(默认升序)Arrays.sort(arr);System.out.println(Arrays.toString(arr));}
}