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