Vector usage in JAVA

Source: Internet
Author: User

Vector usage in JAVA
Java. util. Vector provides a Vector class to implement functions similar to dynamic arrays. There is no pointer concept in the Java language, but correct and flexible use of pointers can indeed greatly improve the quality of the program, for example, the so-called "dynamic array" in C and C ++ is generally implemented by pointers. To make up for this defect, Java provides a wide range of class libraries for programmers to use. The Vector class is one of them. In fact, the flexible use of arrays can also complete the functions of vector classes, but a large number of methods provided in vector classes greatly facilitate the use of users.
Compared with ArrayList, the Vector thread is secure, that is, synchronous.
After creating a Vector class object, you can insert objects of different classes to it at will, without taking into account the type or pre-selected vector capacity, and convenient search. Vector classes can be used when you do not know or do not want to pre-define the size of an array and frequently search, insert, or delete it. Vector classes provide three construction methods:
Public vector ()
Public vector (int initialcapacity, int capacityIncrement)
Public vector (int initialcapacity)
Using the first method, the system automatically manages vector objects. If the last two methods are used, the system sets the capacity of the vector object based on the initialcapacity parameter (that is, the size of the data that the vector object can store). When the number of actually stored data exceeds the capacity, the system will expand the storage capacity of vector objects.
The capacityIncrement parameter specifies the expansion and recharge for each expansion. When capacityIncrement is 0, it is doubled each time. This function can be used to optimize storage. Various methods are provided in the Vector class for your convenience:
Insert Function
(1) public final synchronized void addElement (Object obj)
Insert obj to the end of the vector. Obj can be an object of any class. You can insert objects of different classes to the same vector object. But the inserted value should be an object rather than a value. Therefore, when inserting a value, you must convert the value to the corresponding object.
For example, to insert an integer of 1, do not directly call v1.addElement (1). the correct method is:
Vector v1 = new Vector ();
Integer integer1 = new Integer (1 );
V1.addElement (integer1); www.2cto.com
(2) public final synchronized void setElementAt (object obj, int index)
Set the object at index to obj, and the original object will be overwritten.
(3) public final synchronized void insertElementAt (Object obj, int index)
Insert obj at the position specified by index. The original object and subsequent objects are postponed in turn.
Delete feature
(1) public final synchronized void removeElement (Object obj)
Delete obj from the vector. If multiple vectors exist, start the test from the vector header and delete the first vector member that is the same as obj.
(2) public final synchronized void removeAllElement ()
Delete all objects in the vector.
(3) public final synchronized void removeElementlAt (int index)
Delete the object in the place indicated by index.
Search
(1) public final int indexOf (Object obj)
Search for obj from the vector header and return the subscript corresponding to the first obj. If this obj does not exist,-1 is returned.
(2) public final synchronized int indexOf (Object obj, int index)
Search for obj from the subscript indicated by index.
(3) public final int lastIndexOf (Object obj)
Search for obj from the end of the vector.
(4) public final synchronized int lastIndexOf (Object obj, int index)
Reverse search for obj from the end to the header at the subscript indicated by index.
(5) public final synchronized Object firstElement ()
Obtain the first obj in the vector object.
(6) public final synchronized Object lastelement ()
Obtain the last obj in the vector object.
Instance
After learning about the most basic method of vectors, let's take a look at the example VectorApp. java.
Example: VectorApp. java
Import java. util. Vector;
Import java. lang .*;
// This sentence should not be required, but the original text is as follows:
Import java. util. Enumeration;
Public class VectorApp
{
Public static void main (String [] args)
{
Vector <Integer> v1 = new Vector <Integer> (); // support for [1] is added after jdk1.5!
Integer integer1 = new Integer (1 );
V1.addElement ("one ");
// Add a String object
V1.addElement (integer1 );
V1.addElement (integer1 );
// Add an Integer object
V1.addElement ("two ");
V1.addElement (new Integer (2 ));
V1.addElement (integer1 );
V1.addElement (integer1 );
System. out. println ("The vector v1 is: \ n \ t" + v1 );
// Convert v1 to a string and print it
V1.insertElementAt ("three", 2 );
V1.insertElementAt (new Float (3.9), 3 );
System. out. println ("The vector v1 (used method insertElementAt () is: \ n \ t" + v1 );
// Insert a new object to the specified position. The objects after the specified position are postponed in sequence.
V1.setElementAt ("four", 2 );
System. out. println ("The vector v1 (used method setElementAt () is: \ n \ t" + v1 );
// Set the object at the specified position as the new object
V1.removeElement (integer1 );
// Delete the integer1 object from Vector object v1 due
Multiple integer1 exists, so start from scratch
Locate and delete the first integer1
Enumeration enum = v1.elements ();
System. out. print ("The vector v1 (used method removeElement () is :");
While (enum. hasMoreElements ())
System. out. print (enum. nextElement () + "");
System. out. println ();
// Use the Enumeration method to obtain each element of a vector object
System. out. println ("The position of object 1 (top-to-bottom ):"
+ V1.indexOf (integer1 ));
System. out. println ("The position of object 1 (tottom-to-top ):"
+ V1.lastIndexOf (integer1 ));
// Search for the location of integer1 in different directions
V1.setSize (4 );
System. out. println ("The new vector (resized the vector) is:" + v1 );
// Reset the v1 size, and the excess elements are discarded.
}
}
Running result:
E: \ java01> java VectorApp
The vector v1 is:
[One, 1, 1, two, 2, 1]
The vector v1 (used method insertElementAt () is:
[One, 1, three, 3.9, 1, two, 1]
The vector v1 (used method setElementAt () is:
[One, 1, four, 3.9, 1, two, 1]
The vector v1 (used method removeElement () is:
One four 3.9 1 two 2 1 1
The position of object 1 (top-to-bottom): 3
The position of object 1 (tottom-to-top): 7
The new vector (resized the vector) is:
[One, four, 3.9, 1]
E: \ java01>
You can clearly understand the functions of the above methods from the running results in Example 1. There are also several points to explain.
(1) the class Vector defines the method.
Public final int size ()
This method is used to obtain the number of vector elements. The returned value is the number of actually existing elements in the vector instead of the vector capacity. You can call capactly () to obtain the capacity value.
Method:
Public final synchronized void setsize (int newsize)
This method is used to define the vector size. If the number of existing members of a vector object exceeds the value of newsize, excess elements will be lost.
(2) An object of the Enumeration class is defined in the program.
Enumeration is an interface class in java. util. It encapsulates methods for Enumeration data sets in Enumeration.
In Enumeration, the hawMoreElement () method is provided to determine whether there are other elements and methods in the collection to obtain the next element. These two methods can be used to obtain the elements in the collection in sequence.
Methods provided in Vector:
Public final synchronized Enumeration elements ()
This method maps vector objects to an enumeration type. Most of the other classes in the java. util package also have such methods, so that you can obtain the corresponding Enumeration type.
 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.