Java mutable parameter list

Source: Internet
Author: User

1. A simple implementation of a mutable parameter list

When a method is called, the number of arguments or the type of the method is unknown, which is called a mutable argument list. In previous Java code, you could use an object array to implement such a function. Because all classes are directly or indirectly inherited from the object class.  Varargs.java
Package sample; class a1{} Public class VarArgs {static void PrintArray (object[] args) {For (Object Obj:args)System.out.print (obj+ "");System.out.println ();       } Public static void Main (string[] args) {PrintArray (New object[]{New Integer, New Float (3.14), New Double (11.11)              });PrintArray (New object[]{"One", "one", "three"});PrintArray (New Object[]{new A1 (), New A1 (), New A1 ()});       }}
 Results:3.14 11.11one three[email protected] [email protected] [email protected] here the PrintArray method uses the object array as an argument and uses the foreach syntax to iterate through the array and print each object.  2. Java SE5 Implementing a mutable parameter listIn the same way, parameters can be defined as such, (Object...args), so that the same effect is achieved.  Newvarargs.java
Package sample; class a{} Public class Newvarargs {static void PrintArray (Object...args) {For (Object Obj:args)System.out.print (obj+ "");System.out.println ();       } Public static void Main (string[] args) {PrintArray (New Integer, New Float (3.14), New Double (11.11));PrintArray (,PrintArray ("One", " One", "one", "three");PrintArray (New A (), new A (), new A ());PrintArray ((object[]) new integer[]{1,2,3,4});PrintArray ();       }}
 Results:3.14 11.1147 3.14 11.11one three[email protected] [email protected] [email protected]1 2 3 4 There is no explicit use of an array as an argument, but the compiler will actually populate the array for you. So you can also use the foreach syntax to traverse it. note the second-to-last line, the compiler has found it to be an array, so no transformations are performed on it. The last line indicates that 0 parameters can be passed. You can also specify the type of all mutable parameters, and the following program specifies that all mutable parameters must be string.  Optionalarguments.java
Package sample; Public class Optionalarguments {static void F (string...trailing) {For (String s:trailing)System.out.print (s+ "");System.out.println ();       } Public static void Main (string[] args) {F ("one");F ("A", "three");f ();       }}
 Results:Onetwo Three You can use both the original type and the wrapper class in a mutable argument list.  Autoboxingvarargs.java
Package sample; Public class Autoboxingvarargs {Public static void F (Integer...args) {For (Integer I:args)System.out.print (i+ "");System.out.println ();       } Public static void Main (string[] args) {f (New Integer (1), new Integer (2));f (3,4,5,6,7,8,9);F (10,new Integer (one), n);       }}
 Results:1 23 4 5 6 7 8 910 11 12 3. Overloads of the mutable parameter list (overloading)If an overload occurs, the compiler automatically calls the most appropriate method to match it.  Overloadingvarargs.java
Package sample; Public class Overloadingvarargs {static void F (Character...args) {System.out.print ("first");For (Character C:args)System.out.print (c+ "");System.out.println ();       } static void F (Integer...args) {System.out.print ("second");For (Integer I:args)System.out.print (i+ "");System.out.println ();       }       static void F (Long...args) {System.out.print ("third");For (Long L:args)System.out.print (L + "");System.out.println ();       }       static void F (Double...args) {System.out.print ("forth");For (Double D:args)System.out.print (d+ "");System.out.println ();       }       Public static void Main (string[] args) {F (' A ', ' B ', ' C ');f (1);f (2,1);f (0.1);F (       }}
 Results:First a B csecond 1second 2 1forth 0.1third 0 However, the following scenario is not allowed, that is, adding a non-mutable parameter to a method.  Overloadingvarargs2.java
Package sample; Public class OverloadingVarargs2 {static void F (float I,character...args) {System.out.println ("first");       } static void F (Character...args) {System.out.println ("second");       }       Public static void Main (string[] args) {f (1, ' a ');F (' A ', ' B ');//error       }}
 but you can solve the problem this way.
Package sample; Public class OverloadingVarargs2 {static void F (float I,character...args) {System.out.println ("first");       } static void F (char C,character...args) {System.out.println ("second");       }       Public static void Main (string[] args) {f (1, ' a ');F (' A ', ' B ');       }}
that is, overloaded methods must maintain a consistent parameter form.

Java mutable parameter list

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.