3_数组

数组概述

数组的定义:在连续的内存空间中,储存一组相同类型的元素

数组的访问:通过索引访问元素
数组的搜索:找到这个元素对应的索引


数组的四种操作

任意数据结构基本都要讨论这四种操作,对比两个数据结构也是从这四种操作入手

  • 访问 Access 时间复杂度 O(1)
    通过计算可以得到地址位置,从而进行访问
  • 搜索 Search 时间复杂度 O(N)
    需要对数组进行遍历
  • 插入 Insert 时间复杂度 O(N)
    • 需要将后面的元素往后移动
    • 如果内存不够,需要开辟一块新空间,将数组移进去
  • 删除 Delete 时间复杂度 O(N)
    需要将后面元素往前移

数组特点

  • 适合读
  • 不适合频繁做增删操作
  • 场景:读多写少

数组实际的常用操作(Java)

9_Java集合

  • 创建数组

    1. //Four solutions to create an array in Java
    2. //Take [1,2,3] as example
    3. //Solution 1
    4. int[] a = {1, 2, 3};
    5. System.out.println("a:" + Arrays.toString(a));
    6. //Solution 2
    7. int[] b = new int[]{1, 2, 3};
    8. System.out.println("b:" + Arrays.toString(b));
    9. //Solution 3
    10. int[] c = new int[3];
    11. for (int i = 0; i < a.length; i++) {
    12. c[i] = i + 1;
    13. }
    14. System.out.println("c:" + Arrays.toString(c));
    15. //Solution 4
    16. ArrayList<Integer> arr = new ArrayList<>();
    17. for (int i = 0; i < 3; i++) {
    18. arr.add(i + 1);
    19. }
    20. System.out.println("arr:" + arr.toString());
  • 添加元素

    1. //Add element 空间足够,尾部添加
    2. //Time Complexity:O(1)
    3. arr.add(99);
    4. //[1,2,3,99]
    5. System.out.println("arr:" + arr.toString());
    6. //Insert element 中间插入
    7. //Time Complexity:O(N)
    8. arr.add(3, 88);
    9. //[1,2,3,88,99]
    10. System.out.println("arr:" + arr.toString());
  • 访问元素

    1. //Access element
    2. //Time Complexity:O(1)
    3. int c1 = c[0];
    4. int arr1 = arr.get(0);
    5. //1
    6. System.out.println("c1:" + c1);
    7. System.out.println("arr1:" + arr1);
  • 修改元素

    1. //Update element 先访问后修改
    2. //Time Complexity:O(1)
    3. c[0] = 11;
    4. arr.set(0, 11);
    5. //1->11
    6. System.out.println("c1:" + c[0]);
    7. System.out.println("arr1:" + arr.get(0));
  • 删除元素

    1. //Remove element
    2. //Time Complexity:O(N)
    3. arr.remove(0);
    4. System.out.println("arr1:" + arr.get(1));
  • 数组的长度

    1. //The length of an array
    2. //Time Complexity:O(1)
    3. int cSize = c.length;
    4. int arrSize = arr.size();
    5. System.out.println("c length:" + cSize);
    6. System.out.println("arr length:" + arrSize);
  • 遍历元素

    1. //Iterate an array
    2. //Time Complexity:O(N)
    3. //Iterate c
    4. for (int i = 0; i < c.length; i++) {
    5. int current = c[i];
    6. System.out.println("c at index " + i + ":" + current);
    7. }
    8. //Iterate arr
    9. for (int i = 0; i < arr.size(); i++) {
    10. int current = arr.get(i);
    11. System.out.println("arr at index " + i + ":" + current);
    12. }
  • 查找元素

    1. //Find an element
    2. //Time Complexity:O(N)
    3. //Find an element in c
    4. for (int i = 0; i < c.length; i++) {
    5. if (c[i] == 99) {
    6. System.out.println("We found 99 in c!");
    7. }
    8. }
    9. //Find an element in arr
    10. boolean is99 = arr.contains(99);
    11. System.out.println("Are we found 99 in arr?" + is99);
  • 数组的排序

    1. //Sort an array by built-in Lib
    2. c = new int[]{2, 3, 1};
    3. arr = new ArrayList<>();
    4. arr.add(2);
    5. arr.add(3);
    6. arr.add(1);
    7. //[2,3,1]
    8. System.out.println("c:" + Arrays.toString(c));
    9. System.out.println("arr:" + arr.toString());
    10. //From small to big
    11. //Time complexity:O(NLogN)
    12. Arrays.sort(c);
    13. Collections.sort(arr);
    14. //[1,2,3]
    15. System.out.println("c:" + Arrays.toString(c));
    16. System.out.println("arr:" + arr);
    17. //From big to small
    18. //Time complexity:O(NLogN)
    19. //For c, you can read an array in reverse
    20. //Arrays.sort(T[], Collections.reverseOrder());
    21. //For arr
    22. Collections.sort(arr, Collections.reverseOrder());
    23. //[3,2,1]
    24. System.out.println("arr:" + arr);

LeetCode练习

#485

#283

#27


参考:https://www.bilibili.com/read/cv8568185?spm_id_from=333.788.b_636f6d6d656e74.6