2013年11月19日星期二

JAVA SE5 Why put forward the concept of deformable participate ?

RT, use int fun (String []) and use int fun (String ...) are representative of the number of String parameter uncertainty .

then raised (Sting ...) What is the root cause ? It can solve the problem using (String []) can not solve it ? ? Perhaps I did not learn how long , did not understand the benefits , so please have experienced large cattle guidance. Thank you ~
------ Solution -------------------------------------- ------

public static void testArray() {

/**
 * 用countString(String[] arr)能做到的, countString1(String... arr) 都能做到。
 * 可是反过来是不成立的。
 * 比如:我只需要传入一个Sting,而不是一个Stirng[]的数组,那么countString(String[] arr)就做不到。
 */
String[] arr0 = {"hello","hello"};
System.out.println(countString(arr0));

System.out.println(countString1(arr0));
System.out.println(countString1("hello"));
System.out.println(countString1("hello","world","home"));
}

/**
 * 可以接受的参数:String[]数组,单个String,多个String的列表
 * @param arr
 * @return
 */
private static int countString1(String... arr){
return arr.length;
}
/**
 * 只能接受String[]数组作为参数
 * @param arr
 * @return
 */
private static int countString(String[] arr){
return arr.length;
}

------ Solution ---------------------------- ----------------
array function is variable parameters function subset , who used to do the array parameter can be used where a variable parameter substitution . For example the main method :
public static main (String [] args) {
/ / code to be executed
}
can be written as
public static main (String. .. args) {
/ / code to be executed
}

variable parameters of the function is more powerful than the array , in addition to receiving an array type argument , but also be able to receive an unlimited number of individual arguments.
written "String ... args" like this, in fact, is to remind the compiler will received several organized into a single array argument passed args,
args ultimately received or array. Of course , if you pass an array directly args are not wrong , so instead Province compiler to finishing .

summarize: variable parameter is an array parameter functional extensions.
------ For reference only -------------------------------------- -
with an array really is no different , just looking a little bit code . I also think that there is the necessity of not great.
------ For reference only -------------------------------------- -
may be more convenient
------ For reference only ------------------------------ ---------


really fraught answer ah ~
------ For reference only ---------------------------------------
add: variable parameters can only be the last parameter list .

For example:
public void method (int a, String ... args) {/ / correct

}

public void method (String. .. args, int a) {/ / error
}
------ For reference only --------------------------------- ------
Why not syntactic sugar
This provincial c + + kids ask to ask a
------ For reference only ---------------------- -----------------


thanks ~ original single String can not use [ ] Yeah .
------ For reference only -------------------------------------- -


Thank you very much , and now understand. Is an array parameter expansion ~
------ For reference only ------------------------------- --------

  
really fraught answer ah ~     

Wurenzidi will not , and I think this problem each have their own views on it. I have always felt the java variable length parameters and generics are tasteless, I could replace other ways , what is the value of existence . So I generally do not have a variable-length parameters and generics. Replaced with an array of variable-length parameters , generics I usually directly accept Object, save more .

没有评论:

发表评论