Java basics-set (3)-generic, enhanced for, tool class, java tool class

Source: Internet
Author: User

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.


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.