Java study notes 28, java Study Notes

Source: Internet
Author: User

Java study notes 28, java Study Notes

Vector is the implementation class of the List interface and supports all functions of the List interface. The Vector class is a List class implemented based on arrays and encapsulates a dynamic,


The Object [] array that allows redistribution. The Vector is thread-safe and requires no program to ensure the synchronization of the set.


Some methods of the Vector class are described as follows:


Public class Main {public static void main (String [] args) {Vector vector = new Vector (); vector. add ("BILL"); vector. add ("JACK"); // output: [BILL, JACK] System. out. println (vector); // output: vector capacity: 10System. out. println ("vector capacity:" + vector. capacity ();/** set the vector size. If the new size is greater than the current size, the corresponding number of null items will be added at the end of the vector. If the new size is smaller than the current size, all items at and after the * newSize index are discarded. If the new size is negative, an ArrayIndexOutOfBoundsException exception is thrown */vector. setSize (21); // output: vector capacity: 21System. out. println ("vector capacity:" + vector. capacity ();/** returns the capacity of this vector */System. out. println (vector. size (); // output: 21/** return some views of this List. The element range is from fromIndex (included) to toIndex (not included ). (If fromIndex and * toIndex are equal, the returned List will be empty ). The returned List is supported by this List, so the changes in the returned List will be reflected in this * List, and vice versa. The returned List supports all optional list operations supported by this list. */List list = (List) vector. subList (0, 1); // output: [BILL] System. out. println (list. toString ();/** returns an array containing all elements in the proper order of the vector. */Object [] obj = (Object []) vector. toArray (); // output: bill jac null // null nullfor (Object ob: obj) {System. out. println (ob);} vector. setSize (5); // set the vector capacity to 5 String [] str = new String [6];/** return an array, contains all elements stored in the proper order in this vector; the runtime type of the returned array is the type of the specified array. If the vector can adapt to the specified array, the array is returned. * Otherwise, use the runtime type of the array and the size of the vector to allocate a new array. Note: When the returned array size is smaller than the vector capacity, the returned array is null. * When the returned array size is equal to the vector capacity, the vector element is placed in the array; if the returned array is larger than the vector capacity, put the vector element into the array, * the remaining position is null. */Vector. toArray (str); // output: [BILL, JACK, null] System. out. println (Arrays. toString (str);/** returns the String representation of this vector, which contains the String representation of each element. * // Output: [BILL, JACK, null] System. out. println (vector. toString ();/** tune the vector capacity so that it is equal to the current vector size. */Vector. trimToSize (); // output: 5System. out. println (vector. capacity ();/** set the component at the index specified by this vector to the specified object. Discard components before this location. If the index is out of the range (index <0 | index> = * size (), an ArrayIndexOutOfBoundsException exception */vector is thrown. setElementAt ("Marry", 3); // output: [BILL, JACK, null, Marry, null] System. out. println (vector);/** Replace the element at the specified position in this vector with the specified element. If the index is out of the range (index <0 | index> = size () *, an ArrayIndexOutOfBoundsException exception is thrown */vector. set (3, "Change"); // output: [BILL, JACK, null, Change, null] System. out. println (vector);/** remove all components from this vector and set its size to zero. */Vector. removeAllElements (); System. out. println (vector. size (); // output 0 }}

Reprinted please indicate the source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44119233 sentiment control _



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.