数组最常用方法

- 日理万妓 2024-03-29 15:07 129阅读 0赞

目录

  • 初始化数组
  • 复制数组
  • 合并数组
  • 截取子串
  • 数组遍历
  • 基本方法
  • 检查数组是否包含某个值
  • 查找元素下标
  • 查找并返回两个同类型数组之间第一个不匹配的索引
  • 插入元素
  • 移除数组中的元素
  • 逆向一个数组
  • 移动数组的顺序
  • 交换数组两元素位置
  • 二次编辑每个元素
  • Java8流式用法
  • 数据equals
  • 数组 to string
  • 数组 to List
  • List to 数组
  • 数组 to Set
  • 数组 to Map
  • 打印二维数组
  • 将整数转换为字节数组
  • 参考

初始化数组

  1. String[] arr = new String[] {
  2. "zyy", "zxx", "zww"};
  3. String[] arr = {
  4. "wyy", "wzz", "wxx", "wxx"};
  5. String[] arr = ArrayUtils.toArray("a", "b", "c", "d", "e");
  6. String[] arr = new String[4];
  7. Arrays.fill(arr , "wyy");
  8. String[] arr = arr .clone();
  9. String[] arr = ArrayUtils.clone(s3);

复制数组

  1. Integer[] array1 = {
  2. 1, 1, 1, 2, 7, 11, 22, 37};
  3. Integer[] integers = Arrays.copyOf(array1, 4); // [1, 1, 1, 2]
  4. String[] s1 = {
  5. "wyy", "wzz", "wxx", "wxx"};
  6. String[] strArr = Arrays.copyOf(s1, 4); // [wyy, wzz, wxx, wxx]
  7. String[] strings1 = Arrays.copyOfRange(s1, 0, 1); //[wyy]
  8. String[] strings2 = Arrays.copyOfRange(s1, 0, s1.length); //[wyy, wzz, wxx, wxx]
  9. String[] strings3 = Arrays.copyOfRange(s1, 0, s1.length - 1); //[wyy, wzz, wxx]

合并数组

  1. int[] intArray = {
  2. 1, 2, 88, 3, 4, 5};
  3. int[] intArray2 = {
  4. 6, 7, 8, 9, 10 };
  5. int[] combinedIntArray = ArrayUtils.addAll(intArray, intArray2); //create a new array
  6. //[1, 2, 88, 3, 4, 5, 6, 7, 8, 9, 10]

截取子串

  1. String[] stringArray1 = {
  2. "a", "b", "c", "d", "e"};
  3. String[] subarray = ArrayUtils.subarray(stringArray1, 1, 3); // [b, c]

数组遍历

  1. int[] s5 = new int[] {
  2. 7, 3, 2, 8, 9};
  3. for (Integer s : s5) {
  4. System.out.print(s + " ");
  5. }
  6. for (int i = 0; i < s5.length; i++) {
  7. System.out.print(s5[i] + " ");
  8. }

基本方法

  1. boolean a = ArrayUtils.isEmpty(stringArray);
  2. boolean b = ArrayUtils.isNotEmpty(stringArray);
  3. boolean b = ArrayUtils.isSameLength(stringArray); 判断两个数组的长度是否相同,要同类型;
  4. boolean b = ArrayUtils.isSameType(stringArray); 判断两个数组的类型是否相同
  5. boolean b = ArrayUtils.isSorted(stringArray); 判断数组是否以自然顺序排序
  6. boolean b = ArrayUtils.nullToEmpty(stringArray);

检查数组是否包含某个值

  1. String[] stringArray = {
  2. "a", "b", "c", "d", "e"};
  3. boolean b = Arrays.asList(stringArray).contains("a");
  4. boolean a = ArrayUtils.contains(stringArray, "a");

查找元素下标

  1. # 查找元素下标
  2. Integer[] array1 = {
  3. 1, 1, 1, 2, 7, 11, 22, 37};
  4. int i = Arrays.binarySearch(array1, 2);
  5. System.out.println(i); // 3
  6. # 查找元素第一次出现的下标
  7. int a = ArrayUtils.indexOf(stringArray, "a");
  8. # 查找所有元素下标
  9. String[] stringArray = {
  10. "a", "b", "c", "d", "a"};
  11. BitSet a = ArrayUtils.indexesOf(stringArray, "a"); // {0, 4}
  12. BitSet a = ArrayUtils.indexesOf(arr, "a", 2); 从下标为2开始查找3D这个值得所有下标
  13. # 查找元素最后一次出现的下标
  14. int a = ArrayUtils.lastIndexOf(stringArray, "a");
  15. # 查找下标是否有效
  16. ArrayUtils.isArrayIndexValid(null, 0) // false
  17. ArrayUtils.isArrayIndexValid([], 0) // false
  18. ArrayUtils.isArrayIndexValid(["a"], 0) // true

查找并返回两个同类型数组之间第一个不匹配的索引

  1. int[] array1 = {
  2. 2, 7, 11, 22, 37};
  3. int[] array2 = {
  4. 2, 7, 11, 22, 37};
  5. int[] array3 = {
  6. 2, 7, 19, 31, 39, 56};
  7. int index1 = Arrays.mismatch(array1, array2); //-1
  8. int index2 = Arrays.mismatch(array1, array3); // 2

插入元素

  1. # 从数组后添加元素
  2. String[] stringArray = {
  3. "a", "b", "c", "d", "e"};
  4. String[] fs = ArrayUtils.add(stringArray, "f"); // [a, b, c, d, e, f]
  5. # 往数组里添加数组
  6. String[] stringArray = {
  7. "a", "b", "c", "d", "e"};
  8. String[] stringArray2 = {
  9. "a", "b", "c", "d", "e"};
  10. String[] fs = ArrayUtils.addAll(stringArray, stringArray2); // [a, b, c, d, e, a, b, c, d, e]
  11. # 从数组前添加元素
  12. String[] stringArray = {
  13. "a", "b", "c", "d", "e"};
  14. String[] fs = ArrayUtils.addFirst(stringArray, "t"); // [t, a, b, c, d, e]
  15. # 插入元素到指定位置
  16. String[] stringArray = {
  17. "a", "a", "b", "c", "d", "a"};
  18. String[] zs = ArrayUtils.insert(1, stringArray, "z"); // [a, z, a, b, c, d, a]

移除数组中的元素

  1. # 删除指定位置元素 create a new array
  2. ArrayUtils.remove(["a"], 0) = []
  3. ArrayUtils.remove(["a", "b"], 0) = ["b"]
  4. ArrayUtils.remove(["a", "b"], 1) = ["a"]
  5. ArrayUtils.remove(["a", "b", "c"], 1) = ["a", "c"]
  6. # 删除指定位置多个元素
  7. ArrayUtils.removeAll([1], 0) = []
  8. ArrayUtils.removeAll([2, 6], 0) = [6]
  9. ArrayUtils.removeAll([2, 6], 0, 1) = []
  10. ArrayUtils.removeAll([2, 6, 3], 1, 2) = [2]
  11. ArrayUtils.removeAll([2, 6, 3], 0, 2) = [6]
  12. ArrayUtils.removeAll([2, 6, 3], 0, 1, 2) = []
  13. # 删除所有指定元素
  14. ArrayUtils.removeElement(null, true) = null
  15. ArrayUtils.removeElement([], true) = []
  16. ArrayUtils.removeElement([true], false) = [true]
  17. ArrayUtils.removeElement([true, false], false) = [true]
  18. ArrayUtils.removeElement([true, false, true], true) = [false, true]

逆向一个数组

  1. int[] intArray = {
  2. 1, 2, 3, 4, 5 };
  3. ArrayUtils.reverse(intArray); // [5, 4, 3, 2, 1]

移动数组的顺序

  1. # Shifts the order of the given int array
  2. String[] stringArray1 = {
  3. "a", "b", "c", "d", "e"};
  4. ArrayUtils.shift(stringArray1,1); // [e, a, b, c, d]
  5. String[] stringArray = {
  6. "a", "b", "c", "d", "e"};
  7. ArrayUtils.shift(stringArray,2); // [d, e, a, b, c]
  8. String[] stringArray2 = {
  9. "a", "b", "c", "d", "e"};
  10. ArrayUtils.shift(stringArray2,3); // [c, d, e, a, b]
  11. String[] stringArray3 = {
  12. "a", "b", "c", "d", "e"};
  13. ArrayUtils.shift(stringArray3,4); // [b, c, d, e, a]
  14. String[] stringArray4 = {
  15. "a", "b", "c", "d", "e"};
  16. ArrayUtils.shift(stringArray4,5); // [a, b, c, d, e]
  17. String[] stringArray5 = {
  18. "a", "b", "c", "d", "e"};
  19. ArrayUtils.shift(stringArray5,6);// 6%5 ->1 取模 [e, a, b, c, d]

交换数组两元素位置

  1. ArrayUtils.swap(["1", "2", "3"], 0, 2) -> ["3", "2", "1"]
  2. ArrayUtils.swap(["1", "2", "3"], 0, 0) -> ["1", "2", "3"]
  3. ArrayUtils.swap(["1", "2", "3"], 1, 0) -> ["2", "1", "3"]
  4. ArrayUtils.swap(["1", "2", "3"], 0, 5) -> ["1", "2", "3"]
  5. ArrayUtils.swap(["1", "2", "3"], -1, 1) -> ["2", "1", "3"]

二次编辑每个元素

  1. double[] a = {
  2. 1.0, 2.0, 3.0, 4.4};
  3. Arrays.setAll(a, x -> a[x] * a[x]);
  4. System.out.println("数组中整数的平方:" + Arrays.toString(a));
  5. 数组中整数的平方:[1.0, 4.0, 9.0, 19.360000000000003]

Java8流式用法

  1. Double[] stringArray = {
  2. 1D, 2D, 3D, 11D, 22D, 33D, 111D, 222D, 333D};
  3. Arrays.stream(stringArray).filter(a -> a > 2D).forEach(aDouble -> System.out.print(aDouble + " 、"));
  4. // 3.0 、11.0 、22.0 、33.0 、111.0 、222.0 、333.0 、
  5. Arrays.stream(stringArray, 0, 4).filter(a -> a > 2D).forEach(aDouble -> System.out.print(aDouble + " 、"));
  6. // 3.0 、11.0 、

数据equals

  1. Integer[] array1 = {
  2. 1, 1, 1, 2, 7, 11, 22, 37};
  3. Integer[] array2 = {
  4. 1, 1, 1, 2, 7, 11, 22, 37};
  5. boolean equals = Arrays.equals(array1, array2); // true
  6. boolean equals1 = Arrays.equals(array1, 2, 4, array2, 2, 4); // true
  7. boolean equals3 = Arrays.equals(array1, array2, new Comparator<Integer>() {
  8. @Override
  9. public int compare(Integer o1, Integer o2) {
  10. return (o1 < o2 ? -1 : (o1.equals(o2) ? 0 : 1));
  11. }
  12. }); // true
  13. boolean equals4 = Arrays.equals(array1, array2, (o1, o2) -> (o1 < o2 ? -1 : (o1.equals(o2) ? 0 : 1)));
  14. System.out.println(equals); // true

数组 to string

  1. String[] s1 = {
  2. "wyy", "wzz", "wxx", "wxx"};
  3. # String提供实现
  4. String str4 = String.join(",", s1); // wyy,wzz,wxx,wxx
  5. String str3 = StringUtils.join(s1, ","); // wyy,wzz,wxx,wxx
  6. # 数组提供实现
  7. String str1 = Arrays.toString(s1); // [wyy, wzz, wxx, wxx]
  8. String str2 = ArrayUtils.toString(s1); // {wyy,wzz,wxx,wxx}
  9. # 自定义实现
  10. String str5 = Arrays.stream(s1).collect(Collectors.joining(",")); // wyy,wzz,wxx,wxx

数组 to List

  1. String[] s2 = {
  2. "wyy", "wzz", "wxx", "wxx"};
  3. List<String> list = Arrays.asList(s2);
  4. List<String> list = List.of(s2);
  5. List<String> list = new ArrayList<>(List.of(s2));
  6. ist<String> list = new ArrayList<>(Arrays.asList(s2));

List to 数组

  1. Integer[] list = integers2.toArray(Integer[]::new);
  2. Integer[] list = integers2.toArray(new Integer[0]);
  3. Integer[] list = integers2.toArray(new Integer[integers2.size()]);

字符串,数组,集合之间相互转换总结

数组 to Set

  1. Set<String> set = new HashSet<>(Arrays.asList(s2));

数组 to Map

  1. Map colorMap = ArrayUtils.toMap(new String[][] {
  2. {
  3. "RED", "#FF0000"},
  4. {
  5. "GREEN", "#00FF00"},
  6. {
  7. "BLUE", "#0000FF"}
  8. });

打印二维数组

  1. // 打印二维数组
  2. int[][] ints = new int[][]{
  3. {
  4. 1, 2, 3}, {
  5. 4, 5, 6}};
  6. Arrays.stream(ints).forEach(a -> System.out.println(Arrays.toString(a)));
  7. [1, 2, 3]
  8. [4, 5, 6]

将整数转换为字节数组

  1. byte[] bytes = ByteBuffer
  2. // 从堆空间中分配缓冲区4个字节
  3. .allocate(4)
  4. //向ByteBuffer写数据
  5. .putInt(100).array();
  6. // Stream.of(bytes).forEach(a -> System.out.format("0x%x ", a)); 报错
  7. for (byte t : bytes) {
  8. System.out.format("0x%x ", t);
  9. }
  10. 0x0 0x0 0x0 0x64

参考

stackoverflow中票数最多的数组操作方法
斩草除根学ArrayUtils
斩草除根学Arrays

发表评论

表情:
评论列表 (有 0 条评论,129人围观)

还没有评论,来说两句吧...

相关阅读

    相关 数组方法

    前言 我平时都是需要用到数组时再去搜方法,没想到面试中好几次都被问到对数组的一些操作,所以每次都黑人问号,基本只记得什么pop/push/shift,就想着找点资料好好总

    相关 js 数组方法

    > 零碎知识点: > ① 当给子元素设置了绝对或者固定定位且父元素设置了`overflow:hidden`,若不给父元素设置高/宽,会出现子元素隐藏的现象 > ② 火狐