当前位置:网站首页>可以接收多種數據類型參數——可變參數
可以接收多種數據類型參數——可變參數
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
边栏推荐
- C syntax pattern matching [switch expression]
- Mysql database, inconsistent index character set, slow SQL query, interface timeout
- If the deep replication of objects is realized through C #?
- 队列的存储和循环队列
- MYSQL03_ SQL overview, rules and specifications, basic select statements, display table structure
- Vs code setting line feed
- TP5 inherits base and uses the variables in base
- ASP. Net 6 middleware series - Custom middleware classes
- LNMP MySQL allows remote access
- 2022年做跨境电商五大技巧小分享
猜你喜欢

This new feature of C 11, I would like to call it the strongest!

12.<tag-链表和常考点综合>-lt.234-回文链表
![General testing technology [1] classification of testing](/img/f1/d80b6793b6443cbc4048d7e6319f51.png)
General testing technology [1] classification of testing

研讨会回放视频:如何提升Jenkins能力,使其成为真正的DevOps平台

Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!

What kind of experience is it to prepare for a month to participate in ACM?

全网最全,接口自动化测试怎么做的?精通接口自动化测试详解

一套组合拳,打造一款 IDEA 护眼方案

Ide-idea-problem

编码电机PID调试(速度环|位置环|跟随)
随机推荐
2022T电梯修理考试模拟100题及在线模拟考试
IOTOS物联中台对接海康安防平台(iSecure Center)门禁系统
Tencent video VIP member, weekly card special price of 9 yuan! Tencent official direct charging, members take effect immediately!
在.NE6 WebApi中使用分布式缓存Redis
Laravel's own paging query
微软是如何解决 PC 端程序多开问题的
After the mobile phone is connected to the computer, how can QT's QDIR read the mobile phone file path
Configuration table and page information automatically generate curd operation page
Tips in MATLAB
xutils3修改了我提报的一个bug,开心
LoadRunner - performance testing tool
搭建XAMPP时mysql端口被占用
中后二叉建树
General testing technology [1] classification of testing
MySQL port is occupied when building xampp
可以接收多种数据类型参数——可变参数
PID debugging of coding motor (speed loop | position loop | follow)
LNMP MySQL allows remote access
Recursion - outputs continuously increasing numbers
Aspnetcore configuration multi environment log4net configuration file