List, rule List, ArrayList, Vector,
At present, it seems that we cannot write high-quality essays. Apart from looking at what others write, we can write more and do what we can.
The most frequently heard and seen word is "Do not forget your mind ". Indeed, when many people reach a certain level, they do not know how to do it. (Including my former Idol, najie, lost for a long time after winning the FA net Championship)
To sum it up, remember the reason for putting yourself at a certain level or height, instead of thinking about what you should do to adapt to the height.
Of course, each stage has what should be done and what can be done at each stage. Here, we will talk about how to maintain the quality of moving forward.
Problems in the initial stage of the project:
In Java, List is an interface, and javaslist inherits the abstract class AbstractSequentialList to implement the List interface (the AbstractSequentialList class inherits the AbstractList class)
ArrayList and Vector inherit the AbstractList class and implement the List interface.
AbstractList implements some location-related operations (such as get, set, add, remove) of the List.FirstSet class that implements random access methods. AbstractList is the minimum implementation of the List interface.
(Some people also say that AbstractList is a redundant reference. Why does extends HashSet <E> extend HashSet <e> and implement Set <E>)
This is too far away. An error occurs when the listing list is used in the project.
1. assign a List value to another List
1 rows list <Integer> getData = new rows list <Integer> (); 2 rows list <Integer> saveData; 3 // Error 1: The saveData object references the getData object (similar to the pointer pointing to ), it is not to pass the getData object to savaData 4 saveData = getData; 5 // Error 2: shallow clone, it is still the same object 6 saveData = (shortlist <Integer>) getData. clone (); 7 8 // correct 9 rows list <Integer> saveData = new partition list <Integer> (getData ); 10 // correct 2nd 11 partition list <Integer> saveData = new partition list <Integer> (); 12 saveData. addAll (getData); 13 // correct 3 14 for (Integer s: getData) 15 saveData. add (s );
2. implement multi-threaded operations (add and delete) in a List)
Because the producer list is not synchronized by threads, the method I choose is to lock the modules for operations such as addition and deletion.
KeywordsThe locks obtained by synchronized are all Object locks, instead of using a piece of code or method (function) as a lock.Which thread executes the method with the synchronized keyword first, and which thread willHold the lock of the object to which this method belongsOther threads can only wait. But there is a premise: Since the lock is called an object lock, it is bound to be related to the object, soMultiple Threads must access the same object..
// Code block 1: Put the statement containing the added element into the lock synchronized (test. myApp. getFlag () {// myApp is a class that inherits the Application class. getFlag () gets a String object defined in it (String constant) if (Assets2.start = 1) {float [] I = {event. getAccelerationX (), event. getAccelerationY (), event. getAccelerationZ ()}; test. myApp. getAsensorData (). addLast (I);} else test. myApp. getAsensorData (). clear ();}
// Code block 2: Lock the code block containing the elements in the delete List. Note that the lock is the same as the code block. synchronized (test. myApp. getFlag () {if (! Test. myApp. getAsensorData (). isEmpty ()&&(! Test. myApp. getGsensorData (). isEmpty () {test. myApp. getAsensorData (). removeFirst (); // test. myApp. getGsensorData (). removeFirst ();}}
Test. myApp. getFlag () is only used as an object. If it is used in non-android Java, String flag = "110"; Use flag instead of test. myApp. getFlag.
Test. myApp. getAsensorData () is used to obtain the sequence list defined as a global variable. in Java, the sequence list <float []> asensorData = new sequence list <> (); is used to replace test with asensorData. myApp. getAsensorData ().
Knowledge addition: differences between vector, rule list, And ArrayList
1. Vector and ArrayList are stored in the memory in the form of arrays, while the ArrayList is stored in the form of a linked list.
2. synchronized is added to the Vector method, but not to ArrayList and sorted list. Vector is thread-safe (also known as thread synchronization ).
3. The sort list is suitable for insert and delete operations at specified locations and is not suitable for searching. () Vector and ArrayList are suitable for search, and are not suitable for insertion or deletion at specified locations.
4. When the memory of ArrayList is insufficient, the default value is 50% + 1. (For example, the original size is 10, and the size after expansion is 10 + 5 + 1 = 16); when the growth factor is greater than 0, a growth factor is added by default, otherwise, the default expansion is doubled (when the expansion factor is greater than 0, the length of the new array is the length of the original array + expansion factor, or the length of the new array is twice the length of the original array .). The sort list is a two-way linked list, with no initialization size or expansion mechanism. It is just like adding a list before or after it.
Add: HashMap resizing (the initial size is 16, and the default expansion factor is 0.75 (the initialization size and expansion factor can be specified). The ratio of the current size to the current capacity exceeds the expansion factor, it will be resized, And the size after expansion is doubled.
For example, the initial size is 16 and the expansion factor is 0.75. When the capacity is 12, the proportion is already 0.75. Trigger expansion. The size after expansion is 32.
Writing an article is quite time-consuming. Sometimes it's half done. If you forget it, you don't have to store it next time ~ I hope that I will be more proficient in the future. I hope I can train my organizational skills and review what I have encountered before.