赞
踩
写在开头:本文用于作者学习Java常用API
我将官方文档中Arrays类中所有API全测了一遍并打印了结果,日拱一卒,常看常新
- @Test
- /*
- asList
- 我的理解:将任意类型的数组转成列表
- 官方:返回一个任意类型的受指定数组支持的固定大小的列表。
- */
- public void test_asList(){
- List<String> a = Arrays.asList("a","b");
- System.out.println(a);//[a, b]
-
- Arrays.asList(1,2,3).forEach(System.out::println);//1 2 3
- List<Integer> integers = Arrays.asList(1, 2, 3);
- System.out.println(integers);//[1, 2, 3]
- System.out.println(Arrays.asList(1, 2, 3));//[1, 2, 3]
-
-
- List<Tuple2> tuple2s = Arrays.asList(
- new Tuple2("dahua", 100),
- new Tuple2("xiaoyabing", 99),
- new Tuple2("sangxinran", 98),
- new Tuple2("fangmangmang", 97),
- new Tuple2("dahua", 99));
- System.out.println(tuple2s);//[(dahua,100), (xiaoyabing,99), (sangxinran,98), (fangmangmang,97), (dahua,99)]
- }
- @Test
- public void test_binarySearch(){
- /*
- 输入元素,返回下标,未找到,则返回负数。
- * */
- int[] ints = new int[5];
-
- System.out.println(Arrays.binarySearch(ints, 3));//-6
- System.out.println(Arrays.binarySearch(ints, 1));//-6
- System.out.println(Arrays.binarySearch(ints, 5));//-6
-
-
- int[] numbers = {2, 4, 6, 8, 10, 12, 14};
- System.out.println(Arrays.binarySearch(numbers, 1));//-1
- System.out.println(Arrays.binarySearch(numbers, 2));//0
- System.out.println(Arrays.binarySearch(numbers, 8));//3
-
- }
- @Test
- public void test_copyOf(){
- /*
- * 复制个新数组
- * 截取或用 0 填充(如有必要),以使副本具有指定的长度。
- * 必须指定范围
- * */
- int[] numbers = {2, 4, 6, 8, 10, 12, 14};
- int[] ints = Arrays.copyOf(numbers, 5);
- System.out.println(Arrays.toString(ints));//[2, 4, 6, 8, 10]
- System.out.println(ints);//[I@3d82c5f3
-
- int[] ints1 = Arrays.copyOf(numbers, 10);
- System.out.println(Arrays.toString(ints1));//[2, 4, 6, 8, 10, 12, 14, 0, 0, 0]
-
- // Arrays.copyOf(numbers)//报错,必须指定范围
-
- }
- @Test
- public void test_copyOfRange(){
- /*copyOfRange
- * 将指定数组的【指定范围】复制到一个新数组。
- * */
- int[] numbers = {2, 4, 6, 8, 10, 12, 14};
- int[] ints = Arrays.copyOfRange(numbers, 3, 10);
- System.out.println(Arrays.toString(ints));//[8, 10, 12, 14, 0, 0, 0]
-
- // int[] ints1 = Arrays.copyOfRange(numbers, 10, 20);//报错,范围不能是负数,同时数组不能越界
- // System.out.println(Arrays.toString(ints1));
- }
- @Test
- public void test_deepEquals(){
- /*deepEquals
- * 如果两个二维(不能是一维)数组指定数组彼此是深层相等 的,则返回 true。
- * 深度相等:即比较数组中的元素是否逐个相等。*/
- int[][] numbers = {{1,2},{2, 4, 6, 8, 10, 12, 14}};
- int[][] numbers1 = {{1,2,3},{2, 4, 6, 8, 10, 12, 14}};
- System.out.println(Arrays.deepEquals(numbers,numbers1));//false
-
- int[][] numbers2 = {{1,2},{2, 4, 6, 8, 10, 12, 14}};
- System.out.println(Arrays.deepEquals(numbers,numbers2));//true
- }
- @Test
- public void test_deepHashCode(){
- /*deepHashCode
- 返回二维数组的哈希码。
- 不能是一维数组
- * */
- int[][] numbers = {{1,2},{2, 4, 6, 8, 10, 12, 14}};
- int deepHashCode = Arrays.deepHashCode(numbers);
- System.out.println(deepHashCode);//-656811770
-
- // int[] numbers1 = {2, 4, 6, 8, 10, 12, 14};
- // System.out.println(Arrays.deepHashCode(numbers1));//报错,不能是一维数组
-
- }
- @Test
- public void test_deepToString(){
- /*deepToString
- * 将二维数组转二维列表字符串
- * 我的理解:将一个二维数组以字符串的形式返回
- * 同样参数也只能是二维数组*/
- int[][] numbers = {{1,2},{2, 4, 6, 8, 10, 12, 14}};
- String deepToString = Arrays.deepToString(numbers);
- System.out.println(deepToString);//[[1, 2], [2, 4, 6, 8, 10, 12, 14]]
- }
- @Test
- public void test_equals(){
- /*equals
- * 如果两个指定的 object 类型数组彼此相等,则返回 true。
- * 二维数组用equals永远为false*/
- int[][] numbers = {{1,2},{2, 4, 6, 8, 10, 12, 14}};
- int[][] numbers1 = {{1,2,3},{2, 4, 6, 8, 10, 12, 14}};
- boolean equals = Arrays.equals(numbers, numbers1);//false
- System.out.println(equals);
-
- int[][] numbers2 = {{1,2},{2, 4, 6, 8, 10, 12, 14}};
- boolean equals1 = Arrays.equals(numbers, numbers2);//false
- System.out.println(equals1);
-
-
- /*一维数组才能判等*/
- int[] ints = {2, 4, 6, 8, 10, 12, 14};
- int[] ints1 = {2, 4, 6, 8, 10, 12, 14};
- int[] ints2 = {3, 4, 6, 8, 10, 12, 14};
- System.out.println(Arrays.equals(ints, ints1));//true
- System.out.println(Arrays.equals(ints, ints2));//false
- }
- @Test
- public void test_fill(){
- /* 数组填充 */
- int[] ints = {2, 4, 6, 8, 10, 12, 14};
- Arrays.fill(ints,9);
- System.out.println(Arrays.toString(ints));//[9, 9, 9, 9, 9, 9, 9]
-
- int[] ints1 = {2, 4, 6, 8, 10, 12, 14};
- Arrays.fill(ints1,3,5,100); // 含左不含右
- System.out.println(Arrays.toString(ints1));//[2, 4, 6, 100, 100, 12, 14]
- }
- @Test
- public void test_hashCode(){
- /*hashCode
- * 基于指定数组的内容返回哈希码。*/
- int[] ints = {2, 4, 6, 8, 10, 12, 14};
- int i = Arrays.hashCode(ints);
- System.out.println(i);//-656843545
-
- int[][] ints1 = {{1,2,3},{2, 4, 6, 8, 10, 12, 14}};
- int i1 = Arrays.hashCode(ints1);
- System.out.println(i1);//-1646592051
-
- }
- @Test
- public void test_sort(){
- /* 数组排序,默认升序
- * 也可以指定范围排序(不全排) */
- int[] ints = {1,3,7,2, 4, 6, 8, 10, 12, 14};
- Arrays.sort(ints);
- System.out.println(Arrays.toString(ints));//[1, 2, 3, 4, 6, 7, 8, 10, 12, 14]
-
- int[] ints1 = {1,3,7,2, 4, 6, 8, 10, 12, 0};
- //指定某个部分排序
- Arrays.sort(ints1,3,8);
- /*下标从0开始
- * 数组不能越界*/
- System.out.println(Arrays.toString(ints1));//[1, 3, 7, 2, 4, 6, 8, 10, 12, 0]
- }
- @Test
- public void test_toString(){
- /* 数组转字符串 */
- int[] ints = {1,3,7,2, 4, 6, 8, 10, 12, 14};
- System.out.println(Arrays.toString(ints));//[1, 3, 7, 2, 4, 6, 8, 10, 12, 14]
- }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。