Detailed usage of Vector in java.util.Vector

xiaoxiao2021-03-06  124

ArrayList will be fast than Vector, he is not synchronized, if the design involves multi-thread, or use the Vector better

Import java.util. *;

/ ** * Demonstrate the use of Vector. Including the creation of the Vector, add an element to the VECTOR, remove elements from the Vector, * Statistics the number of elements in the vector and the elements in the VECTOR. * /

Public class vectorDemo {public static void main (String [] args) {// vector creation // uses VECTOR's constructor to create a Vector V = New Vector (4); / / Add an Element to VECTOR to use Add method Add element V.Add ("test0") directly; v.add ("test1"); v.add ("test2"); v.Add ("test2"); // Remove Elements V.Remove ("Test0") from VECTOR; // Delete Elements of Specified Content v.Remove (0); // Delete Elements Follow the Code Number // Number of Elements in Vector INT Size = V .size (); system.out.println ("Size:" size); // Traversing element for (int i = 0; i

-------------

The VECTOR class provides a function of achieving a growing array, with more elements to join, the array changes. After deleting some elements, the array becomes smaller. Vector has three constructors to create an initial storage capacity initialCapacity public Vector (int initialCapacity, int capacityIncrement) public Vector (int initialCapacity) public Vector () Vector operation, the storage capacity incrementally capacityIncrement variable definition of growth. The initial storage capacity and CapacityInCrement can be defined in the constructor of the Vector. The second constructor only creates an initial storage capacity. The third constructor does not specify whether the initial storage capacity is not specified in CapacityInCrement. The access method provided by the Vector class supports similar array operations and computational operations with the Vector size. The calculation of similar arrays allows the vector to increase, delete, and insert elements. They also allow the contents of the test vector and retrieve the specified element, and the calculations associated with the size allow determination byte size and the elements in the vector. Now, for example, the exemplary, deletion, interpretation of the exemplary, addelement (Object Obj), adds the component to the tail of the tail, at the same time, the vector capacity is more than the previous 1 INSERTELEMENTAT (Object Obj, int index) Add it to the index, then the subsequent content moves 1 unit setlementat (Object Obj, int index) to add components to the identifier, which is replaced. RemoveEleMent (Object Obj) removes the contents of this component. RemoveAllelements () removes all components in the vector, the vector size is 0. For example: import java.lang.system; import java.util.vector; import java.util.emumeration; public class aportor {public static void main (string args []) {0. Vector v = new vector (); 1. v.addeElement ("one"); 2. AddElement ("two"); 3. v.addeElement ("three"); 4. V.inSerTelementat ("zero", 0); 5. v.insertelementat ("OOP ", 3); 6. V.selementat (" Three ", 3); 7. V.SETELEMENTAT (" FOUR ", 4); 8. V.removeallelements ();}}}. Changes in the VECTOR: 2. One 3. ONE 4. Zero 5. Zero 6. Zero 7. Zero 8. Two Two One One One ONE Three THREE Three Three Four In addition, Vector play a pivotive role in parameter delivery .

转载请注明原文地址:https://www.9cbs.com/read-126162.html

New Post(0)