Vector class usage in Java programming Learning notes _java

Source: Internet
Author: User
Tags int size

Java.util.vector provides vector classes (vectors) to implement functions similar to dynamic arrays. There is no concept of pointers in the Java language, but if you use pointers correctly and flexibly, you can actually improve the quality of your programs. For example, in c,c++, the so-called "dynamic array" is generally implemented by pointers. To remedy this shortcoming, Java provides a rich library of classes for programmers to use, one of which is the vector class. In fact, a 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 an object of a vector class is created, it is possible to insert objects of different classes at will, that is, without having to take into account the type or the capacity of a pre-selected vector, and to find it easily. For those who do not know beforehand or are unwilling to predetermine the size of an array, and need to find, insert, delete work frequently. You can consider using vector classes.
The vector class implements a dynamic array. and ArrayList and similar, but the two are different:

    • Vectors are accessed synchronously.
    • Vector contains a number of traditional methods, which do not belong to the collection framework.
    • Vectors are mainly used in cases where the size of the array is not known beforehand, or only if an array can be changed.

The vector class supports 4 methods of construction.

1. The first construction method creates a default vector with a default size of 10:

Vector ()
2. The second construction method creates a vector of the specified size.

Vector (int size)
3. The third construction method creates a vector of the specified size, and the increment is specified with incr. Increment represents the number of elements that the vector increments each time.

Vector (int size,int incr)
4. The fourth construction method creates a vector that contains the C element of the collection:

Vector (Collection c)

Using the first method, the system will automatically manage vectors, if the latter two methods are used. The system will, according to the parameters, Initialcapacity set the capacity of the vector object (that is, the vector object can store the size of the data), when the actual number of data stored exceeds the capacity. The system expands the vector object storage capacity.

Parameter capacityincrement given the extended value for each extension. When the capacityincrement is 0, it is not extended one time, using this function can optimize storage. The vector class provides a variety of ways to facilitate user use:

Insert Function:
(1) public final synchronized void adddelement (Object obj)
Inserts obj into the tail of the vector. Obj can be any type of object. For the same vector object, you can also insert objects of different classes in them. Instead of inserting a value, you should be careful to convert the array to the corresponding object.
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)
Sets the object at index to obj, and the original object is overwritten.
(3) Public final synchronized void Insertelement (Object obj,int index)
Inserts obj at the location specified by the index, and the original object and subsequent objects are later postponed in turn.

Delete feature:
(1) public final synchronized void removeelement (Object obj)
Remove obj from the vector, and if there are more than one, try to start from the vector head and delete the first vector member found to be the same as obj.
(2) Public final synchronized void Removeallelement ();
Remove all objects of a vector
(3) Public fianl synchronized void removeelementat (int index)
Remove an object from the location indicated by index

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 this obj does not exist.
(2) public final synchronized int indexOf (Object obj,int index)
Starts searching for obj from the subscript represented by index.
(3) Public final int lastindexof (Object obj)
Start the reverse search for obj from the tail of the vector.
(4) Public final synchornized int lastindex (Object obj,int index)
Search for obj from tail to head from the subscript indicated by index.
(5) Public final synchornized firstelement ()
Gets the first obj in a vector object
(6) Public final synchornized Object Lastelement ()
Gets the last obj of a vector object

Instance
The following procedure illustrates several of the methods supported by this collection:

Import java.util.*; public class Vectordemo {public static void main (String args[]) {//initial the size is 3, increment is 2 Vector v
   = New Vector (3, 2);
   System.out.println ("Initial size:" + v.size ());
   System.out.println ("Initial Capacity:" + v.capacity ());
   V.addelement (New Integer (1));
   V.addelement (New Integer (2));
   V.addelement (New Integer (3));
   V.addelement (New Integer (4));

   System.out.println ("Capacity after four additions:" + v.capacity ());
   V.addelement (New Double (5.45));
   System.out.println ("Current Capacity:" + v.capacity ());
   V.addelement (New Double (6.08));
   V.addelement (New Integer (7));
   System.out.println ("Current Capacity:" + v.capacity ());
   V.addelement (New Float (9.4));
   V.addelement (New Integer (10));
   System.out.println ("Current Capacity:" + v.capacity ());
   V.addelement (New Integer (11));
   V.addelement (New Integer (12));
   System.out.println ("A:" + (Integer) v.firstelement ()); SysteM.out.println ("last element:" + (Integer) v.lastelement ());
   if (V.contains (New Integer (3)) System.out.println ("Vector contains 3.");
   Enumerate the elements in the vector.
   Enumeration venum = V.elements ();
   System.out.println ("\nelements in Vector:");
   while (Venum.hasmoreelements ()) System.out.print (venum.nextelement () + "");
  System.out.println ();

 }
}

The results of the above instance compilation run are as follows:

Initial size:0
Initial capacity:3
capacity after four additions:5 current
capacity:5 current
capacity : 7 Current
capacity:9-
element:1 last
element:12
Vector contains 3.

Elements in Vector:
1 2 3 4 5.45 6.08 7 9.4 10 11 12

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.