Advanced for Loop:
Format: for (data type variable name: Traversed collection (Collection) or array)
Iterates over a collection. Only elements can be fetched, and the collection cannot be manipulated at that time.
variable parameter : The abbreviated form of an array parameter, without having to manually create an array object each time. As long as the element you want to manipulate is passed as a parameter. These parameters are implicitly encapsulated as arrays. When used, variable parameters must be defined behind the parameter list.
Public classJihe { Public Static voidMain (string[] args) {Show ("haha","Bai","Wang","Priory","Gong"); } Public Static voidShow (String ... arr) {System. out. println ("Array of arr's degrees:" +arr.length); for(intI=0; i<arr.length;i++) {System. out. println ("Array subscript" +i+"value: "+Arr[i]); } } }
The result is:
The degree of arr in the array: 5
Array subscript is 0 value: haha
Array subscript is 1 value: Bai
Array subscript is 2 value: King
Array subscript is 3 value: Priory
Array subscript is 4 value: Gong
Public classJihe { Public Static voidMain (string[] args) {Show ("haha",1,2,3,4); } Public Static voidShow (String s,int... arr) {System. out. println (Arr.length); for(intI=0; i<arr.length;i++) {System. out. println ("the array is labeled"+i+"values:"+Arr[i]); } } }
The result is:
4
Array subscript is 0 value: 1
Array subscript is 1 value: 2
Array subscript is 2 value: 3
Array subscript is 3 value: 4
Advanced for loop create arrays and collections