java成神之——集合框架之Array

Array

初始化

声明数组的几种方式

    int[] arr = new int[3]; 
    int[] arr = { 1, 2, 3 };
    int[] arr = new int[] { 1, 2, 3 }; 
    int[][] arr = { { 1, 2 }, { 3, 4, 5 } };
    int[][] arr = new int[5][]; 
    int[][] arr = new int[5][4];

初始化

    int[] arr = {1, 2, 3};
    List<int[]> list = Arrays.asList(arr);

    Integer[] arr = {1, 2, 3};
    List<Integer> list = Arrays.asList(arr);

指定数组元素类型

    int[] arr = (int[]) Array.newInstance(int.class, 3);
    arr[0] = 0;
    arr[1] = 1;
    arr[2] = 2;

填充元素的几种方式

第一种

    int[] arr = new int[3];
    Arrays.fill(arr, 1);                        // [1,1,1]

第二种

    int[] arr = new int[3];
    Arrays.fill(arr, 0, 1, 2);                  // [2, 0, 0]

第三种

    int[] arr = new int[5];
    Arrays.setAll(arr, i -> 2 * i);             // [0,2,4,6,8]

第四种

    ArrayList<String> arr = new ArrayList<>();
    String[] str = {"a", "b", "c"};
    arr.addAll(Arrays.asList(str));             

第五种

    ArrayList<String> arr = new ArrayList<>();
    String[] str = {"a", "b", "c"};
    Collections.addAll(arr, str);

第六种

    int[] arr = {1, 2, 3};
    List<Integer> list = Arrays.stream(arr).boxed().collect(Collectors.toList());

第七种

    String[] arr = {"a", "b", "c"};
    List<String> list = Arrays.stream(arr).collect(Collectors.toList());

第八种

    List<String> list = new ArrayList<>(Arrays.asList("a", "b"));

数组转流

第一种

    String[] str = new String[] {"a", "b", "c"};
    Stream<String> stream = Arrays.stream(str);
    
第二种

    Stream<String> stream = Stream.of("a", "b", "c");

遍历

第一种

    for (int i = 0; i < array.length; i++) {
        array[i] = i;
    }

第二种

    for (int e : array) {
        System.out.println(e);
    }

第三种

    Integer[] boxed = {1, 2, 3};
    Iterable<Integer> boxedIt = Arrays.asList(boxed);
    Iterator<Integer> fromBoxed1 = boxedIt.iterator();
    while(fromBoxed1.hasNext()) {
        System.out.println(fromBoxed1.next());
    }

第四种

    int[] primitives = {1, 2, 3};
    IntStream primitiveStream = Arrays.stream(primitives);
    PrimitiveIterator.OfInt fromPrimitive1 = primitiveStream.iterator();
    while(fromPrimitive1.hasNext()) {
        System.out.println(fromPrimitive1.next());
    }

数组转成字符串

int[] arr = {1, 2, 3, 4, 5};
Arrays.toString(arr);

排序

int[] array = {7, 4, 2, 1, 19};
Arrays.sort(array); // [1,2,4,7,19]

Integer[] array = {7, 4, 2, 1, 19};
Arrays.sort(array, 0, array.length, Collections.reverseOrder()); // [19,7,4,2,1]

查找

二分查找

    String[] str = new String[] { "a", "b", "c" };
    int index = Arrays.binarySearch(str, "b");

查找索引位置

    int index = Arrays.asList(str).indexOf("b");

使用流链式查找索引位置

    int index = IntStream
                .range(0, str.length)
                .filter(i -> "a".equals(str[i]))
                .findFirst()
                .orElse(-1);

contains是否存在

    boolean isPresent = Arrays.asList(str).contains("b");

使用流判断是否存在

    boolean isPresent = Stream.of(str).anyMatch(x -> "a".equals(x));

数组扩大

String[] str = new String[] { "a", "b", "c" };
String[] newstr = Arrays.copyOf(str, str.length + 1);
newstr[str.length] = "d";

原始类型数组和包装类型数组转换

原始类型转成包装类型
int[] primitiveArray = {1, 2, 3, 4};
Integer[] boxedArray = Arrays.stream(primitiveArray).boxed().toArray(Integer[]::new);

包装类型转成原始类型
Integer[] boxedArray = {1, 2, 3, 4};
int[] primitiveArray = Arrays.stream(boxedArray).mapToInt(Integer::intValue).toArray();

移除元素

String[] str = new String[]{"a", "b", "c"};
List<String> list = new ArrayList<>(Arrays.asList(str));
list.remove("a");

比较数组是否相等

String[] str1 = new String[]{"a", "b", "c"};
String[] str2 = new String[]{"a", "b", "c"};
Arrays.equals(str1, str2);

克隆

浅克隆

第一种

    int[] a = { 4, 1, 3, 2 };
    int[] b = a.clone();

第二种

    int[] a = {4, 1, 3, 2};
    int[] b = Arrays.copyOf(a, a.length);

第三种

    int[] a = { 4, 1, 3, 2 };
    int[] b = new int[a.length];
    System.arraycopy(a, 0, b, 0, a.length);

第四种

    int[] a = { 4, 1, 3, 2 };
    int[] b = Arrays.copyOfRange(a, 0, a.length); 

类型转换

Integer[] arr = { 1, 2, 3 };
Number[] numbers = Arrays.copyOf(arr, arr.length, Number[].class);

过滤元素

List<String> list = new ArrayList<String>();
list.add("a");
list.add("b");
list.add("c");

List<String> filteredList = list.stream().filter(item -> !"b".equals(item)).collect(Collectors.toList());

list.removeIf(item -> "b".equals(item));

结语

本文章是java成神的系列文章之一

如果你想知道,但是本文没有的,请下方留言

我会第一时间总结出来并发布填充到本文

猜你喜欢

转载自www.cnblogs.com/ye-hcj/p/9723756.html