The application of vector in Java programming

Source: Internet
Author: User
Tags array constructor integer interface
The programming Vector class provides the ability to implement a scalable array, and the array becomes larger as more elements are added. After you delete some elements, the array becomes smaller.

The
Vector has three constructors,


public Vector (int initialcapacity,int capacityincrement)


public Vector (int initialcapacity)


public Vector ()

The
Vector runtime creates an initial storage capacity initialcapacity, and the storage capacity is 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 neither the initial storage capacity nor the capacityincrement.

The
Vector class provides access methods that support similar array operations and operations associated with vector size. An array-like operation allows the addition of vectors, deleting and inserting elements. They also allow you to test the contents of the vector and retrieve the specified elements, and size-related operations allow you to determine the byte size and the number of elements in the vector.


is currently used for the vector increase, delete, interpolation function Example Description:


addelement (Object obj)


adds the component to the tail of the vector, with a size of 1 and a vector capacity of 1


 


insertelementat (Object obj, int index)


adds the component to the set index, and thereafter the content moves backwards by 1 units


 


setelementat (Object obj, int index)


adds the component to the set index, where the content is substituted.


removeelement (Object obj) removes the contents of this component from the vector.


Removeallelements () Removes all components in the vector, with a vector size of 0.


For 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 ("two");


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 ();


}


}

Changes in the vector of
:


1. One 2. One 3. One 4. Zero 5.zero 6. Zero 7. Zero


8.


two two one one one one one


Three two two two two


three oop three three


three three four


 


In addition, Vector plays an important role in the transfer of parameters.


has a canvas (Canvas) and a (panel) in the applet, and the Panel contains the information that the user wants to enter, passing the parameters to the Canvas based on the information, then using an interface (Interface) in Java, In an interface, a vector is required to pass these parameters. In addition, you can use this method when one class passes to another class parameter.


For example:


 


Import Java.util.Vector


Interface codeselect{


vector codeselect=new vector ();


             }


Display Mathematical Information


Vector (0) is deposited in the student number


Vector (1) deposited in subject


 


in the panel when the user in the TextField and choice select their own content, the program

The
passes the value through the event response 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 time, the vector has been deposited into the student number and subject index number (0 for the language, 1 for mathematics, 2 for politics).


and canvas This value in the


 


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)


{


Display Chinese information


}


else if (t==1)


{


Display Mathematical Information


 


}


else if (t==2)


{


Show political Information


}


}


} 











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.