当前位置:网站首页>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
边栏推荐
- Establishing and traversing binary tree
- “如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
- Recommend reading | share the trader's book list and ask famous experts for trading advice. The trading is wonderful
- Maui initial experience: Cool
- IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
- 中后二叉建树
- Top 9 task management system in 2022
- js递归树结构计算每个节点的叶子节点的数量并且输出
- 求二叉树的叶子结点个数
- 队列的存储和循环队列
猜你喜欢

2022G2电站锅炉司炉考试题库及在线模拟考试

Data mining series (3)_ Data mining plug-in for Excel_ Estimation analysis

类似Jira的十大项目管理软件

TP5 customization in extend directory succeeded and failed. Return information

【VS Code】解决jupyter文件在vs code中显示异常的问题

Xamarin effect Chapter 21 expandable floating operation button in GIS

Realize QQ login with PHP

Source generator actual combat

IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统

Tips in MATLAB
随机推荐
交换二叉树中每个结点的左和右
ASP. Net 6 middleware series - conditional Middleware
《C语言程序设计》(谭浩强第五版) 第8章 善于利用指针 习题解析与答案
全网讲的最细,软件测试度量,怎样优化软件测试成本提高效率---火爆
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
准备一个月去参加ACM,是一种什么体验?
【VS Code】解决jupyter文件在vs code中显示异常的问题
Establishing and traversing binary tree
Top 9 task management system in 2022
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
MYSQL_ From mastery to abandonment
Mysql database, inconsistent index character set, slow SQL query, interface timeout
. net core current limiting control - aspnetcoreratelimit
C language to achieve address book - (static version)
再战leetcode (290.单词规律)
svg标签中利用<polygon/>循环数组绘制多边形
The most easy to understand service container and scope of dependency injection
Use of metagroup object tuple in C
腾讯视频VIP会员,周卡特价9元!腾讯官方直充,会员立即生效!
【新版发布】ComponentOne 新增 .NET 6 和 Blazor 平台控件支持