Java applications in Vector

Source: Internet
Author: User
By November Rain

Date: 8:41:19

The vector class provides the function of increasing arrays. As more elements are added to the array, the array becomes larger. After deleting some elements, the array becomes smaller.

Vector has three constructors,

Public vector (INT initialcapacity, int capacityincrement)

Public vector (INT initialcapacity)

Public vector ()

When a vector is run, an initial storage capacity initialcapacity is created. The storage capacity is incremental growth defined by the capacityincrement variable. The initial storage capacity and capacityincrement can be defined in the vector constructor. The second constructor only creates the initial storage capacity. The third constructor does not specify the initial storage capacity or capacityincrement.

The access method provided by the vector class supports operations similar to Array Operations and vector size operations. Operations similar to arrays allow adding, deleting, and inserting elements in vectors. They also allow testing the content of the vector and retrieving the specified elements. The size-related operations allow determining the byte size and the number of elements in the vector.

Examples of adding, deleting, and inserting vectors that are frequently used:

Addelement (Object OBJ)

Add the component to the end of the vector, and increase the size by 1. The vector capacity is 1 larger than the previous one.

 

Insertelementat (Object OBJ, int index)

Add the component to the specified index, and move the subsequent content to one unit.

 

Setelementat (Object OBJ, int index)

Add the component to the specified index. The content here is replaced.

Removeelement (Object OBJ) removes the content of the component contained in the vector.

Removeallelements () removes all components from the vector. The vector size is 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 vector:

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

8.

Two One

Three Two

Three OOP three

Three three four

 

In addition, vector plays an important role in parameter transmission.

In the applet, there is a canvas and a panel, and the Panel contains the information you want to enter. Based on this information, the parameters are passed to the canvas, in this case, an interface is used in Java, and a vector is required to pass these parameters in the interface. In addition, this method can be used when a class is passed to another class parameter.

For 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 you select the content you want in textfield and choice

Upload the value to the vector through the event response.

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 ){

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 Subject Index number are saved in the vector (0 is Chinese, 1 is mathematics, and 2 is politics ).

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)

{

Show Chinese Information

}

Else if (t = 1)

{

Display mathematical information

 

}

Else if (t = 2)

{

Display 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.