Next, "Java Foundation--Set (ii)--iterator, Map collection"
Six. Generic type(1) Definition:
generics are special types that put explicit types of work in the form of creating objects or calling methods.
(2) Format:
< data types >
Arraylist<string> alist=new arraylist<string> ();
<String> represents a generic type
(3)Benefits: (Master)
A: solved the yellow warning line problem
B: To advance the conversion exception during the run to the compile time
C: optimized programming, no forced type conversions required
(4) Development of generics
A: Generic class
B: Generic method
C: Generic interface
(5)use of generics:
Look at the class or interface in the API , followed by <> Application.
generally used in collections.
Seven. Enhanced for loop (new features that appear after JDK5)(1) Format:
For (an element type variable of an array or collection collection: An array or an object of the collection collection)
{
You can use variables directly.
}
Example: one of the Map collection traversal methods uses enhanced for in key-finding values
public static void Main (string[] args) {map<string,integer> Map = new hashmap<string,integer> (); Map.put ("Two Yang Map.put ("Two Zheng"), Map.put ("Two Lights", 25); Set <String> keys=map.keyset ();//Put the key together and deposit it into the set set. for (String Key:keys) {//Traversal key collection, gets each key. <u><span style= "color: #ff0000;" > Enhanced for</span></u> Integer value=map.get (key);//Allow key to find value get (Object key) System.out.println (key+ "* * *" + value);}}
(2) Benefits:
facilitates traversal of arrays and collection collections.
(3)Note:
A: Enhanced for is used to replace iterators.
B: do not use the enhanced for when iterating through a collection, modify the collection itself with a collection.
(4) Traversal of the set three ways
Iterators
General For+get
Enhanced for (at work)
Eight Tool class
Common to the tool class there are two kinds, collections and arrays, not much to say, direct.
Summary
The contents of the collection, although many, but not messy. As long as the comb clearly, will be clear in the chest.
Java Foundation--Set (iii)--generics, enhanced for, tool class