java数据结构之数组详解
开篇说明
上篇详细总结了字符串的使用String详解,很多都是边看源码边总结的,印象非常深刻。建议大家在学习时也要敲代码记笔记,真的比单看这些文章有用多啦~
这篇开始记录数组,会涉及数组与其他数据结构的转换,常用方法、底层原理等等。每天进步一点点,冲鸭!!!
现在是12月6日晚上10.42,这会儿我的一个舍友在播放“改革春风吹满地,中国人民真争气”这首歌,刚刚加班回来一直在唱,虽然有点烦,但是大家都是程序猿,也能理解压力,哈哈哈我现在也被稍稍洗脑了🥱
数组
在java中数组虽然是一个对象,但是并未明确定义一个类。
很多文章把数组想象成一个容器,可以容纳固定大小的同类项的元素。
- 数组的创建
String[] stringArray = new String[2];
stringArray[0] = "hello";
stringArray[1] = "world";
System.out.println(Arrays.toString(stringArray)); // [hello, world]
String[] stringArray2 = {"hello", "world"};
System.out.println(Arrays.toString(stringArray2)); // [hello, world]
System.out.println(stringArray.equals(stringArray2)); // false
System.out.println(Arrays.equals(stringArray, stringArray2)); // true
// 二维数组
String[][] strings = {
{"banana", "apple", "peal"},
{"black", "red", "yellow"}
};
System.out.println(Arrays.deepToString(strings)); // [[banana, apple, peal], [black, red, yellow]]
- 获取数组长度: stringArray.length 注意没有括号,length是属性
Arrays类
Arrays类用来方便地操作数组,提供的所有方法都是静态的。
- Arrays.sort(array)
note:对array进行升序排列 - Arrays.parallelSort(array)
note:并行排序合并,分解成子数组调用Arrays.sort - Arrays.equals(array1, array2)
note:两个数组元素相同,并且顺序排列相同,则相等 - Arrays.deepEquals(array1, array2)
note:deepEquals用来判定两个数组是否深层次相等,适用于任何深度的嵌套数组 - Arrays.binarySearch(array, key)
note:二分查找法,数组在调用前必须排序好,返回key在array中的下标;否则返回 (-(插入点) - 1) - Arrays.fill(array, value)
note:将array用value填充 - Arrays.toString(array)
note:打印数组 - Arrays.deepToString(object)
note:常用来打印多维数组
int[][] ints = {
{1, 2},
{23, 32},
{44, 34}
};
System.out.println(Arrays.deepToString(ints)); // [[1, 2], [23, 32], [44, 34]]
- Arrays.copyOf(array, int)/Arrays.copyOfRange(array, int, int)
note:用来数组的复制 - Arrays.asList(array)
将数组转为list;原始类型int的数组调用asList得到的List只有一个元素,这个元素是元素类型的数组。而封装类Integer调用asList是把数组每个元素加到List中
。(此处是因为asList方法接收一个类型为T的数组,而基本类型不能作为泛型参数,只能接收引用类型。int数组传进来时就把int[]当成了引用传参。)
返回的list是一个不可变长度的列表,不具备原List的很多特性。
不支持add和remove操作
int[] ints = {1, 2, 3};
List<int[]> list = Arrays.asList(ints);
Integer[] integers = {1, 2, 3};
List<Integer> list1 = Arrays.asList(integers);
- Array.parallelPrefix(array, op)
并行累计操作
int[] ints = {1, 2, 3};
String[] strings = {"hello", "world", "dajun"};
Arrays.parallelPrefix(ints, (x, y) -> (x + y));
Arrays.parallelPrefix(strings, (x, y) -> (x + y));
System.out.println(Arrays.toString(ints)); // [1, 3, 6]
System.out.println(Arrays.toString(strings)); // [hello, helloworld, helloworlddajun]
- Arrays.parallelSetAll(array, op)
note:并行设置array,将索引代入op计算
array = new int[]{3, 10, 4, 0, 2};
Arrays.parallelSetAll(array, (x)->(x*x));
System.out.println(Arrays.toString(array)); //[0, 1, 4, 9, 16]
Arrays.setAll(array, (x)->(x%3));
System.out.println(Arrays.toString(array)); //[0, 1, 2, 0, 1], 与parallelSetAll相比只是不并行
数组转list的几种方式比较
- 使用
Arrays.asList(strArray)
不支持add和remove操作
String[] names = {"ada", "tom", "alice", "fred"};
List<String> nameList = Arrays.asList(names);
System.out.println(nameList); // [ada, tom, alice, fred]
原因:Arrays.asList(strArray)返回值是java.util.Arrays
类中一个私有静态内部类java.util.Arrays.ArrayList
, 并非java.util.ArrayList
类。java.util.Arrays.ArrayList
类具有set(),get(),contains()等方法,但是不具有add()和remove()方法,所以调用add方法会报错。
使用场景:Array.asList(strArray)
仅用在数组转换在list后,不需要增删,仅作为数据源读取使用。
- 使用
new ArrayList<String>(Arrays.asList(strArray))
String[] names = {"ada", "tom", "alice", "fred"};
List<String> nameList = new ArrayList<>(Arrays.asList(names));
nameList.add("jackson");
System.out.println(nameList); // [ada, tom, alice, fred, jackson]
System.out.println(Arrays.toString(names)); // [ada, tom, alice, fred]
使用场景:数组转换为list后,对list进行增删操作,在List数据量不大的情况下可以使用。
- Collections.addAll(list, array)
String[] names = {"ada", "tom", "alice", "fred"};
List<String> nameList = new ArrayList<>(names.length);
Collections.addAll(nameList, names);
nameList.add("dajun");
System.out.println(nameList); //[ada, tom, alice, fred, dajun]
- java8使用stream流将3种基本数据类型转为list
List<Integer> intList = Arrays.stream(new int[]{1, 2, 3}).boxed().collect(Collectors.toList());
System.out.println(intList); // [1, 2, 3]
List<Long> longList = Arrays.stream(new long[] {1, 2, 3}).boxed().collect(Collectors.toList());
System.out.println(longList); // [1, 2, 3]
List<Double> doubleList = Arrays.stream(new double[] {1.2, 2.0, 3.4}).boxed().collect(Collectors.toList());
System.out.println(doubleList); // [1.2, 2.0, 3.4]
- java9中使用List.of()
List<String> resultList = List.of(array);
参考大佬文章:Java数组转List的三种方式及对比