Vector in Java in detail and purpose

Source: Internet
Author: User

The Vector class provides the ability to implement a growing array, and as more elements are added to it, the array becomes larger. After you delete some elements, the array becomes smaller. Vector has three constructors, public vector (Int. initialcapacity,int capacityincrement) public vector (int. initialcapacity) Public V Ector ()----The Vector runtime creates an initial storage capacity of initialcapacity, which is the incremental growth defined by the capacityincrement variable. The initial storage capacity and capacityincrement can be defined in the vector's constructor. The second constructor creates only the initial storage capacity. The third constructor does not specify either the initial storage capacity or the capacityincrement. The access methods provided by the----vector class support similar array operations and operations related to vector size. An array-like operation allows the addition, deletion, and insertion of elements in the vector. They also allow you to test the contents of the vector and retrieve the specified elements, and the size-dependent operations allow you to determine the size of the bytes and the number of elements in the vector. ----is now used for the use of vector increment, delete, interpolation function Example Description: addelement (Object obj)----The component to the end of the vector, while the size plus 1, the vector capacity is larger than before 1 Insertelementat (object obj, int index)----Add the component to the index at which it was set, and thereafter the content moves backwards by 1 units setelementat (object obj, int i NDEX)----Add the component to the index, where the contents are replaced. ----RemoveelEment (Object obj) removes the contents of this component from the vector. ----removeallelements () moves all the components in the vector to a vector size of 0. ----Example: Import Java.lang.System; Import Java.util.Vector; Import java.util.Emumeration; public class avector{public static void Main (String args[]) {0. Vector v=new vector (); 1. V.addelement ("one"); 2. AddElement ("both"); 3. V.addelement ("three"); 4. V.insertelementat ("Zero", 0); 5. V.insertelementat ("Oop", 3); 6. V.setelementat ("three", 3); 7. V.setelementat ("Four", 4); 8. v.removeallelements (); }} Vector changes: 1. One 2. One 3. One 4. Zero 5.zero 6. Zero 7. Zero 8.  Two-one one one one-three-three OOP three three three three four----in addition, vectors play a lifting The role of gravity. ----have a canvas (canvas) and a (panel) in the applet, and the Panel puts the information that the user wants to enter, and then passes the parameters to the canvas based on that information, in Java With an interface (Interface), a vector is used in the interface to pass these parameters. In addition, this method can be used to pass a class to another class parameter. ----Example: Import Java.util.Vector interface codeselect{Vector codeselect=new Vector ();  Display mathematical information vector (0) deposited into the student number vector (1) deposited into the subject----in the panel when the user chooses what they want in TextField and choice, the program----through the event The value should be passed to vector vectors. ----If in the Panel class: public void Codepanel extends panel{public void init () {* *. TextField s=new TextField (); Choice c=new Choice (); C. AddItem ("language"); C.additem ("mathematics"); C.additem ("politics"); Add (s); Add (c); * *} public Boolean handleevent (event event) {if (event.id==event.action_event) {if (event.target.instanceof Textfield) { Coderesult.setelementat (S.gettext (), 0); } else if (Event.target intanceof Choice) {coderesult.setelementat (New Integer (C.getselectedindex ()), 1);}} }}----At this point, the student number and subject index number (0 for Chinese, 1 for mathematics, and 2 for politics) have been deposited in the vector. ----and get this value in the Canvas, public class Codecanvas extends canvas{public void code{} public void paint{String str; int t ; Str= (String) coderesult.elementat (0); t= (New Integer (Codeselect.elementat (1). toString ())). Intvalue (); if (t==0) {displays language information} else if (t==1) {displays mathematical information} else if (t==2) {Show Political Information}}} 

Vector in Java in detail and purpose

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.