Array of Java data structures (Java Academy of power nodes)

What is the use of arrays When you need to arrange the 30 numbers in size, it is a good choice to store them in a data structure such as array. When you are a class teacher, the array is also very useful to record the number of absences of those students every time. Arrays can be inserted, deleted, searched, etc.

1) Create and allocate memory

There are two data types in Java, basic type and object type, also known as reference type. In Java, arrays are regarded as objects, and the new operator is used when creating arrays.

Since it is an object, array is a reference to the array. According to the Java programming idea (I) - everything is the memory allocation of the object, array will open up space in the stack, and the space stores the address where the array is stored. The place where the object is really saved is right. The new operation opens up the required space in the heap, and then array points to the header address.

initialization:

The value in the array after new is initialized to 0 by default, while the initialization of the object is empty and null. Of course, it can also be initialized by {}.

2) Use after array encapsulation

Encapsulate the entire array and replace the number of arrays with number. When inserting data, you don't have to worry about which subscript to insert. Of course, you can also customize a specific subscript method.

The method is relatively simple and will not be introduced. However, there is a disadvantage in delete. In fact, it only moves left from the deleted element. Therefore, although the number is reduced, the last element is not deleted, but it is hidden when the display output is displayed, The next time you insert an element, the new element will replace the position of the last element.

3) Search optimization - binary search

Binary search presupposes that the array is ordered. At first, the index is written as end minus start, resulting in an endless loop. In fact, what we need is addition. 1,6,7。 If index = 2, value = 7, 3 is less than 7, and start = 3, then index needs the middle number between 3 and 4, so it is added and divided by 2,6, less than 7, start = 4, and find to 7.

4) Large o representation

Set n as the total number of data, and the time for adding and inserting a data is K. Then the total linear search time t = k * n / 2, because the search is about half of the comparison number.

For binary search, t = k * log2 (n). In the representation of large o, O can be regarded as order of, which means about. K / 2 is also a constant, so it can be regarded as O (n).

The disadvantage of array is that the size is fixed and the search is slow. If you want to often search millions of data, will you still use array? No, so the selection of data structure should be combined with the specific actual situation to achieve the maximum efficiency value.

The above is the array of Java data structures introduced by Xiaobian to you (Java college sorting of power nodes). I hope it will be helpful to you. If you have any questions, please leave me a message and Xiaobian will reply to you in time. Thank you very much for your support for the programming tips website!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>