Foreach and for loop instances in jsp

Source: Internet
Author: User
Tags rand

Knowledge Point:

1. Use foreach.

2. Generate a Random number of decimals. Random. nextFloat ().

3. Convert the String to a character array, String. toAharArray ().

 

Java SE5 introduces a more concise for syntax for arrays and containers, that is, the foreach syntax, indicating that you do not need to create int variables to count the sequences composed of access items, foreach automatically generates each item.

The usage is as follows:

The code is as follows: Copy code

Import java. util. Random;

Public class ForEach {
Public static void main (String [] args ){
Random rand = new Random (47 );
Float f [] = new float [10];
For (int I = 0; I <10; I ++ ){
F [I] = rand. nextFloat ();
        }
// Simple for method usage "foreach"
For (float x: f ){
System. out. println (x );
} // Convert the string into a character array and then use "foreach"
For (char c: "I'm a good man! ". ToCharArray ()){
System. out. print (c + "");
        }
    }
}

Output:

0.72711575
0.39982635
0.5309454
0.0534122
0.16020656
0.57799757
0.18847865
0.4170137
0.51660204
0.73734957
I'm a g o d m a n!

Any method that returns an array can use foreach, and foreach can also be used for any Iterable object

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.