当前位置:   article > 正文

Java常用API之Arrays类解读

Java常用API之Arrays类解读

写在开头:本文用于作者学习Java常用API

我将官方文档中Arrays类中所有API全测了一遍并打印了结果,日拱一卒,常看常新

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

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/354345?site
推荐阅读
相关标签
  

闽ICP备14008679号