当前位置:网站首页>It can receive multiple data type parameters - variable parameters
It can receive multiple data type parameters - variable parameters
2022-04-23 03:13:00 【The name of the father】
One 、 Problem introduction
If you need to define a method to sum , This method can flexibly fulfill the following requirements :
- Calculation 1 The sum of two data .
- Calculation 2 The sum of two data .
- Calculation 3 The sum of two data .
- Calculation n The sum of two data ,
- It can even support calling without receiving parameters .
Two 、 effect
- The transmission parameters are very flexible , convenient 、 No parameters can be passed ,
- You can transfer a parameter Multiple parameters can be transferred
- You can transfer an array
- Variable parameters are essentially an array inside a method
Note on variable parameters :
1. There can only be one variable parameter in a formal parameter list
2. Variable parameters must be placed at the end of the formal parameter list
Format :
Format of variable parameters : data type ... Parameter name
public class MethodDemo {
public static void main(String[] args) {
sum();//1. Don't pass parameters
sum(10);//2. You can pass a parameter
sum(10,20,30);//3. Multiple parameters can be passed
sum(new int[]{10,20,40,50});//4. You can transfer an array
}
/**
* Be careful : There can only be one variable parameter in a formal parameter list
* @param nums
*/
public static void sum (int... nums){
// Be careful : Variable parameters are actually an array inside the method .nums
System.out.println(" Element number :" + nums.length);
System.out.println(" Element content :" + Arrays.toString(nums));
}
}
版权声明
本文为[The name of the father]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230311424748.html
边栏推荐
- 《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
- TP5 email (2020-05-27)
- How does Microsoft solve the problem of multiple PC programs
- Judge whether there is a leap year in the given year
- 建立与遍历二叉树
- [Mysql] LEFT函数 | RIGHT函数
- OLED multi-level menu record
- Student achievement management
- IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
- 【VS Code】解决jupyter文件在vs code中显示异常的问题
猜你喜欢
[mock data] fastmock dynamically returns the mock content according to the incoming parameters
Xamarin effect Chapter 22 recording effect
Recursion - outputs continuously increasing numbers
[untitled]
软件测试相关知识~
研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台
ASP. Net 6 middleware series - conditional Middleware
What kind of experience is it to prepare for a month to participate in ACM?
类似Jira的十大项目管理软件
C WPF UI framework mahapps switching theme
随机推荐
利用正反遍历来解决“字符的最短距离”问题
MAUI初体验:爽
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
Drawing polygons with < polygon / > circular array in SVG tag
yes. Net future
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
C语言实现通讯录----(静态版本)
Blazor University (11)组件 — 替换子组件的属性
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
Use split to solve the "most common words" problem
Mysql database
OLED multi-level menu record
ASP. Net 6 middleware series - conditional Middleware
准备一个月去参加ACM,是一种什么体验?
TP5 multi conditional where query (using PHP variables)
Use DFS to solve the problem of "number of dictionary rows"
Top 9 task management system in 2022
General testing technology [1] classification of testing