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