Vector base of vectors in Java collection

Source: Internet
Author: User

Vector Vector :

Vectors are similar to dynamic arrays, vector and arrays, but once the array capacity is determined to be immutable, the capacity of the vectors is variable. Vectors can only hold any type of object and the capacity is unlimited, the array has no restrictions on the element type but has limited capacity.

Application: The vector is applicable to the frequent additions and deletions of elements and the element array is uncertain, the elements are objects,

The array is suitable for elements of the object number determination, underlying data type.

Construction Method:

public Vector ();

Public Vector (int initialcapacity); Specifies the initial capacity, which exceeds the capacity system by an automatic increase of one times

Public Vector (int initialcapacity,int capacityincrement), specifying initial capacity and growth increment, A capacityincrement of 0 is automatically incremented by default (the vector automatically grows more than the required memory space)

Public vector (collection<? extends e> c); Specifies the collection constructs the vector

Property:

protected int capacityincrement vector increment

Protected the number of elements of an int elementcount vector

protected Object elementdata[] vector member's buffer

Common methods:

void AddElement (Object obj), added to tail, 1 increase in capacity

int capacity () returns the vector capacity

Boolean contains (Object obj) to determine whether the vector contains the specified element

void Copyinfo (Object array[]) copies a vector to an array

Synchronized Object elementat (int index) returns the specified position element, which throws an arrayindexoutofboundsexception exception when the subscript is illegal

void ensurecapacity (int size) set vector minimum capacity to size

Synchronized Object firstelement () returns the first element, which throws a nosuchelementexception exception when the vector is empty

int indexOf (Object element) returns the element subscript, returns 1 if not present

int indexOf (Object element,int start) specifies the position at which to start searching for the element, returns the subscript value, returns 1 if not present

void Insertelementat (Object obj,int Index) is inserted at the specified position, and the contents of this position are moved backwards 1

Boolean IsEmpty () determines whether the vector is empty

Synchronized Object lastelement () returns the last element of the vector, which is thrown when the vector is empty nosuchelementexception

int lastIndexOf (Object Element) searches from the tail of the vector, returning the element subscript

int lastIndexOf (Object element,int start) Searches forward from the specified position, returning the element subscript

The Boolean removeelement (Object obj) deletes the specified element, if there are more than one delete only the first

void Removeallelements () Delete all, capacity becomes 0

void Removeelementat (int index) deletes the specified position element

void Setelementat (Object obj,int Index) replaces the specified location content

void setSize (int size) sets the vector length to size, the new length is less than the original length, and the element is lost, greater than the original length, automatically null

int size () returns the number of elements in the vector

String toString () vector converted to strings

Example:

Package test;

import java.util.Arrays;

import java.util.*;

Public class vectortest {

Public Static void Main (string[] args) {

vector<string> vector = new vector<string> ();

Vector.add ("Vector1");

Vector.addelement ("Vector2");

Vector.add ("Vector 3");

Vector.addelement ("Vector 4");

Vector.insertelementat ("Vector 5 insert", 2);

System. out. println ("1-vector:" +vector);

Vector.setelementat ("vector substitution 1", 1);

System. out. println ("2-Vector:" +vector+vector.size ());

System. out. println ("3-Vector:" +vector.lastelement ());

System. out. println ("4-Vector:" +vector.lastindexof ("Vector1"));

There is no element vector2

if (Vector.indexof ("Vector2") ==-1) {

System. out. println ("5-Vector:" +vector.indexof ("Vector2"));

}

System. out. println ("6-Vector:" +vector.contains ("Vector 4"));

String[] arr= new string[5];

Vector Copy to array

Vector.copyinto (arr);

System. out. println ("7-Array Printing:" +arrays. ToString(arr));

Vector.removeallelements ();

System. out. println ("8-Vector:" +vector.isempty ());

Vector = new vector<string> (Arrays. Aslist(arr));

System. out. println ("9-vector:" +vector);

System. out. println ("10-vector toString print:" +vector.tostring ());

for (String vec:vector) {

System. out. println (VEC);

}

}

}

Vector base of vectors in Java collection

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.