search
HomeJavajavaTutorialWhat is the principle of dynamic array in java

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 [];

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"};

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!

Statement
This article is reproduced at:亿速云. If there is any infringement, please contact admin@php.cn delete
How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?How does IntelliJ IDEA identify the port number of a Spring Boot project without outputting a log?Apr 19, 2025 pm 11:45 PM

Start Spring using IntelliJIDEAUltimate version...

How to elegantly obtain entity class variable names to build database query conditions?How to elegantly obtain entity class variable names to build database query conditions?Apr 19, 2025 pm 11:42 PM

When using MyBatis-Plus or other ORM frameworks for database operations, it is often necessary to construct query conditions based on the attribute name of the entity class. If you manually every time...

How to use the Redis cache solution to efficiently realize the requirements of product ranking list?How to use the Redis cache solution to efficiently realize the requirements of product ranking list?Apr 19, 2025 pm 11:36 PM

How does the Redis caching solution realize the requirements of product ranking list? During the development process, we often need to deal with the requirements of rankings, such as displaying a...

How to safely convert Java objects to arrays?How to safely convert Java objects to arrays?Apr 19, 2025 pm 11:33 PM

Conversion of Java Objects and Arrays: In-depth discussion of the risks and correct methods of cast type conversion Many Java beginners will encounter the conversion of an object into an array...

How do I convert names to numbers to implement sorting and maintain consistency in groups?How do I convert names to numbers to implement sorting and maintain consistency in groups?Apr 19, 2025 pm 11:30 PM

Solutions to convert names to numbers to implement sorting In many application scenarios, users may need to sort in groups, especially in one...

E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?E-commerce platform SKU and SPU database design: How to take into account both user-defined attributes and attributeless products?Apr 19, 2025 pm 11:27 PM

Detailed explanation of the design of SKU and SPU tables on e-commerce platforms This article will discuss the database design issues of SKU and SPU in e-commerce platforms, especially how to deal with user-defined sales...

How to set the default run configuration list of SpringBoot projects in Idea for team members to share?How to set the default run configuration list of SpringBoot projects in Idea for team members to share?Apr 19, 2025 pm 11:24 PM

How to set the SpringBoot project default run configuration list in Idea using IntelliJ...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

DVWA

DVWA

Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

EditPlus Chinese cracked version

EditPlus Chinese cracked version

Small size, syntax highlighting, does not support code prompt function

Atom editor mac version download

Atom editor mac version download

The most popular open source editor