java-Common data types

Source: Internet
Author: User

1 Vector class

A vector is similar to an array, but has the following two advantages over the use of the array.

(1) You do not need to declare the upper limit, as the element increases, the length of the vector will automatically increase.

(2) vector provides an additional way to increase and remove elements, which is more efficient than array operations.

The vector class has three constructors, as follows:

public Vector (); This method creates an empty vector.

Public Vector (int initialcapacity); This method creates a vector with an initial length of initialcapacity.

Public Vector (int initialcapacity, int capacityincrement); This method creates a vector with an initial length of initialcapacity and increments the capacityincrement element when the vector needs to grow.

The 1.1 vector class adds and removes objects in the following ways:

public void Add (int index, object element) adds an object element at the index position.

The public boolean Add (object o) adds an object o at the end of the vector.

public object Remove (int index) deletes the object at the index position, followed by the next object.

The methods for accessing and modifying objects in the 1.2 vector class are as follows:

public object get (int index) returns the index position object.

The public object Set (int index, object element) modifies the object of the index position to element.

1.3 Other methods:

The public string toString () converts the element to a string.

public int size () returns the length of the object.

2 Hashtable class

The Hashtable class stores the name-value pairs of an object. Associates the name of an object with its value, stores it, and extracts its value according to the object name.

In Hashtable, a key name can only correspond to a key value, but a key value may correspond to multiple key names, and the key name must be unique. Constructors and common methods are as follows:

Public Hashtable () constructs a hash list.

Public Hashtable (int initialcapacity) builds a hash table with a length of initialcapacity.

public int size () returns the number of names of the hash table.

public object Remove (object key) removes the key name and its corresponding value value in the hash list.

public object put (object Key,object value) stores the name key and the object value values in the hash table.

Cases:

1  Packagetest;2 3 ImportJava.util.IdentityHashMap;4 ImportJava.util.Map;5 ImportJava.util.Map.Entry;6 7  Public classusehashtable {8      Public Static voidMain (string[] args) {9          TenString str1 =NewString ("XX");  OneString str2 =NewString ("XX");  A         //System.out.println (str1 = = str2); -            -map<string,string> map =NewIdentityhashmap<string,string>();  theMap.put (str1, "Hello");  -Map.put (STR2, "World");  -        -           for(entry<string,string>Entry:map.entrySet ()) +{/*Map.entryset () takes each key-value pair out of a map and encapsulates it into an entity object that is stored in a set.  - entry<string,string> means a generic type, which is Allrecordmap key and value respectively. */ +      ASystem.out.println (Entry.getkey () + "" +Entry.getvalue ());  at          }  -         /*System.out.println ("ContainsKey--->" + map.containskey ("xx")); - System.out.println ("str1 containskey--->" + map.containskey (str1)); - System.out.println ("str2 containskey--->" + map.containskey (str2)); - System.out.println ("value---->" + map.get ("xx")); - System.out.println ("str1 value---->" + map.get (str1)); in System.out.println ("str2 value---->" + map.get (str2));*/ -  to     }  +}
View Code

public object get (object key) returns the value corresponding to the hash table key name.

The public string toString () is converted to a string.

java-Common data types

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.