A detailed explanation of the use of vectors in Java

Source: Internet
Author: User

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, if the latter two methods are used. The system will initialcapacity the capacity of the vector object according to the parameters (that is, the vector object can store the size of the data), 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. But instead of a numeric value, you should be careful to convert the array 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 Insertelement (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 PlainCopyprint?
  1. Import Java.util.Vector;
  2. Import java.lang.*;
  3. Import java.util.Enumeration;
  4. Public class Vectorapp
  5. {
  6. public static void Main (String args[])
  7. {
  8. Vector v1 = new vector ();
  9. Integer integer1= New Integer (1);
  10. //Join As String Object
  11. V1.addelement ("one");
  12. //Add an integer to the object
  13. V1.addelement (Integer1);
  14. V1.addelement (Integer1);
  15. V1.addelement ("both");
  16. V1.addelement (new Integer (2));
  17. V1.addelement (Integer1);
  18. V1.addelement (Integer1);
  19. //Convert to string and print
  20. System.out.println ("The Vector v1 is:\n\t" +v1);
  21. //Inserts a new object into the specified position
  22. V1.insertelement ("Three",2);
  23. V1.insertelement (new Float (3.9),3);
  24. System.out.println ("The Vector v1 (used method
  25. Insertelementat () is:\n\t) "+v1);
  26. //Set the object at the specified location to the new object
  27. //The object after the specified position is postponed in turn
  28. V1.setelementat ("Four",2);
  29. System.out.println ("The vector v1 cused method Setelmentat () is:\n\t" +v1);
  30. V1.removeelement (Integer1);
  31. //Delete the object from the vector object V1 integer1
  32. //Because there are multiple integer1, start from scratch.
  33. //Find the first integer1 to delete.
  34. Enumeration enum = V1.elements ();
  35. System.out.println ("The vector v1 (used method removeelememt () is");
  36. While (enum.hasmoreelements ())
  37. System.out.println (enum.nextelement () +"");
  38. System.out.println ();
  39. //Use the enumeration class (enumeration) method to get each element of the vector object.
  40. System.out.println ("The position of Object1 (Top-to-botton):" +v1.indexof (Integer1));
  41. System.out.println ("The position of Object1 (tottom-to-top):" +v1.lastindexof (Integer1));
  42. //Find the location of the object Integer1 in different directions
  43. V1.setsize (4);
  44. System.out.println ("The new vector (resized the vector) is:" +V1);
  45. //Reset the size of the V1, the extra elements are discarded
  46. }
  47. }


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.

A detailed explanation of the 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.