Home > Java > javaTutorial > What is the principle of dynamic array in java

What is the principle of dynamic array in java

WBOY
Release: 2023-05-02 17:40:07
forward
1405 people have browsed it

1. Basic concepts of arrays

1. What is an array?

Probably the most commonly used one is the array.

It is the most widely used data structure. It is a collection of elements of the same data type (can be a basic type or a custom type) arranged in a certain order. They are stored in memory in this order. Stored together in sequence. There are one-dimensional arrays, two-dimensional arrays, and multi-dimensional arrays.

The popular understanding is that we usually put a group of sheep or a group of cows in a circle. This circle is equivalent to an array container, and each sheep is equivalent to an element.

The above concept requires knowing these words: the same data type, a certain order, collection, and memory storage.

2. How to declare an array

As you can see from the title, declaring and creating an array are two different processes. The effect of the statement is like telling others that I am going to take a shower, and the effect of creation is like telling others that I am actually going to take a shower (inappropriate metaphor haha). So how to declare an array?

int[] students ; int students [];
Copy after login

From the above we can see that there are two ways, but the first one is generally recommended. After all, the first one looks more readable.

3. How to create an array

After we know how to declare an array, the next step is how to create an array. Different languages ​​have different ways of creating an array, but they are generally the same. Here are several methods in Java.

//第一种: int [] students = new  int[50]; //第二种: String [] colors =  {"red","blue","black"};
Copy after login

From the above, you can find that creating an array is so simple. Don’t worry, there are actually a lot of knowledge points that need to be mastered in these three methods. In fact, there is a link in the creation of an array called array initialization. For example, I create an array, but the values ​​may not be in the array container at the beginning. So when did these values ​​come into existence? That is, when did the system install the red, blue, etc. I declared into the array container? This process is the initialization of the array. How is the array initialized?

The initialization of the array is divided into static initialization and dynamic initialization:

  • Static initialization: The array is explicitly specified by the programmer during initialization. The initial value of array elements. The array length is determined by the system. Among the three ways to create an array above, the third is static initialization. The second one is the same, but it is a simplified way of static initialization.

  • Dynamic initialization: During dynamic initialization, the number of elements must be specified. During dynamic initialization, the number of array elements is unknown and must be specified. The first one above is.

4. Classification of arrays

You may have a question after seeing this title. Are there classifications for arrays? Isn’t it just to classify the same type of arrays? Put the elements together. actually not. Let’s give you a good classification:

** According to whether they are ordered or not: **Ordered arrays and unordered arrays.

According to whether the array can be expanded: static array and dynamic array.

Let’s look at static arrays first: arrays that allocate memory on the stack during compilation. The storage space cannot be changed during runtime and is automatically released by the system after runtime.

Let’s look at dynamic arrays: Dynamic arrays are relative to static arrays. The length of a static array is predefined and cannot be changed throughout the entire program once the size is given. This is not the case with dynamic arrays, which can be resized as the program needs. The memory space of dynamic arrays is allocated from the heap (that is, dynamically allocated). It allocates storage space by executing code. These statements are allocated when the program executes them. The programmer is responsible for freeing memory.

The principle of dynamic arrays in java

There is an existing array:

int [] data = new int[5];

What is the principle of dynamic array in java

The array can no longer add elements, so we initialize a new array with a capacity of 10, which is 2 times the capacity of the array arr: int [] newData = new int [ 10];

What is the principle of dynamic array in java

#Then assign all elements of the original array to the new array.

What is the principle of dynamic array in java

# Then point the reference arr of the original array to the new array.

What is the principle of dynamic array in java

Comparison of static arrays and dynamic arrays:

For static arrays, they are very convenient to create and do not need to be released after use. It is easy to reference, but the inability to change its size after creation is its fatal weakness! For dynamic arrays, it is troublesome to create and must be released by the programmer after use, otherwise it will seriously cause memory leaks. But its use is very flexible and the size can be dynamically allocated according to program needs.

2. Characteristics of arrays

After mastering the basic concepts above, let’s take a look at the characteristics of arrays. The characteristics of arrays are also based on their classification. For example, the characteristics of ordered arrays are definitely ordering. It is convenient for us to find data. Unordered We insert and delete data. So the characteristics mentioned here are the characteristics common to all arrays, that is, the general characteristics: back to the article I posted before, the characteristics are about time efficiency and space efficiency.

1. The length of the array is fixed. When it exceeds the length, you can only create a new array and pass the value of the old array into it;

2. Storage type of the array It is single, and the same array can only store data of the same data type.

3. Arrays can only access data through subscripts

3. Usage scenarios of arrays

The biggest advantage of arrays over containers is efficiency . In Java, an array is the most efficient way to store and randomly access a sequence of object references. An array is a simple linear sequence, which makes element access very fast. The advantage of an array is high efficiency, but for this, it costs a lot The price is that the size of the array object is fixed. This also makes arrays impractical at work. We should prefer containers in java instead of arrays.

4. The underlying implementation of the array

The underlying implementation here is also compared to the Java language. For example, in future articles, data such as linked lists I will also talk about the structure in conjunction with the container implemented by linked lists in Java.

Java provides great collection APIs and collection classes such as ArrayList and HashMap, which are all based on arrays internally. Java If the program tries to access an invalid array index, the jvm will throw ArrayIndexOutOfBoundException.

In Java language, what is the implementation principle of arrays?

This question involves the principle of compilation. I can only say that this is a compilation specification. In the specification, for example: the int in int[] tells the computer that this is an integer data, and [] tells the computer that this is a memory address space for continuous storage. To put it simply, the storage space for continuous data is an array, and the array is just a name. !!Array is a special type in Java, which is different from ordinary "instance of class" objects.

Taking HotSpot VM as an example, the answer is that there is a length field in the object header of the array object to record the length of the array. The implementation of arraylength bytecode only needs to read the _length field. The array object in JVM is a special object. Its Object Header has one more word than the ordinary object to store the length of the array. The length will be compiled into the corresponding bytecode to read this field.

The above is the detailed content of What is the principle of dynamic array in java. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template