Java Foundation--Set (iii)--generics, enhanced for, tool class

Source: Internet
Author: User
Tags set set

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

Related Article

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.