import java.util. *;
/ **
* demonstrates the use of Vector . Including the creation of Vector , Vector to add elements to remove elements from the Vector ,
* statistics the number of elements in the Vector and traverse elements in the Vector .
* /
public class VectorDemo {
public static void main (String [] args) {
/ / Vector creation
/ / use the Vector constructor method to create
Vector v = new Vector (4);
/ / add elements to the Vector
/ / add elements directly using the add method
v.add ("Test0");
v.add ("Test1");
v.add ("Test0");
v.add ("Test2");
v.add ("Test2");
/ / remove elements from the Vector
v.remove ("Test0"); / / delete the specified content element
v.remove (0); / / remove elements by index
/ / get the number of elements in the Vector has been
int size = v.size ();
System.out.println ("size:" + size);
/ / traverse the elements in the Vector
for (int i = 0; i
}
}
}
-------------
Vector class implements a growable array of features , as more elements are added one for the array becomes even greater. After deleting some elements in the array smaller.
Vector has three constructors,
public Vector (int initialCapacity, int capacityIncrement) public Vector (int initialCapacity) public Vector () Vector runtime creates an initial storage capacity initialCapacity, storage capacity is in capacityIncrement variables defined increment growth. Initial storage capacity and capacityIncrement Vector constructor can be defined . The second constructor creates only an initial storage capacity. The third constructor specify the initial storage capacity is neither specified nor capacityIncrement. Vector class provides access method supports a similar array of computing and related to the size of the operation with the Vector . Array-like computing allows vector add, delete and insert elements . They also allow the contents of test vectors and retrieve the specified element size-related computing allows determination bytes in size , and the number of elements in the vector is not . For frequently used right now to increase in volume , delete, insert Function Example Description:
addElement (Object obj) added to the component vector tail , while size plus one , vector capacity than before the big one
insertElementAt (Object obj, int index) set the component added to the index , then the contents of the backward moving a unit
setElementAt (Object obj, int index) set the component added to the index , where the contents are replaced . removeElement (Object obj) components of the vector containing the contents removed. removeAllElements () to remove all the components of the vector , vector size 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 ();
}
}
Vector of changes :
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 passed in the parameter plays a pivotal role. In the Applet has a canvas (Canvas) and a (Panel), while the Panel placed the user to enter the information , according to the information of the parameters passed to the canvas , then used in Java an interface (Interface), and in the interface required to pass the parameters of a Vector . In addition, in one class to another class of arguments you can use this method. For example:
import java.util.Vector
interface codeselect {Vector codeselect = new Vector ();} display mathematical information
Vector (0) into the Student ID
Vector (1) into the disciplines in the Panel TextField and Choice When users select their desired content, procedures in response to the value reached by the event vector in the Vector .
synchronization is a big problem , especially in multi-threading, and processes , and therefore, we have multiple threads at the same time an array operation, support synchronization vector is undoubtedly a good choice , in general needs to be more there is an element in the collection at the same time .
Java . util class Vector < ; E>
boolean add (E o)
Appends the specified element to the end of this vector .
void add (int index, E element)
specified position in this Vector Inserts the specified element .
boolean addAll (Collection c)
the specified Collection to all of the elements added to the end of this vector , in accordance with the specified collection 's iterator returns the order added these elements.
boolean addAll (int index, Collection c)
at the specified position in the specified Collection of all elements into this vector.
void addElement (E obj)
the specified component to the end of this vector , increasing its size by one .
int capacity ()
Back to the current capacity of this vector .
void clear ()
Removes all elements from this Vector .
Object clone ()
return one copy of the vector .
boolean contains (Object elem)
test whether the specified object in this vector components.
boolean containsAll (Collection c)
If this Vector contains all of the elements in the specified Collection , it returns true.
void copyInto (Object [] anArray)
components of this vector into the specified array.
E elementAt (int index)
return the component at the specified index .
Enumeration
return an enumeration of the components of this vector .
void ensureCapacity (int minCapacity)
increase the capacity of this vector ( if necessary ) to ensure that it can hold at least the minimum capacity argument specifies the number of components .
boolean equals (Object o)
Compares the specified object with this vector equality.
E firstElement ()
return the first component of this vector ( the item at index 0 ) .
E get (int index)
return vector elements in the specified location .
int hashCode ()
return the hash code value of this vector .
int indexOf (Object elem)
search for the given parameters of the first match , testing for equality using the equals method .
int indexOf (Object elem, int index)
search for the given parameters of the first match from the start the search at index , and using the equals method to test for equality .
void insertElementAt (E obj, int index)
Adds the specified object as a component in this vector is inserted into the specified index .
boolean isEmpty ()
test this vector has no components.
E lastElement ()
return the last component of this vector .
int lastIndexOf (Object elem)
Returns the specified object in this vector, the index of the last occurrence .
int lastIndexOf (Object elem, int index)
searching backwards for the specified object from the specified index to start the search , and returns an index.
E remove (int index)
remove this vector at the specified position elements.
boolean remove (Object o)
Remove the specified element in this vector the first match, if the vector does not contain the element, it is unchanged.
boolean removeAll (Collection c)
removed from this vector in the specified Collection contains all the elements .
void removeAllElements ()
Removes all components from this vector and sets its size to zero.
boolean removeElement (Object obj)
removed from this vector variable first ( lowest-indexed ) match.
void removeElementAt (int index)
delete the component at the specified index .
protected void removeRange (int fromIndex, int toIndex)
Remove from List in its index fromIndex (inclusive ) and toIndex ( not included ) between all the elements.
boolean retainAll (Collection c)
retained in this vector contains only the elements in the specified Collection .
E set (int index, E element)
replace this element with the specified vector element at the specified position .
void setElementAt (E obj, int index)
this vector at the specified index component to the specified object .
void setSize (int newSize)
set size of this vector .
int size ()
return the number of components in this vector .
List
Back view of the portion of this List , a range of elements from fromIndex (inclusive ) to toIndex ( not included ) .
Object [] toArray ()
returns an array containing this Vector in the correct order of all the elements.
toArray (T [] a)
returns an array containing this Vector in the correct order all of the elements ; runtime type of the returned array is specified array type.
String toString ()
returns a string representation of this vector , which contains the String representation of each element .
void trimToSize ()
Trims the capacity of this vector to be equal to the current size of the vector
------ Solution --------------------- -----------------------
hair tutorial ?
------ Solution ------------------------------------ --------
this should be very basic , and just started to learn ,
------ Solution -------------- ------------------------------
you should stick to your own blog. .
------ Solution ---------------------------------------- ----
can see, still thank landlord
------ For reference only ----------------------- ----------------
------ For reference only ---------------------------------- -----
------ For reference only ---------------------------------- -----
okay hey
没有评论:
发表评论