Application of Vector in Java Programming

zhaozj2021-02-08  375

The VECTOR class provides a function of achieving a growing array, with more elements to join, the array changes. After deleting some elements, the array becomes smaller.

There are three constructors,

Public Vector (int InitialCapacity, int CapacityIncrement)

Public Vector (Int InitiaLCapacity)

Public vector ()

The vector is running an initial storage capacity initialcapacity, and the storage capacity is incrementally increased in the CapacityInCrement variable. The initial storage capacity and CapacityInCrement can be defined in the constructor of the Vector. The second constructor only creates an initial storage capacity. The third constructor does not specify whether the initial storage capacity is not specified in CapacityInCrement.

The access method provided by the Vector class supports similar array operations and computational operations with the Vector size. The calculation of similar arrays allows the vector to increase, delete, and insert elements. They also allow the contents of the test vector and retrieve the specified element, and the calculations associated with the size allow determination byte size and the elements in the vector.

For example, for example, it is now used, deleted, interpolated, and exemplified:

AddElement (Object Obj)

Add the components to the tail of the tail, at the same time, 1, vector capacity than before 1

INSERTETELEMENTAT (Object Obj, Int Index)

Add the components to the findings, this subsequent content moves 1 unit

SETELEMENTAT (Object Obj, Int Index)

Add the components to the findings, the content here is replaced.

RemoveEleMent (Object Obj) removes the contents of this component.

RemoveAllelements () removes all components in the vector, the vector size is 0.

E.g:

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 Two One One One ONE

Three Two Two Two Two

Three OOP Three Three

Three Three Four

In addition, Vector play a pivotable role in parameter transmission.

There is a Canvas and a (Panel) in the applet, while the user wants to enter information, passes the parameters to Canvas based on this information, at this time, in Java, in the interface, Use a vector to pass these parameters in an interface. In addition, this method can be used in one class to another class parameter. E.g:

Import java.util.vector

Interface code {

Vector codeselect = new vector ();

}

Show mathematical information

VECTOR (0) deposits the student number

VECTOR (1) Depository

In Panel, when users choose the content you request in TextField and Choice, the program

Pass the value into the vector vector by an event response.

If you are 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 ("political");

Add (s);

Add (c);

**

}

Public Boolean Handleevent (Event Event) {

IF (event.id == Event.Action_event) {

Event.target.instanceof TextField

{

CodeResult.SetElementat (s.getText (), 0);

}

Else if (Event.Target IntanceOf Choice)

{

CodeResult.selementat (new integer (c.getSelectedIndIndex ()), 1);

}

}

}

}

At this time, the vector number and subject index quotation marks (0 are languages, 1 is politics).

This value is obtained in Canvas,

Public class codecanvas extends canvas {

Public void code {

}

Public void point {

String Str;

Int T;

Str = (string) CodeResult.Elementat (0);

T = (New Integer (Codeselect.ementat (1) .tostring ())))))). INTVALUE ();

IF (t == 0)

{

Display language information

}

Else IF (t == 1)

{

Show mathematical information

}

ELSE IF (t == 2)

{

Show political information

}

}

}

转载请注明原文地址:https://www.9cbs.com/read-947.html

New Post(0)