A comparison between the indefinite length parameters of Java and the indefinite length parameters of Python

Source: Internet
Author: User
Tags mul

One: Cause

(0) Indeterminate length parameter, also called variable parameter, is not sure how many parameters need to be passed before calling function again

(1) Variable length parameters in Java with string ... args/int ... Args, such as three points to represent; Python uses a *args tuple to represent the/**args dictionary (map) to represent

(2) Indefinite parameters have two provisions: first, the parameter list of the method has only one parameter of indefinite length; second, the position of an indefinite length array must be the last parameter. Otherwise it cannot be compiled.

Two: Example comparison

(1) Variable length parameters of Python

The first method-a parameter that starts with a *, represents an arbitrarily long tuple:

>>> def mul (*arg): ...     Print arg ... >>> mul (1,2,3,4,5,6,7, ' hello ', ' Panfei ') (1, 2, 3, 4, 5, 6, 7, ' Hello ', ' Panfei ') # a tuple >>> *********************************

The second method, a parameter that starts with a * *, represents a dictionary:

>>> def mul2 (**arg): ...     Print arg ... >>> Mul2 (a=11,b=444,c=888) {' A ': one, ' C ': 888, ' B ': 444} A dictionary >>>*********************************
Note: The two parameters of the former can be directly realistic parameters, the latter is written as the name = value of the form!

(2) Variable length parameters in Java

Package Cs.tju.test;public class Test {public static void main (string[] args) {Changeargs ("AAA"); Changeargs ("AAA", "BBB" ); Changeargs ("AAA", "BBB", "CCC");    /* @function  variable length parameter usage test     * @data 2015.05.19     *string ... and string[] should have the same meaning, but not as String ... Flexible, he will generate a dynamic array similar to a vector or list, able to use for access     * Indefinite parameters have two rules: first, the parameter list of the method has only one parameter of indefinite length;     * Second, the position of an array of indefinite length must be the last parameter. Otherwise it cannot be compiled.      */public static void Changeargs (String arg, string ... zyp) {System.out.println (ARG); System.out.println ("----------------"); for (String S:zyp) {System.out.println (s);} System.out.println ("*************-");}}


A comparison between the indefinite length parameters of Java and the indefinite length parameters of Python

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.