Java basics-set (3)-generic, enhanced for, tool class, java tool class
Next, Java basics-set (2)-iterator and Map set
6. Generic(1) Definition:
Generic is a special type that puts explicit types of work in the process of creating objects or calling methods.
(2) format:
<Data type>
ArrayList<String> aList=new ArrayList<String>();
<String> indicates the generic type.
(3) Benefits: (master)
A: solved the yellow warning line problem.
B: Advance the conversion exception during the runtime to the compilation period.
C: optimized the program design, without the need for forced type conversion
(4) Development of generics
A: Generic
B: Generic Method
C: generic interface
(5) Generic usage:
Check whether there are any classes or interfaces in the API. If yes, they are generic applications.
It is generally used in a set.
7. Enhance the for Loop (new features after JDK 5)(1) format:
For (element type variable of an array or Collection set: object of an array or Collection set)
{
Use variables directly.
}
For example, in one of the map set traversal modes, the enhanced
Public static void main (String [] args) {Map <String, Integer> map = new HashMap <String, Integer> (); map. put ("Two Yang", 23); map. put ("two rows", 24); map. put ("diguang", 25); Set <String> keys = map. keySet (); // store the key to the set. for (String key: keys) {// retrieve each key by traversing the key set. <U> <span style = "color: # ff0000;"> enhanced for </span> </u> Integer value = map. get (key); // Let the key find the value get (Object key) System. out. println (key + "***" + value );}}
(2) benefits:
This facilitates the traversal of arrays and Collection sets.
(3) Note:
A: Enhancement for is used to replace the iterator.
B: Do not use the for enhancement function to traverse the set and modify the set itself.
(4) traverse the set in three ways
Iterator
Common for + get
Enhanced for (used at work)
8. Tools
There are two commonly used tool classes: Collections and Arrays.
Summary
There are many contents in the set, but they are not messy. As long as the Organization is clear, it must be clear to the chest.