Java Learning Note 28

Source: Internet
Author: User
Tags null null

Vector is the implementation class of the list interface, supports all functions of the list interface, the vector class is an array-based implementation of the list class, in the internal encapsulation of a dynamic,


Allow redistribution of the object[] array, vector is thread-safe, without the need for a program to ensure the synchronization of the collection.


The following is a description of the methods used by the vector class:


public class Main {public static void main (string[] args) {vector vector = new vector (); Vector.add ("BILL"); Vector.add ("JAC K ");//output: [BILL, Jack]system.out.println (vector);//output: Vector Capacity: 10system.out.println (" vector Volume: "+ vector.capacity ( ); */* Sets the size of this vector. If the new size is greater than the current size, the corresponding number of NULL entries is added at the end of the vector. If the new size is less than the current size, all items at index * newSize and beyond are discarded. If the new size is negative, throw ArrayIndexOutOfBoundsException exception */vector.setsize (21);//output: Vector Capacity: 21system.out.println ("Vector Volume:" + vector.capacity ());/* Returns the capacity of this vector */system.out.println (vector.size ());//output: 21/* * Returns a partial view of this List, with elements ranging from fromIndex (including) 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 returned in the list are 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, Contains all the elements in this vector that are stored in the proper order. */object[] obj = (object[]) Vector.toarray ();//output: BILL JAC null NULL NULL NULL NULL NULL//NULL NULL NULL NULL NULL NULL NULL null//NULL NULL NULL nullfor (Object ob:oBJ) {System.out.println (OB);} Vector.setsize (5);//Set the vector capacity to 5string[] str = new string[6];/* * Returns an array containing all the elements in the proper order of this vector; the run-time type of the returned array is the type of the specified array. If the vector adapts to the specified array, the array is returned. * Otherwise, a new array is allocated using the run-time type of this array and the size of this vector. Note: When the size of the returned array is less than the capacity of the vector, the returned array is null; * When the size of the returned array is equal to the capacity of the vector, the vector elements are placed in the array, and if the size of the returned array is larger than the capacity of the vector, the vector elements are placed in the array, and the remaining positions */vector.toarray (str);//output: [BILL, JACK, NULL, NULL, NULL, NULL]SYSTEM.OUT.PRINTLN (arrays.tostring (str));/* * Returns a string representation of this vector that contains a string representation of each element. *///output: [BILL, JACK, NULL, NULL, NULL]SYSTEM.OUT.PRINTLN (vector.tostring ());/* fine-tune the capacity of this vector so that it is equal to the current size of the vector. */vector.trimtosize ();//output: 5system.out.println (Vector.capacity ());/* * This vector specifies the component at index to the specified object. Discards the previous component of the location. If the index is out of range (Index < 0 | | Index >= * SIZE ()), throw arrayindexoutofboundsexception exception */vector.setelementat ("Marry", 3);//Output Out: [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 range (Index < 0 | | index >= SIZE ()) * Throws ArrayIndexOutOfBoundsException exception */vector.set (3, "change");//output: [BILL, JACK, NULL, change, null]system.out.println (vector),/* * Remove all components from this vector and set their size to zero. */vector.removeallelements (); System.out.println (Vector.size ());//Output 0}}

Reprint Please specify source: http://blog.csdn.net/hai_qing_xu_kong/article/details/44119233 Emotional Control _ 



Java Learning Note 28

Related Article

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.