Convert to Java data structure (1)

Source: Internet
Author: User
Tags add numbers

 

Java is a very well-developed programming language. It has a very safe syntax definition and a fully object-oriented programming idea. What's more commendable is that it also has a very powerful API. Today, let's take a look at the pre-defined data structure in Java.
Some people have a horrible association when talking about data structures. Indeed, it is a headache to perform some operations on complex data structures, and I am also a little daunting about it. However, the operations on the data structure in Java are very convenient. In Java, common data structures are defined, and various operations are defined as methods that can be called directly.
All Java pre-defined data structures are placed in the util package. In the code, enter import java. util. *; so that the data structure can be used.
Next we will introduce the Vector class. In some books of vector, it is translated into "vector", which we will always call it later. A vector is actually a dynamic array that can dynamically add or delete members. See the following example:
Import java. util .*;
Public class usevector extends vector {

Public usevector (){
Addelement ("Tom ");
Addelement ("Jim ");
Addelement ("Jack ");
}
Public object get (INT index ){
Return super. Get (INDEX );
}
Public static void main (string [] ARGs ){
System. Out. Print (New usevector (). Get (1 ));
}
}
The output in this example is:
Jim
Here, we should note that adding members to the vector is stored in objects. Therefore, we must forcibly convert the elements to the original type when getting them. We should write the following code:
(String) New usevector (). Get (1 );
However, we only want to output the string above, so it is not clearly written. In fact, it is implicitly converted into a string.
Let's look at another example:
Import java. util .*;
Public class usevector extends vector {

Public usevector (){
Addelement (New INTEGER (2 ));
Addelement (New INTEGER (4 ));
Addelement (New INTEGER (1 ));
}
Public object get (INT index ){
Return super. Get (INDEX );
}
Public static void main (string [] ARGs ){
Vector v = new usevector ();
Collections. Sort (v );
For (INT I = 0; I <3; I ++)
System. Out. Print (V. Get (I ));
}
}
We can see that when adding an element, we do not directly add numbers, but use the packaging class integer to add them. This is because all elements are stored as objects, so we cannot use the basic data type. In the following example, we use collections. Sort (V); this is a static method. Its function is to sort the V vector. As you can see, the final result is 124, which indicates that the sorting is successful.
Next let's take a look at hashtable. We are very familiar with the "hash table". Let's use an example to briefly describe its usage:
Import java. util .*;
Public class hash {
Public static void main (string ARGs []) {
Hashtable H = new hashtable ();
H. Put ("first", new INTEGER (1 ));
H. Put ("second", new INTEGER (2 ));
H. Put ("third", new INTEGER (3 ));
Enumeration EK = H. Keys ();
While (EK. hasmoreelements ())
System. Out. println (EK. nextelement ());
}
}
When a hash table is stored, each element has a key and a value. We can read the value based on the key, and the key and value are also a set. In the preceding example, we traverse the key set. In this example, we use enumeration, which is a set of enumeration implemented by the keys method of the hashtable interface, enumeration has two methods: hasmoreelements and nextelement. The former is used to determine whether there are more elements, and the latter returns the next element.
Summary: This article introduces the usage of vector and hashtable, and briefly describes sorting and traversing. In the next article, I will continue to introduce the data structure in Java.

 

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.