In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
In this article Xiaobian for you to introduce in detail "what is the principle of dynamic array in java", the content is detailed, the steps are clear, and the details are handled properly. I hope that this article "what is the principle of dynamic array in java" can help you solve your doubts.
I. the basic concept of array
What is an array?
I'm afraid it's the array that is most used in peacetime.
It is the most widely used data structure, it is a collection of elements of the same data type (either a basic type or a custom type) in a certain order, and they are stored together in this order in memory. There are one-dimensional array, two-dimensional array, multi-dimensional array.
The popular understanding is that we usually put a flock of sheep or cattle in a circle, which is equivalent to an array container, and each sheep is equivalent to an element.
The above concept needs to know these words: the same data type, a certain order, collection, 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 function of the declaration is like telling someone that I am going to take a shower, and the effect of creating it is as if I am really going to take a bath. So how do you declare an array?
Int [] students; int students []
We can see from above that there are two ways, but the first one is generally recommended. After all, the first one looks a little 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 to create an array, but basically the same, here are several ways to java.
/ / first: int [] students = new int [50]; / / second: String [] colors = {"red", "blue", "black"}
From the above, you can find that creating an array is so easy, don't worry, there are actually a lot of knowledge points to master in these three ways. In fact, there is a link in the creation of an array called array initialization. For example, I created an array, but these values may not have been in the array container at first. So when will these values be available? That is, when did the system put the red, blue, and so on I declared into the array container? This process is the initialization of the array. How is the array initialized?
Array initialization is divided into static initialization and dynamic initialization:
Static initialization: the programmer explicitly specifies the initial value of each array element when the array is initialized. The length of the array is determined by the system. Of the three ways to create an array above, the third is static initialization. The second is also, but it is a simplified way of static initialization.
Dynamic initialization: the number of elements must be specified during dynamic initialization. The number of array elements during dynamic initialization is unknown and must be specified. The first one above is.
4. Classification of arrays
There may be a question in this title, is there any classification in the array? It's just putting the same type of elements together. Actually this is not so. Here is a good classification for you:
* * divided according to whether it is ordered: * * ordered array and unordered array.
According to whether the array can be expanded: static array and dynamic array.
First, let's look at static arrays: an array that allocates memory in the stack during compilation, cannot change the storage space during operation, and is automatically released by the system after running.
Let's look at dynamic arrays: dynamic arrays, as opposed to static arrays. The length of the static array is predefined and cannot be changed once the size is given throughout the program. This is not the case with dynamic arrays, which can be resized as needed by the program. The memory space of a dynamic array is allocated (that is, dynamically allocated) from the heap. It allocates storage space by executing the code. These statements are allocated only when the program executes them. The programmer is responsible for freeing memory himself.
The principle of dynamic Array in java
There is an existing array:
Int [] data = new int [5]
The array can no longer add elements, so we initialize a new array with a capacity of 10, that is, twice the arr capacity of the array: int [] newData = new int [10]
Then assign all the elements of the original array to the new array.
Then point the reference arr of the original array to the new array.
Comparison of static and dynamic arrays:
For static arrays, it is easy to create, no need to release after use, and easy to reference, but not being able to change its size after creation is its Achilles' heel! 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 the needs of the program.
2. the characteristics of the array
After mastering the basic concepts above, let's take a look at the characteristics of the array. The characteristics of the array are also based on its classification. For example, the characteristics of the ordered array must be ordered, and it is convenient for us to find the data. Unordered our side inserts and deletes data. So what we are talking about here is the common feature of all arrays, that is, the general feature: back to the previous article, the characteristic is to look at time efficiency and space efficiency.
1. The length of the array is fixed. When the length is exceeded, you can only create a new array and pass in the value of the old array.
two。 The storage type of an array is single, and the same array can only store data of the same data type.
3. Arrays can only access data through subscripts.
Third, the use of the array scene
The greatest advantage of arrays over containers is efficiency. In Java, array is the most efficient way to store and randomly access object reference sequence. Array is a simple linear sequence, which makes element access very fast. Array has the advantage of high efficiency, but the price is that the size of array object is fixed. This also makes arrays impractical at work. We should prefer containers in java rather than arrays.
Fourth, the underlying implementation of the array
The underlying implementation here is also compared with the Java language. For example, in future articles, I will also cooperate with the container of linked list implementation in Java for data structures such as linked lists.
Java provides great collection API and collection classes such as ArrayList and HashMap, which are internally based on arrays. Java jvm throws an ArrayIndexOutOfBoundException if the program tries to access an invalid array index.
What is the principle of implementing arrays in the Java language?
This involves the compilation principle, I can only say that this is a compilation specification. In the specification, for example, int in int [] tells the computer that this is an integer data, [] tells the computer that this is a continuous memory address space, to put it simply, the storage space of a 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.
In HotSpot VM, for 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 the arraylength bytecode simply reads the _ length field. JVM array object is a special object, its Object Header than ordinary objects have a word to store the length of the array, length will be compiled into the corresponding bytecode to read the field.
After reading this, the article "what is the principle of dynamic arrays in java" has been introduced. If you want to master the knowledge of this article, you still need to practice and use it to understand it. If you want to know more about related articles, please follow the industry information channel.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.