In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-09-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
The Vector of the java construction method modifies the element source code analysis. In view of this problem, this article introduces the corresponding analysis and solution in detail, hoping to help more partners who want to solve this problem to find a more simple and feasible method.
Preface
Add, delete, modify, and modify elements, Vector provides three methods, including one of the iterators, but this article only analyzes the two methods of modifying elements of Vector itself, and the methods in the iterator will be analyzed separately
Set (int,E) method to analyze public synchronized E set (int index, E element) {if (index > = elementCount) throw new ArrayIndexOutOfBoundsException (index); E oldValue = elementData (index); elementData [index] = element; return oldValue;}
A method used to modify a subscript, which is modified with synchronized, to protect the shared variable elementData from being modified by multiple threads at the same time, and only one thread can execute the method at the same time. Other threads that do not acquire the object lock will block at the entrance of the method, waiting for the release of the object lock. The passed parameter index indicates which subscript element to modify, and the passed parameter element indicates which new element object to modify.
1. Check whether the subscript is legal
A legal subscript must be less than the number of elementCount held by the Vector object, because only the subscript from 0 to elementCount-1 holds the element, and the other subscript throws an ArrayIndexOutOfBoundsException object to remind the user that there is something wrong with the passed subscript.
2. Get the saved element object at the current subscript
Using the elementData () method, you can pass in the subscript, and the old element object obtained will be temporarily saved in the local variable oldValue.
3. Update the new element object to the specified subscript
Just assign the new element object at the subscript index of elementData.
4. Return the old element object to the caller
SetElementAt (E obj int) method to analyze public synchronized void setElementAt (E obj, int index) {if (index > = elementCount) {throw new ArrayIndexOutOfBoundsException (index + "> =" + elementCount);} elementData [index] = obj;}
In addition to the anti-human use of the new element object as the first parameter, an important difference is that this method does not return the old element object!
On the java construction method of the Vector modified element source code analysis questions shared here, I hope the above content can be of some help to you, if you still have a lot of doubts have not been solved, you can follow the industry information channel to learn more related knowledge.
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.
The market share of Chrome browser on the desktop has exceeded 70%, and users are complaining about
The world's first 2nm mobile chip: Samsung Exynos 2600 is ready for mass production.According to a r
A US federal judge has ruled that Google can keep its Chrome browser, but it will be prohibited from
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
About us Contact us Product review car news thenatureplanet
More Form oMedia: AutoTimes. Bestcoffee. SL News. Jarebook. Coffee Hunters. Sundaily. Modezone. NNB. Coffee. Game News. FrontStreet. GGAMEN
© 2024 shulou.com SLNews company. All rights reserved.