Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analysis of Vector modified element Source Code of java Construction method

2025-04-02 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.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report