How to Use vector in Java

Source: Internet
Author: User

Use of Vector

The underlying Array Structure of the vector class, which contains components that can be accessed using integer indexes. However, the vector size can be increased or reduced as needed to meet the needs of adding or removing items after the vector is created. Therefore, you do not need to consider whether the element is out of bounds or whether it will waste memory.

The iterator returned by the vector iterator and listIterator methods quickly fails: that is, it cannot execute operations concurrently. If a vector is modified from the structure at any time after the iterator is created (by means of the iterator's own remove or add method), The iterator throws ConcurrentModificationException. Therefore, in the face of concurrent modifications, the iterator quickly fails completely, rather than risking any uncertain behavior at any time in the future.

The Enumeration returned by the elements () method of vector is not a quick failure.

Code to illustrate this problem:

Package cn. list. demo; import java. util. enumeration; import java. util. iterator; import java. util. vector; public class VectorDemo {public static void main (String [] args) {Vector v = new Vector (); v. addElement ("abc1"); v. addElement ("abc2"); v. addElement ("abc3"); v. addElement ("abc4"); Enumeration en = v. elements (); while (en. hasMoreElements () {Object object = en. nextElement (); if (object. equals ("abc3") {v. addElement ("abc5 "); // No exception} System. out. println (object);} Iterator it = v. iterator (); while (it. hasNext () {Object obj = it. next (); if (obj. equals ("abc3") {v. addElement ("abc5"); // exceptions may occur. // in the face of concurrent modifications, the iterator will soon fail completely !} System. out. println (obj );}}}


<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + kernel/kernel + kernel/ywb + kernel/U9sG/ubnU7NK7uPa/1c/ywb + kernel/XKvMjdwb + 6 zcjdwb/U9sG/ubnu7nkupa /1 bXEz/expires + expires/ywb + expires/expires + expires/LBv9bQoaM8L3A + expires/LBv7XEtbHHsMjdwb + hozwvcD4KPHA + expires/ ywb + signature + oaM8L3A + signature + 7TLz/Signature + signature/tcTX6bz + signature/fS/bSmtcTX6bz + oaM8L3A + signature/LBv7XE1 + m8/rXEw7a + examples/tcTI3cG/examples/ss7K/da4tqi1xNfpvP7K/examples/PT67TLz/LBv7XEz + C1yNDUoaM8L3A + CjxwPjxwcmUgY2xhc3M9 "brush: java; "> package cn. list. demo; import java. util. enumeration; import java. util. iterator; import java. util. vector; public class VectorDemo {public static void main (String [] args) {Vector v = new Vector (); v. addElement ("abc1"); v. addElement ("abc2"); v. addElement ("abc3"); v. addElement ("abc4"); Vector vec = new Vector (); vec. add ("abc1"); vec. add ("abc2"); vec. add ("abc3"); vec. add ("abc4"); System. out. println (v. equals (vec ));}}15. E firstElement (); returns the item at the first component of the vector (index 0.

Package cn. list. demo; import java. util. enumeration; import java. util. iterator; import java. util. vector; public class VectorDemo {public static void main (String [] args) {Vector v = new Vector (); v. addElement ("abc1"); v. addElement ("abc2"); v. addElement ("abc3"); v. addElement ("abc4"); System. out. println (v. firstElement (); System. out. println (v. firstElement (); Vector vec = new Vector (); vec. add ("aa"); vec. add ("bb"); v. add (0, vec); // Insert the first location System. out. println (v. firstElement (); System. out. println (v. firstElement ());}}
16. E lastelement (); returns the last component of this vector. Code omitted.

17. E get (int index); returns the element at the specified position in the vector.

Package cn. list. demo; import java. util. enumeration; import java. util. iterator; import java. util. vector; public class VectorDemo {public static void main (String [] args) {Vector v = new Vector (); v. addElement ("abc1"); v. addElement ("abc2"); v. addElement ("abc3"); v. addElement ("abc4"); System. out. println (v. get (0); System. out. println (v. get (3); // here, the index value cannot be less than 0, or greater than 3 }}
18. int hashCode (); returns the hash code value of this vector.

package cn.list.demo;import java.util.Enumeration;import java.util.Iterator;import java.util.Vector;public class VectorDemo {public static void main(String[] args){Vector v=new Vector();v.addElement("abc1");v.addElement("abc2");v.addElement("abc3");v.addElement("abc4");System.out.println(v.hashCode());}}
19. int indexOf (Object o, int index); returns the index of the specified element that appears for the first time in this vector. It is a forward index from the index. If this element is not found, -1 is returned.

20. int indexOf (Object o); returns the index of the specified element that appears for the first time in this vector. If this vector does not contain this element,-1 is returned.

21. int lastIndexOf (Object o, int index); returns the index of the last occurrence of the specified element in this vector, and returns a reverse search from the index. If this element is not found, -1 is returned.

22. int lastIndexOf (Object o); returns the index of the last occurrence of the specified element in this vector. If this vector does not contain this element,-1 is returned.

package cn.list.demo;import java.util.Enumeration;import java.util.Iterator;import java.util.Vector;public class VectorDemo {public static void main(String[] args){Vector v=new Vector();v.addElement("abc1");v.addElement("abc2");v.addElement("abc3");v.addElement("abc4");v.addElement("abc1");v.addElement("abc2");v.addElement("abc3");v.addElement("abc4");System.out.println(v.indexOf("abc3"));System.out.println(v.indexOf("abc3",3));System.out.println(v.lastIndexOf("abc3"));System.out.println(v.lastIndexOf("abc3",3));}}
23. boolean isEmpty (); test whether the vector contains components.

24. void clear (); removes all elements from this vector.

Package cn. list. demo; import java. util. enumeration; import java. util. iterator; import java. util. vector; public class VectorDemo {public static void main (String [] args) {Vector v = new Vector (); v. addElement ("abc1"); v. addElement ("abc2"); v. addElement ("abc3"); v. addElement ("abc4"); v. addElement ("abc1"); v. addElement ("abc2"); v. addElement ("abc3"); v. addElement ("abc4"); System. out. println (v. isEmpty (); // non-empty output falsev. clear (); System. out. println (v. isEmpty (); // Null Output true }}
25. E remove (int index); remove the element at the specified position in the vector.

26. boolean remove (Object o); Removes the first matching item of the specified element in this vector. If the vector does not contain this element, the element remains unchanged.

27. boolean removeAll (Collection c); removes all elements contained in the specified Collection from this vector.

28. void removeAllElements (); remove all components from this vector and set its size to zero.

29. boolean removeElement (Object obj); Removes the first (minimum index) matching item of the variable from this vector.

30. void removeElementAt (int index); Delete the component at the specified index.

package cn.list.demo;import java.util.Enumeration;import java.util.Iterator;import java.util.Vector;public class VectorDemo {public static void main(String[] args){Vector v=new Vector();v.addElement("abc1");v.addElement("abc2");v.addElement("abc3");v.addElement("abc4");v.addElement("abc1");v.addElement("abc2");v.addElement("abc3");v.addElement("abc4");System.out.println(v.removeElement("abc1"));System.out.println(v);Vector vec=new Vector();vec.add("abc4");System.out.println(v.remove(2));System.out.println(v);System.out.println(v.remove("abc2"));System.out.println(v);System.out.println(v.removeAll(vec));System.out.println(v);v.removeAllElements();System.out.println(v);}}
31. protected void removeRange (int fromIndex, int toIndex); remove all elements between formIndex (inclusive) and toIndex (excluded) from this list.

32. boolean retainAll (Collection c); only elements contained in the specified Collection are retained in this vector.

package cn.list.demo;import java.util.Enumeration;import java.util.Iterator;import java.util.Vector;public class VectorDemo {public static void main(String[] args){Vector v=new Vector();v.addElement("abc1");v.addElement("abc2");v.addElement("abc3");v.addElement("abc4");v.addElement("abc1");v.addElement("abc2");v.addElement("abc3");v.addElement("abc4");Vector vec=new Vector();vec.add("abc2");vec.add("abc4");System.out.println(v.retainAll(vec));System.out.println(v);}}

In addition, there are some other methods, which will not be listed here. If you are interested, you can use the API introduction to do your own exercises.



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.