6. java. util
Q6.1 can I dynamically allocate arrays in Java?
A: Yes. For example, int n = 3; language [] myages = new language [N];
Q6.2 how do I know the length of an array?
A: Use the Length attribute. In the preceding example, myages. length is 3.
Q6.3 I also want to automatically change the length of the array to add/delete elements.
A: Use the sequential table -- Java. util. List interface.
You can choose to use arraylist or arraylist. The former is array implementation, and the latter is linked list implementation.
For example, list = new arraylist (); or list = new arraylist ();.
Q6.4 what is a linked list? Why are two lists available: arraylist and rule list?
A: Please study the data structure.
Q6.5 I want to use the queue/stack.
A: use Java. util. Collections list.
Q6.6 I hope there are no repeated elements.
A: Use the java. util. Set interface. For example, set = new hashset ().
Q6.7 I want to traverse the set/map.
A: use Java. util. iterator. See API.
Q6.8 I also want to be able to sort.
A: use Java. util. treeset. For example, set = new treeset (). The elements are automatically sorted.
.
You need to implement the comparable interface for the element. You may also need to provide the equals () method, compareto () method, and hashcode () method.
Q6.9 but I want to sort the array.
A: The java. util. arrays class contains practical methods such as sort.
Q6.10 I want to sort by different methods.
A: define a sort class that implements the interface comparator for each method and use it in combination with arrays or treeset.
Q6.11 what is the purpose of map?
A: You can use keywords to quickly access key-value pairs.
Q6.12 the set method is correct, but the get method returns the object.
A: force type conversion to the type you need. See q5.12.
Q6.13 what is the difference between arraylist and vector? What is the difference between hashmap and hashtable?
A: arraylist and hashmap are insecure due to multithreading. Access to the same arraylist object in multiple threads may cause conflicts and errors. While vector and hashtable are multi-threaded and secure, even if you access the same vector object in multiple threads at the same time, they will not cause errors. It seems that we should use vector and hashtable, but in fact the performance of vector and hashtable is too poor, so if you are not using multithreading, we should still use arraylist and hashmap.
Q6.14 I want to obtain a random number.
A: Use the java. util. Random class.
Q6.15 I compared two strings which are always false, but they are all "ABC "!
A: To compare strings, you must use the equals or equalsignorecase method. Do not use =! = Compares whether two references (variables) point to the same object rather than the content.
Q6.16 I want to modify a string but the Edit Method is not found in the string class.
A: Use the stringbuffer class.
String STR = "..."; // the string to be processed stringbuffer buffer = new stringbuffer (STR); // use this string to initialize a stringbuffer
Buffer. append ("..."); // call the stringbuffer API to edit the string.
String str2 = buffer. tostring (); // obtain the edited string.
In addition, if you need to connect multiple strings, try to avoid using the + sign for direct connection, but use the stringbuffer. append () method.
Q6.17 I want to process the date/time.
A: Use the java. util. Date class. You can use the java. Text. simpledateformat class
Da
Inter-te conversion.
Simpledateformat formatter = new simpledateformat ("yyyy-mm-dd hh: mm: SS"); // specifies the Date Format
Date = formatter. parse ("18:30:35"); // convert the string that matches the format to datestring S = formatter. format (date); // convert date to a string that complies with the format
From: http://www.cndw.com/tech/page/200602154466.asp