Dark Horse programmers-map, generic, set framework tools: collections, Arrays

Source: Internet
Author: User

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:

    1. 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.
    2. Benefit: Transfers classcastexception during runtime to compilation. It is convenient for programmers to solve problems. Reduce and ensure security during running.
    3. 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

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.