当前位置:网站首页>Array acquaintance

Array acquaintance

2020-11-08 18:59:00 Java house

What is an array

​ When you see this problem , The answer must be in your head . In layman's terms : An array is a set of contiguous memory spaces , To store a set of data of the same type . It can also be said that the function of array is to transfer the same type of data , Stored in a set of contiguous memory spaces .

Continuous memory space : The key words are continuous , Why continuous memory space is needed , Can't it be continuous ?

​ according to 《Java Virtual machine specification 》 The provisions of the ,Java The heap can be in a physically discontinuous memory space , But logically it should be seen as continuous , It's like we use disk space to store files , It is not required that every document be kept continuously . But for large objects ( Typically like an array object ), Most virtual machine implementations are simple to implement 、 Storage efficiency considerations , It's likely to require continuous memory space .

    Because of continuous memory space and the same type of data , So that the data has the characteristics of random access , However, success is also a storm , It's hard to lose . because " Wind and cloud " It makes many operations of data inefficient , For example, when deleting or inserting data , To ensure continuity , You need to do a lot of moving work .

How to create

Java in , Use [] To define an array . There are three common ways to create

//1) Use length to define arrays 
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};

Be careful : Once an array is defined , Its length cannot be changed , If you need to change , You can define a new data , Put the original data copy Just go ahead

Different data types are stored in memory in different ways

  • int、long、char Data of basic data types

new The space for the application is on the heap ,arr Stored on the stack .arr The first address of the array space is stored .

From the picture , stay Java in , The basic data type array still conforms to the definition of array in data structure . The data in the array is of the same type 、 And stored in a continuous memory space .

  • Store object type data

stay Java in , The object array stores the address of the object in memory , Not the object itself . Objects themselves are not continuously stored in memory , It's all over the place .

How to use

assignment

int[] arr = new int[3];
arr[2] = 3;// The subscript is 2 The assignment is  3

arr[2] = 6;// If you assign it again , The original data will be covered 

Insert

  1. New array , Expand the original array
  2. Assign the original array data to the new array
  3. Will be bigger than the i Of the data moved back one bit
  4. Assign values to the k Location

If the array is just a container for storing functions and the data elements inside are out of order ; To avoid massive data movement , You can put the k The data of bits is moved to the end of array elements , Put the new element directly into the k A place , On the contrary, we can only move data elements .

Delete

It's similar to inserting data , For memory continuity , You also need to move data .

If you want to delete multiple data , Do we have to move data every time we delete it ? It's like in life , When we have something we want to throw away , Do we take things out every time ? Of course not. , In fact, we just put it in the garbage pass , And it's not gone , Just being " Mark " It's rubbish . Only the trash can is full , To clean the trash can .

We can use this idea , When you want to delete multiple data , First put it " Mark "( Throw it in the trash ), When the marking is complete ( The trash can is full ), In the deletion operation , Move data ( Clean up the trash can ).

Some roommates may say :" Throw things in the trash , Didn't that move the position ?"

It can be understood in this way : Throwing things in the trash can is just " Mark " An implementation of , For example, we can also write on the things to be discarded " It's rubbish ".

be familiar with JVM My roommate , It may be found that this is not the idea of mark clean garbage collection algorithm ?

JVM Tag clearing algorithm :

Most mainstream virtual machines use reachability analysis algorithm to judge whether the object is alive or not , In the marking phase , Will traverse all GC ROOTS, Will all GC ROOTS Reachable objects are marked as alive . Only when the tagging is done , The cleanup will start . The specific content will be introduced in a special article later .

Index out of bounds

When creating an array , The array length has been specified . If the array length is k, The index value of the array is from 0 Start , So the index range is 0--(k-1),

int[] arr = new int[3];
arr[3] = 6;// Will throw out  java.lang.ArrayIndexOutOfBoundsException

Application scenarios

  1. In normal development, most of them are business development , It's enough for us to use containers directly , Because it encapsulates the operational details of the data , It is very convenient to use . If the performance requirements are very high , Then use arrays first
  2. In development , If the data size is known , And there's no complicated operation , You can also use arrays directly .

版权声明
本文为[Java house]所创,转载请带上原文链接,感谢