Java: enhanced for Loop)

Source: Internet
Author: User

Enhanced for Loop)
The so-called "enhanced for loop" mainly targets containers. When this feature is used, developers can
The logic of traversing the container is handed over to the compiler for processing. For example, the following code:
Void cancelall (collection C ){
For (iterator I = C. iterator (); I. hasnext ();){
Timertask TT = (timertask) I. Next ();
TT. Cancel ();
}
}
You can use an enhanced for loop to rewrite it:
Void cancelall (collection C ){
For (Object O: C)
(Timertask) O). Close ();
}
After the compiler determines that object C is a collection sub-object (that is, a container), it will allow the use of an enhanced for Loop
Form, and automatically get the C iterator to automatically traverse each element in C.
As you can see, the above Code still has a forced type conversion (timertask) O). Close ();). In fact
Item features should be generally combined with generic features to maximize the benefits. Combined with generics, the above Code becomes:
Void cancelall (collection C ){
For (timertask task: C)
Task. Cancel ();

}

Public Enum loglevel {
Verbose (2, "verbose", 'V'), // $ NON-NLS-1 $
Debug (3, "debug", 'd), // $ NON-NLS-1 $
Info (4, "info", 'I'), // $ NON-NLS-1 $
Warn (5, "Warn", 'w'), // $ NON-NLS-1 $
Error (6, "error", 'E'), // $ NON-NLS-1 $
Assert (7, "assert", 'A'); // $ NON-NLS-1 $

Private int mprioritylevel;
Private string mstringvalue;
Private char mpriorityletter;

Loglevel (INT intpriority, string stringvalue, char prioritychar ){
Mprioritylevel = intpriority;
Mstringvalue = stringvalue;
Mpriorityletter = prioritychar;
}

/**
* Circular buffer containing the logcat output. This is unfiltered.
* The valid content goes from <code> mbufferstart </code>
* <Code> mbufferend-1 </code>. Therefore its number of item is
* <Code> mbufferend-mbufferstart </code>.
*/
Private logmessage mkernermsg = new logmessage ();

/*
* This below inherited from log. Java
*/
/**
* Returns the {@ link loglevel} Enum matching the specified letter.
* @ Param letter the letter matching a <code> loglevel </code> Enum
* @ Return a <code> loglevel </code> object or <code> null </code> if no match were found.
*/
Public static loglevel getbyletter (char letter ){
For (loglevel mode: values ()){
If (mode. mpriorityletter = letter ){
Return mode;
}
}

Return NULL;
}

How to understand this values?

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.