Data Structure - Table - ArrayList, LinkedList

The "table" in the data structure can be understood in the same way as the database table, but the conceptual understanding is: the table has many records (data elements), and each record has the same form and consists of many data items. . However, to understand the data structure of the table, it is necessary to pay attention to the storage form of its data in memory.

 
There are two types of tables:
Linear lists, and linked lists.
A linear table is a continuous storage space in memory; for example, if the content of a table is: [1, 2, 3], it may be stored in memory as follows:
1
2
3
It can be seen from this structure that as long as you know the location of the first element in memory. You can easily know the position of other elements. Because each element occupies the same space. So, if we know that the first element: 1 is in memory number: 1000; and each element occupies 8 memory spaces; then the memory space of the second element: 2 is: 1000 + 8; and so on. Therefore, accessing data elements in a linear list is fast . Its disadvantage is precisely because it is a continuous piece of memory space. So, if you add to the middle or remove an element in the middle. All other elements must be moved. For example: I add an element at the front: 0. Then 1, 2, and 3 will be shifted back by one; or, if I delete 1, then 2 and 3 will be shifted forward by one; Estimate the operation time: Add at the first element, to move all elements. The time it takes is the time X to add the element plus the time to move other elements: N, adding an element at the end doesn't require moving any elements. The time is only the time to add an element: X . So the average time to add an element is (N + 2X)/2; the delete operation is the same as the add operation; so, when changing the length of the linear table, it will move the element spend a lot of time on it.
in JAVA. The most direct application of the linear table is the array: Array; but Array must specify the length of its elements at the time of initialization. Such as:
int[] arr = new int[5]; The length must be specified, but the content can be left blank. It needs to know the length, and then go to open up a memory space. Once the array is initialized, it cannot add or remove elements to it. But you can set the element value to be empty;
If you need to expand the length of the array, JAVA provides another class: ArrayList.
All virtual data containers in JAVA inherit from the interface: Collection; ArrayList inherits from: List. And List inherits from Collection; Collection provides several basic methods: add();clear();remove();size ();toArray();iterator() etc.;
ArrayList provides a more flexible usage:
List<Long> lists = new ArrayList<Long>();
ArrayList<Long> lists = new ArrayList<Long>();
The above usage uses generics. And lists do not specify a length. It can be freely extended in the program. But this feature means that it may run out of memory at runtime and have problems; arrays are of a specified length, so if there is not enough memory, you can't initialize them at all, and once initialized, you can use them as you like.
ArrayList, and Array are both implementations of linear lists in data structures.
linked list;
The storage of the linked list is chained, it does not force the data to be in a contiguous memory space; it can be stored scattered. Therefore, in addition to the value of the element, each element of it also includes some additional information; for example, where is its next element.
The element in the most basic chain storage consists of two parts: the element value and the position of the next element;
It can be understood as follows:
memory space: value next element space
50 46 empty
.
100               10             200
 .
 .
200               15             50
When this chain exists, we definitely know where the chain head is. We assume that the chain head above is at memory space 100. Then the data form represented by the above linked list is: [10,16,46]; knowing where the chain head is, if you want to find other elements. It must start from the head of the chain and search for the value in the next memory space in turn; so the search time is N/2; obviously it is slower than the linear table. If it is to remove or add elements. For example, now we want to change: [10,16,46] to: [10,16,30,46]; what we have to do is to put 30 in memory, assuming its memory address is 500; then put Change the value of the next element space of the value of 16 to 500; and change the value of the next element space corresponding to the value of 30 to 50 (the memory space corresponding to the value of 46); it looks simpler than a linear table. But if you look closely, you will find that to add or remove an element, you must know who its previous element is; but now only the next element of a wireless is stored in the data. If you want to find the previous element, you have to traverse from the head of the chain again. This wastes a lot of time and is not as easy to use as a linear table. So at this time, another linked list appeared: the double linked list . It is different from the basic linked list in that each element not only stores the space of the next element, but also stores the space of the previous element.
The implementation of linked list in JAVA is achieved through the class: LinkedList. LinkedList finally traces back to the Collection interface;
So it also has methods such as: add().clear() mentioned above.

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326646282&siteId=291194637