Java-Vector application in Java Programming

Source: Internet
Author: User

The Vector class provides the function of increasing the number of groups. As more elements are added to the group, the number groups become larger. After deleting a few elements, the number groups become smaller.

Vector has three structure functions,

Public Vector (int initialCapacity, int capacityIncrement)

Public Vector (int initialCapacity)

Public Vector ()

---- Create an initial storage capacity initialCapacity when the Vector is run. The storage capacity is an increment defined by the capacityIncrement variable. The initial storage capacity and capacityIncrement can be defined in the number of constructed functions of the Vector. The second structure function only creates the initial storage capacity. The number of letters created in the third statement does not specify the initial storage volume or the capacityIncrement.

---- The Vector class provides the access method to support operations such as number groups and operations related to the large and small Vector. Operations similar to a Number Group allow adding, deleting, and inserting elements into the volume. They also allow the test vector volume's internal capacity and the element defined by the cable, the computation related to big and small nodes allows you to determine the number of small nodes and the number of elements in the vector.

---- The example of adding, deleting, and inserting power of the current needle to the commonly used pair:

AddElement (Object obj)

---- Add the group parts to the end of the vector, increase the size of the Group by 1 at the same time, and increase the volume of the vector by 1

InsertElementAt (Object obj, int index)

---- Add the group to the selected cable reference, and then the internal volume moves one single bit to the back

SetElementAt (Object obj, int index)

---- Add the component to the selected cable reference, and the content in this position is replaced.

---- RemoveElement (Object obj) removes the content contained in the vector from the group.

---- RemoveAllElements () removes all parts from the vector, and the vector size is smaller than 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 ("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 Vector:

1. one 2. one 3. one 4. zero 5. zero 6. zero 7. zero

8.

Two one

Three two

Three oop three

Three four

---- In addition, the Vector plays a simple role in the parameter data delivery.

---- In the Applet, there is a Canvas and a Panel, and the Panel contains the information to be input by the user, the root information transmits the parameter numbers to the canvas. In this case, an Interface is used in Java ), in the interface, a Vector is required to pass these parameters. In addition, this method can be used when a class is passed to another class parameter.

---- Example:

Import java. util. Vector

Interface codeselect {

Vector codeselect = new Vector ();

}

Display mathematical information

Vector (0) saved student ID

Vector (1) stored in the subject

---- In Panel, when the user selects the content he wants in TextField and Choice, the process is

---- Pass the Event Response to the Vector.

---- False in the Panel class:

Public void codepanel extends Panel {

Public void init ()

{

**.

TextField s = new TextField ();

Choice c = new Choice ();

C. addItem (" ");

C. addItem ("Mathematics ");

C. addItem ("Politics ");

Add (s );

Add (c );

**

}

Public boolean handleEvent (Event event ){

If (event. id = Event. ACTION_EVENT ){

If(event.tar get. instanceof Textfield)

{

Coderesult. setElementAt (s. getText (), 0 );

}

Else if(event.tar get intanceof Choice)

{

Coderesult. setElementAt (new Integer (c. getSelectedIndex (), 1 );

}

}

}

}

---- At this time, the student ID and the question mark (0 is a text, 1 is a number, and 2 is a political rule) have been stored in the vector ).

---- This value is obtained 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)

{

Show Chinese Information

}

Else if (t = 1)

{

Display mathematical information

}

Else if (t = 2)

{

Display political information

}

}

}

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.