Methods with Variable Argument Lists (var-args)
■ arguments The things you specify between the parentheses when you're
invoking a method:
doStuff("a", 2); // invoking doStuff, so a & 2 are arguments
■ parameters The things in the method's signature that indicate what the
method must receive when it's invoked:
■ Var-arg type When you declare a var-arg parameter, you must specify the
type of the argument(s) this parameter of your method can receive. (This can
be a primitive type or an object type.)
■ Basic syntax To declare a method using a var-arg parameter, you follow the
type with an ellipsis (...), a space, and then the name of the array that will
hold the parameters received.
■ Other parameters It's legal to have other parameters in a method that uses
a var-arg.
■ Var-args limits The var-arg must be the last parameter in the method's
signature, and you can have only one var-arg in a method.
Let's look at some legal and illegal var-arg declarations:
Legal:
void doStuff(int... x) { } // expects from 0 to many ints
// as parameters
void doStuff2(char c, int... x) { } // expects first a char,
// then 0 to many ints
void doStuff3(Animal... animal) { } // 0 to many Animals
Illegal:
void doStuff4(int x...) { } // bad syntax
void doStuff5(int... x, char... y) { } // too many var-args
void doStuff6(String... s, byte b) { } // var-arg must be last
invoking a method:
doStuff("a", 2); // invoking doStuff, so a & 2 are arguments
■ parameters The things in the method's signature that indicate what the
method must receive when it's invoked:
■ Var-arg type When you declare a var-arg parameter, you must specify the
type of the argument(s) this parameter of your method can receive. (This can
be a primitive type or an object type.)
■ Basic syntax To declare a method using a var-arg parameter, you follow the
type with an ellipsis (...), a space, and then the name of the array that will
hold the parameters received.
■ Other parameters It's legal to have other parameters in a method that uses
a var-arg.
■ Var-args limits The var-arg must be the last parameter in the method's
signature, and you can have only one var-arg in a method.
Let's look at some legal and illegal var-arg declarations:
Legal:
void doStuff(int... x) { } // expects from 0 to many ints
// as parameters
void doStuff2(char c, int... x) { } // expects first a char,
// then 0 to many ints
void doStuff3(Animal... animal) { } // 0 to many Animals
Illegal:
void doStuff4(int x...) { } // bad syntax
void doStuff5(int... x, char... y) { } // too many var-args
void doStuff6(String... s, byte b) { } // var-arg must be last
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/7899089/viewspace-623463/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- methods
- Sitecore ListManager Contact Lists vs Segment Lists
- variable: Type 與 Type variable
- Python基礎——positional argument vs keyword argumentPython
- global_variable VS local_variable &functionalFunction
- There are 5 methods of index lookupIndex
- Oracle environment variableOracle
- CSS Variable StudyCSS
- MySQL 報錯'Variable 'XXX' is a read only variable'MySql
- Stochastic Methods in Finance (1)ASTNaN
- Vue踩坑之旅——methodsVue
- bootstrap-select——Methodsboot
- javaScript argument 學習筆記JavaScript筆記
- 9,6 argument應用
- OSError: [Errno 22] Invalid argumentError
- Laravel 5.4 使用 pluck () 代替 lists ()Laravel
- [譯] part 17: golang 方法methodsGolang
- 【Java 8實戰】Extension MethodsJava
- Python 的 Magic Methods 指南Python
- 程式碼會引發 Notice: Undefined variable: undefined_variable 錯誤Undefined
- 關於argument變數的理解變數
- compact (): Undefined variable: operatorUndefined
- Creating External Lists From Code
- Guava Lists transform 使用及問題GuavaORM
- golang programming language study methods websocketGolangWeb
- Python socket.help Methods薦Python
- Leetcode Merge k Sorted ListsLeetCode
- Leetcode Merge Two Sorted ListsLeetCode
- sql mysql variable autocommit (5)MySqlMIT
- SELECT INTO FROM mysql Undeclared variableMySql
- Bex Query variable Type 說明
- MySQL 中出現報錯提示: ‘Variable ‘XXX‘ is a read only variable‘的解決方法MySql
- Nifty File Lists for mac檔案列表建立工具Mac
- Leetcode 21 Merge Two Sorted ListsLeetCode
- Leetcode 23 Merge k Sorted ListsLeetCode
- [leetCode][003] Intersection of Two Linked ListsLeetCode
- Leetcode-Merge Two Sorted ListsLeetCode
- Leetcode-Intersection of Two Linked ListsLeetCode