Turn: Use of vectors in Java

Source: Internet
Author: User
Tags int size

Tag: res represents a DataSet element find object OSI return implementation Delete

Ext.: https://www.cnblogs.com/zhaoyan001/p/6077492.html

Vectors can be used to achieve an automatically growing array of objects.
Java.util.vector provides a vector class (vector) to implement a function similar to a dynamic array. There is no concept of pointers in the Java language, but if you use pointers correctly and flexibly, you can greatly improve the quality of your programs. For example, in c,c++, the so-called "dynamic array" is usually implemented by pointers. To compensate for this shortcoming, Java provides a rich library of libraries to facilitate the use of programmers, which is one of the vector classes. In fact, the flexible use of arrays can also complete the function of vector classes, but the vector class provides a large number of methods to greatly facilitate the user's use.
Once you have created an object of a vector class, you can insert objects of different classes at will, that is, without regard to the type and the capacity of the pre-selected vector, and you can easily find it. For unknown or reluctant to pre-define the size of the array, and the need to frequently find, insert, delete the work of the situation. You might consider using a vector class.

The vector class provides three ways to construct:
Public Vector ()
Public vector (int initialcapacity,int capacityincrement)
Public vector (int initialcapacity)

Using the first method, the system automatically manages the vectors. Using the latter two methods, the system will initialcapacity the capacity of the vector object (that is, the vector object can store the size of the data) according to the parameters, when the number of data actually stored exceeds the capacity. The system expands the vector object storage capacity.

The parameter capacityincrement is given an expanded value for each expansion. When the capacityincrement is 0, it is no longer expanded by one time, and this function optimizes storage. A variety of methods are available in the vector class for user-friendly use:

Insert Function:
(1) Public final synchronized void adddelement (Object obj)
Inserts obj at the tail of the vector. Obj can be any type of object. Objects of different classes can also be inserted into the same vector object. However, the inserted value should be an object instead of a numeric value, so be careful to convert the value to the appropriate object when inserting values.
For example: To insert an integer 1 o'clock, do not call V1.addelement (1) directly, the correct method is:
Vector v1 = new vector ();
Integer integer1 = new Integer (1);
V1.addelement (Integer1);
(2) Public final synchronized void Setelementat (Object obj,int index)
The object at index is set to obj, and the original object is overwritten.
(3) Public final synchronized void Insertelementat (Object obj,int index)
Inserts obj at the position specified by index, and the original object and subsequent objects are deferred sequentially.

Remove Features:
(1) Public final synchronized void removeelement (Object obj)
Remove obj from the vector, and if there are multiple, try starting from the vector header and delete the first vector member found with the same obj.
(2) Public final synchronized void Removeallelement ();
Delete all objects of vector
(3) Public fianl synchronized void removeelementat (int index)
Delete an object where Index refers to

Query search function:
(1) Public final int indexOf (Object obj)
Searches for obj from the vector header, returns the subscript corresponding to the first obj encountered, and returns 1 if the obj does not exist.
(2) public final synchronized int indexOf (Object obj,int index)
Search for obj starting at the subscript indicated by index.
(3) Public final int lastindexOf (Object obj)
Reverses the search for obj from the tail of the vector.
(4) Public final synchornized int LastIndex (Object obj,int index)
The inverse of the tail-to-head search from the subscript at index indicates obj.
(5) Public final synchornized firstelement ()
Gets the first obj in the vector object
(6) Public final synchornized Object Lastelement ()
Gets the last obj of a vector object

Example: Vectorapp.java
[Java] View plain copy print?
Import Java.util.Vector;
Import java.lang.*;
Import java.util.Enumeration;
public class Vectorapp
{
public static void Main (String args[])
{
Vector v1 = new vector ();
Integer integer1= new Integer (1);
Join As String Object
V1.addelement ("one");
The object that is added as an integer
V1.addelement (Integer1);
V1.addelement (Integer1);
V1.addelement ("both");
V1.addelement (New Integer (2));
V1.addelement (Integer1);
V1.addelement (Integer1);
Convert to string and print
System.out.println ("The Vector v1 is:\n\t" +v1);
Inserts a new object into the specified position
V1.insertelementat ("three", 2);
V1.insertelementat (New Float (3.9), 3);
System.out.println ("The Vector v1 (used method
Insertelementat () is:\n\t) "+v1);
Sets the object at the specified location to the new object
The object after the specified position is deferred in turn
V1.setelementat ("Four", 2);
System.out.println ("The vector v1 cused method Setelmentat () is:\n\t" +v1);
V1.removeelement (Integer1);
Removes an object from the vector object V1 integer1
Since there are multiple integer1, start from scratch.
Find the first integer1 found in the delete.
enumeration enum = V1.elements ();
System.out.println ("The vector v1 (used method removeelememt () is");
while (Enum.hasmoreelements ())
System.out.print (enum.nextelement () + "");
System.out.println ();
Use the enumeration class (enumeration) method to get each element of the vector object.
System.out.println ("The position of Object1 (Top-to-botton):" +v1.indexof (Integer1));
System.out.println ("The position of Object1 (tottom-to-top):" +v1.lastindexof (Integer1));
Find the location of the object Integer1 in different directions
V1.setsize (4);
System.out.println ("The new vector (resized the vector) is:" +V1);
Reset the size of the V1, the extra elements are discarded
}
}

Operation Result:
E:\java01>java Vectorapp
The Vector v1 is:[one,1,1,two,2,1,1]
The vector v1 (used method Insetelementat ()) is:
[one,1,three,3.9,1,two,2,1,1]
The vector v1 (used method Setelementat ()) is:
[one,1,four,3.9,1,two,2,1,1]
The Vector v1 (Useed method Removeelement ()) is:
One four 3.9 1, 2 1 1
The position of Object1 (Top-to-botton): 3
The position of Object1 (Botton-to-top): 7
The new vector (resized the vector) is:
[one,four,3.9,1]

(1) class vector defines the method
Public final int size ();
This method is used to get the number of vector elements. Their return values are the number of elements actually present in the vector, not the vector capacity. You can call the method capacity () to get the capacity value.

Method:
Public final synchronized void setsize (int newsize);
This method is used to define the size of the vector, and if the number of existing members of the vector object exceeds the value of newsize, the excess elements of more than a portion will be lost.
(2) An object in the program that defines the enumeration class enumeration is an interface class in Java.util,
A method for enumerating data collections is encapsulated in enumeration.
The enumeration provides a method Hasmoreelement () to determine if there are other elements and methods in the collection Nextelement () to determine if there are other elements and methods in the collection nextelement () to get the next element. Using these two methods, you can sequentially get the elements in the collection.
Methods available in vectors:
Public final synchronized enumeration elements ();
This method corresponds to a vector object to an enumeration type. Such methods are also available in other classes in the Java.util package to allow the user to obtain the corresponding enumeration type.

Turn: Use of vectors in Java

Related Article

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.