What is an array

By the time you look at this question, the answer is probably already in your head. In layman’s terms, an array is a set of contiguous memory Spaces that store a set of data of the same type. You can also say that arrays are used to store data of the same type in a contiguous set of memory Spaces.

Contiguous memory space: keyword contiguous, why need contiguous memory space, is not contiguous not ok?

According to the Java Virtual Machine Specification, the Java heap can be in a physically discontinuous memory space, but logically it should be considered contiguous, just as we use disk space to store files without requiring each file to be contiguous. But for large objects (typically groups of objects), most virtual machine implementations will most likely require contiguous memory space for simplicity of implementation and storage efficiency.

It is the contiguous memory space and the same type of data that make data randomly accessible. For example, when deleting or inserting data, a lot of moving work is needed to ensure continuity.Copy the code

How to create

In Java, use [] to define an array. There are three common creation methods

//1) Define array with length
int[] arr = new int[3];

//2) Use data to define arrays directly
int[] arr = new int[] {1.2.3};

//3) Use data to define directly
int[] arr = {1.2.3};
Copy the code

Note: Once an array is defined, its length cannot be changed. If you need to change it, you can define a new data and copy the original data

Different data types are stored differently in memory

  • Int, long, char, and other basic data types

The space requested by new is stored on the heap and arR is stored on the stack. Arr stores the first address of the array space.

As you can see from the figure, arrays of primitive data types are still defined as arrays of data structures in Java. The data in the array is of the same type and stored in a contiguous memory space.

  • Store object type data

In Java, object arrays store the location of objects in memory, not the objects themselves. The objects themselves are not stored consecutively in memory, but scattered everywhere.

How to use

The assignment

int[] arr = new int[3];
arr[2] = 3;// Assign subscript 2 to 3

arr[2] = 6;// If the value is assigned again, the original data will be overwritten
Copy the code

insert

  1. Create an array and expand the original array
  2. Assigns data from the original array to the new array
  3. Move data greater than I back one bit
  4. Assign to k

If the array is just a container for storing functions and the data elements are unordered; To avoid large-scale data movement, you can directly move the KTH bit of data to the end of the array element, and put the new element directly into the KTH position. Otherwise, you can only move the data element.

delete

Similar to inserting data, data needs to be moved for memory continuity.

If we want to delete multiple data, do we need to move the data every time we delete it? Just like in life, when we have something we want to throw away, do we take it out every time? Of course not. In fact, we just put it in Garbage, and it doesn’t disappear, it’s just marked as garbage. Garbage cans are only cleaned when they are full.

We can take advantage of this idea by “marking” multiple pieces of data when deleting them (throwing them in the trash) and then moving them (cleaning the trash) after the marking is complete (the trash is full).

Some roommates might say, “Throw things in the trash. Isn’t that moving?”

For example, we can also write “this is garbage” on the things we want to throw away.

If you are familiar with the JVM, you may find that this is the idea of the tag garbage collection algorithm.

JVM tag removal algorithm:

Most mainstream virtual machines use the reachable analysis algorithm to judge whether objects are alive or not. In the marking stage, all GC ROOTS will be traversed and all objects reachable by GC ROOTS will be marked as alive. Only when the marking is complete will the clean-up begin. There will be a special article behind the specific content.

Copy an array

There is a method in System that makes a copy of an array called ArrayCopy

Static void arrayCopy (Object SRC, int srcPos, Object Dest, int destPos, int Length) : Copy an array.

  • The SRC argument represents the source array.
  • Parameter srcPos: indicates the starting position of the source array.
  • The dest argument represents the destination array.
  • DestPos: indicates the starting position of the destination array.
  • Parameter length: indicates how many elements to copy.

The index of crossing the line

When you create an array, you already specify the array length. If the array length is k and the index of the array starts at 0, then the range of the index is 0–(k-1),

int[] arr = new int[3];
arr[3] = 6;/ / will be thrown Java. Lang. ArrayIndexOutOfBoundsException
Copy the code

Application scenarios

  1. In the usual development is mostly to do business development, we directly use the container is enough, because it will encapsulate the operation details of data, it is very convenient to use. Arrays are preferred if performance is critical
  2. In development, if the data size is known and there are no complex operations, you can also use arrays directly.