The application of vector in Java programming

Source: Internet
Author: User
Tags constructor function examples

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

Now for the frequent use of the vector increase, delete, interpolation function examples described:

AddElement (Object obj)

Add the component to the tail of the vector with a size plus 1, and the vector capacity is 1 larger than before

Insertelementat (Object obj, int index)

Add the component to the set index, and then move the contents back 1 units

Setelementat (Object obj, int index)

Add 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[]){
   Vector v=new Vector();
   v.addElement("one");
   v.addElement("two");
  v.addElement("three");
  v.insertElementAt("zero",0);
  v.insertElementAt("oop",3);
  v.setElementAt("three",3);
     v.setElementAt("four",4);
   v.removeAllElements();
}
}

Changes in vectors:

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

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.