JDK源码阅读-------自学笔记(五)(浅析数组)

一、数组基础

1、定义和特点

  • 数组也可以看做是对象,数组变量属于引用类型,数组中每个元素相当于该队形的成员变量,数组对象存储在堆中.

2、初始化数组

  • 常用类初始化
    1          // 整型初始化
    2         int[] integerInitialization = new int[10];
    View Code
  • 对象初始化
    1         // 对象初始化
    2         User[] usersInitialization = new User[10];
    View Code

3、数组赋值

  • 动态初始化(根据数组角标)
    1         // 整型初始化
    2         int[] integerInitialization = new int[10];
    3 
    4         // 整型赋值
    5         integerInitialization[0] = 1;
    6         integerInitialization[1] = 2;
    View Code
  • 循环赋值
    1         // 整型初始化
    2         int[] integerInitialization = new int[10];
    3 
    4         // 循环赋值
    5         for (int i = 0; i < integerInitialization.length; i++) {
    6             integerInitialization[i]=10*i;
    7         }
    View Code
  • 静态初始化
    常用类初始化
    1         // 整型初始化
    2         int[] integerInitialization = {1,2,3,4,5,6,7,8,9};
    View Code

    对象初始化
    1         // 对象静态初始化
    2         User[] usersInitialization ={new User(101,"龙五"),new User(102,"李四")};
    View Code
  • 默认初始化
    注意:当默认初始化的时候,会按照设置的数组大小自动填入数组长度多个0,布尔型为false,引用型为null.
    1         // 默认初始化
    2         int[] integerInitialization = new int[3];
    3 
    4         // 测试查看默认值
    5         System.out.println("第一个元素"+integerInitialization[0]);
    6         System.out.println("第二个元素"+integerInitialization[1]);
    7         System.out.println("第三个元素"+integerInitialization[2]);
    View Code

4、数组遍历取值

  • 循环
    1         // 整型初始化
    2         int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    3 
    4         // 循环获取元素
    5         for (int i = 0; i < integerInitialization.length; i++) {
    6             System.out.println("第" + (i + 1) + "个元素" + integerInitialization[i]);
    7         }
    View Code
  • foreach语句
    1         // 整型初始化
    2         int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9};
    3 
    4         // foreach获取元素
    5         for (int i : integerInitialization) {
    6             System.out.println("第" + i + "个元素" + i);
    7         }
    View Code

二、数组拷贝

  • 容器拷贝,底层就是数组拷贝
  • 使用方法
    源码分析:
     1     /**
     2      * @param src     源数组(从这个数组中拷出元素).
     3      * @param srcPos  源数组起始位置.
     4      * @param dest    目的数组(拷贝到这里元素).
     5      * @param destPos 目的数组起始位置.
     6      * @param length  拷贝数组的长度(拷贝多少个元素到目的数组).
     7      * @throws IndexOutOfBoundsException 拷贝会产生数组越界的异常.
     8      * @throws ArrayStoreException       源数组和目的数组类型要一致,否则,会产生类型不一致的异常.
     9      * @throws NullPointerException      源数组和目的数组中有一个为空,就会产生空指针异常.
    10      */
    11     public static native void arraycopy(Object src, int srcPos,
    12                                         Object dest, int destPos,
    13                                         int length);
    View Code


    使用实例:

     1         // 整型初始化
     2         int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9};
     3 
     4         // 目的数组默认初始化
     5         int[] destPosition = new int[10];
     6 
     7         // 数组拷贝,从integerInitialization的第一个位置起拷贝8个元素到目的数组第二个位置起
     8         System.arraycopy(integerInitialization, 0, destPosition, 1, 8);
     9 
    10         System.out.println("数组中得到的拷贝元素:" + Arrays.toString(destPosition));
    View Code
  • 数组删除的本质,也是拷贝实现的方式
     1 class DemoApplicationTests {
     2 
     3 
     4     public static void main(String[] args) {
     5 
     6         // 整型初始化
     7         int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9};
     8 
     9         // 删除第五个元素,就是6
    10         integerInitialization = deletedElements(integerInitialization, 5, 5, integerInitialization.length);
    11 
    12         // 删除后的结果
    13         System.out.println("数组中得到的拷贝元素:" + Arrays.toString(integerInitialization));
    14 
    15 
    16     }
    17 
    18     /**
    19      * 数组删除元素的本质,是数组自身的拷贝
    20      * <p>
    21      * 算法:
    22      * 数组从startCopyLocation个位置开始拷贝,startCopyLocation后的元素被拷贝了,然后向前移动了一位重新放回数组
    23      *
    24      * @param objects           需要修改的数组
    25      * @param startCopyLocation 拷贝元素的起始位置
    26      * @param deletedLocation   存放元素的起始位置
    27      * @param length            拷贝几个元素
    28      * @return 删除后的数组
    29      */
    30     private static int[] deletedElements(int[] objects, int startCopyLocation, int deletedLocation, int length) {
    31 
    32         int[] newList = objects;
    33 
    34         System.arraycopy(newList, startCopyLocation + 1, newList, deletedLocation, length - deletedLocation - 1);
    35 
    36         newList[length - 1] = 0;
    37 
    38         return newList;
    39 
    40     }
    41 
    42 
    43 }
    View Code

三、数组扩容

  • 算法:先定义一个更大的数组,然后将原来数组的内容原封不动的拷贝到新数组中
     1 class DemoApplicationTests {
     2 
     3 
     4     public static void main(String[] args) {
     5 
     6         // 整型初始化
     7         int[] integerInitialization = {1, 2, 3, 4, 5, 6, 7, 8, 9};
     8 
     9         // 在2的位置添加一个元素10
    10         integerInitialization = addElement(integerInitialization, 2, 10);
    11 
    12 
    13         // 删除后的结果
    14         System.out.println("数组中添加元素:" + Arrays.toString(integerInitialization));
    15 
    16 
    17     }
    18 
    19 
    20     /**
    21      * 插入元素
    22      *
    23      * @param ints            需要插入元素的数组
    24      * @param elementLocation 插入元素的位置
    25      * @param value           插入的值
    26      * @return 新的数组
    27      */
    28     private static int[] addElement(int[] ints, int elementLocation, int value) {
    29 
    30         // 数组扩容
    31         int[] newElements = new int[ints.length << 1];
    32 
    33         // 拷贝elementLocation个元素
    34         System.arraycopy(ints, 0, newElements, 0, elementLocation + 1);
    35 
    36         // 添加要插入的元素
    37         newElements[elementLocation + 1] = value;
    38 
    39         // 在插入的元素后,把数组原来后边的元素拷贝进来
    40         System.arraycopy(ints, elementLocation + 1, newElements, elementLocation + 2, ints.length - elementLocation - 1);
    41 
    42         return newElements;
    43     }
    44 
    45 }
    View Code


原文地址:https://www.cnblogs.com/liuyangfirst/p/12364850.html