当前位置:网站首页>可以接收多種數據類型參數——可變參數
可以接收多種數據類型參數——可變參數
2022-04-23 03:13:00 【名之以父】
一、問題引入
假如需要定義一個方法求和,該方法可以靈活的完成如下需求:
- 計算1個數據的和。
- 計算2個數據的和。
- 計算3個數據的和。
- 計算n個數據的和,
- 甚至可以支持不接收參數進行調用。
二、作用
- 傳輸參數非常靈活,方便、 可以不傳參數,
- 可以傳輸一個參數 可以傳輸多個參數
- 可以傳輸一個數組
- 可變參數在方法內部本質上就是一個數組
可變參數的注意事項:
1.一個形參列錶中可變參數只能有一個
2.可變參數必須放在形參列錶的最後面
格式:
可變參數的格式:數據類型...參數名稱
public class MethodDemo {
public static void main(String[] args) {
sum();//1.不傳參數
sum(10);//2.可以傳一個參數
sum(10,20,30);//3.可以傳多個參數
sum(new int[]{10,20,40,50});//4.可以傳輸一個數組
}
/**
* 注意:一個形參列錶中只能有一個可變參數
* @param nums
*/
public static void sum (int... nums){
//注意:可變參數在方法內部其實就是一個數組。nums
System.out.println("元素個數:" + nums.length);
System.out.println("元素內容:" + Arrays.toString(nums));
}
}
版权声明
本文为[名之以父]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230311424748.html
边栏推荐
- 宁德时代地位不保?
- Mysql database, inconsistent index character set, slow SQL query, interface timeout
- 再战leetcode (290.单词规律)
- 利用栈的回溯来解决“文件的最长绝对路径”问题
- A set of C interview questions about memory alignment. Many people make mistakes!
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- MYSQL05_ Ordr by sorting, limit grouping, group by grouping
- TP5 inherits base and uses the variables in base
- IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
- [mock data] fastmock dynamically returns the mock content according to the incoming parameters
猜你喜欢
編碼電機PID調試(速度環|比特置環|跟隨)
MYSQL04_ Exercises corresponding to arithmetic, logic, bit, operator and operator
Yes Redis using distributed cache in NE6 webapi
“如何实现集中管理、灵活高效的CI/CD”在线研讨会精彩内容分享
Source code interpretation of Flink index parameters (read quantity, sent quantity, sent bytes, received bytes, etc.)
在.NE6 WebApi中使用分布式缓存Redis
微软是如何解决 PC 端程序多开问题的
svg标签中利用<polygon/>循环数组绘制多边形
Top 9 task management system in 2022
Laravel8- use JWT
随机推荐
Vs code setting line feed
2022a special equipment related management (elevator) work license question bank and simulation examination
Top ten project management software similar to JIRA
A set of C interview questions about memory alignment. Many people make mistakes!
How does Microsoft solve the problem of multiple PC programs
be based on. NETCORE development blog project starblog - (2) environment preparation and creation project
. net tip: talk about the problem that the scoped service cannot be obtained in the middleware structure
建立与遍历二叉树
Ide-idea-problem
The most detailed in the whole network, software testing measurement, how to optimize software testing cost and improve efficiency --- hot
Blazor University (12) - component lifecycle
OLED多级菜单记录
Swap the left and right of each node in a binary tree
C WPF UI framework mahapps switching theme
First in the binary tree
TP5 inherits base and uses the variables in base
Aspnetcore configuration multi environment log4net configuration file
Xamarin effect Chapter 21 expandable floating operation button in GIS
全网最全,接口自动化测试怎么做的?精通接口自动化测试详解
This new feature of C 11, I would like to call it the strongest!