Advanced for loop create arrays and collections

Source: Internet
Author: User

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

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.