The use of variable parameter function in Java foundation---->java

Source: Internet
Author: User

Java's parameter function implementation is actually an array, and today we will simply learn how to use it.

In Java, the variable parameter function one, it uses the following method:
 Public classVariableparam {Private Static voidvariableParam1 (Object ... args) { for(Object V:args) {System.out.print (v.tostring ()+ " "); } System.out.println (Args.length); //you can see that the args and arrays have the length property} @Test Public voidparam_1 () {//if it's list<string>,List<string> Lists1 =NewArraylist<>(); Lists1.add ("Huhx"); Lists1.add ("Linux"); VARIABLEPARAM1 (LISTS1); //[Huhx, Linux] 1//if it's list<object>,List<object> lists2 =NewArraylist<>(); Lists2.add ("Huhx"); Lists2.add ("Linux"); VARIABLEPARAM1 (LISTS2); //[Huhx, Linux] 1//if it is an arraystring[] Arrays1 =Newstring[]{"Huhx", "Linux"};//HUHX Linux 2variableParam1 (ARRAYS1); //if it is an arrayobject[] Arrays2 =Newobject[]{"Huhx", "Linux"}; VARIABLEPARAM1 (ARRAYS1); //HUHX Linux 2//if the parameterVARIABLEPARAM1 ("Huhx", "Linux");//HUHX Linux 2    }}

Second simple use case
 PublicString Basereplacefromarray (String string, list<string>list) {    returnMessageformat.format (String, translistinfoarrays (list));}PrivateString[] Translistinfoarrays (list<string>list) {    intLength =list.size (); String[] Arrays=NewString[length];  for(inti = 0; i < length; i++) {Arrays[i]=List.get (i); }    returnarrays;} @Test Public voidbasereplacefromarray_test () {List<String> list =NewArraylist<>(); List.add ("Huhx"); List.add (L "); System.out.println (Basereplacefromarray ("{0} love {1}", list));//huhx Love L. }

Friendship Link

The use of variable parameter function in Java foundation---->java

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.