Lecture 1 and lecture 2: Dark Horse programmer _ Bi Xiangdong _ Java basic video tutorial 15th-06-set framework (generic Overview), (generic usage)
1. Understanding about generics:
- The origin of generics: generics are new features of JDK and later versions. It is a type of security mechanism used to solve security problems.
- Benefit: Transfers classcastexception during runtime to compilation. It is convenient for programmers to solve problems. Reduce and ensure security during running.
- Benefit: This avoids the trouble of forced conversion.
2. Generic Format: Use <> to define the referenced data type to operate. For example, arraylist <string> // defines the elements to be stored in the set as string type.
Iii. Generic usage time: Many generic frameworks are used. You only need to see <> generic frameworks.
4. Code exercises.
1 import Java. util. *; 2 public class genericdemo {3 Public static void main (string [] ARGs) {4 // use the generic 5 arraylist <string> List = new arraylist <string> (); 6 7 List. add ("Lisi"); 8 list. add ("wangwu"); 9 list. add ("zhaoliu"); 10 11 // list. add (4); // compilation fails through 12 13 for (iterator <string> iterator = List. iterator (); iterator. hasnext ();) {14 // generic mechanism, do not forcibly convert 15 string STR = iterator. next (); 16 system. out. println (STR + Str. length (); 17} 18} 19}
Lecture 3: Dark Horse programmer _ Bi Xiangdong _ Java basic video tutorial 15th-08-collection framework (generic class)
1. generic class definition: add the generic class name after the class definition name, for example, class tool <string>;
Ii. Use Time of generic classes: When the type to be operated (only the reference type can be used) in the class is uncertain. Use generic. ==== Note: objects are defined earlier to complete extension.
3. Code exercises:
1 class worker {2 3} 4 5 class tools <E> {6 private e element; 7 8 Public void sete (E) {9 this. element = E; 10} 11 12 public e Gete () {13 return this. element; 14} 15} 16 public class generictestdemo {17 public static void main (string [] ARGs) {18 19 tools <worker> tool = new tools <worker> (); 20 21 tool. sete (new worker (); 22 23 // tool. sete (new string (); // compilation error 24 worker = tool. gete (); 25 26 // string STR = tool. gete (); // compilation error 27} 28}
Lecture 4: Dark Horse programmer _ Bi Xiangdong _ Java basic video tutorial 15th-09-collection framework (generic method)
Dark Horse programmers-map, generic, set framework tools: collections, Arrays