集合框架-工具类-JDK5.0特性-函数可变参数

1 package cn.itcast.p4.news.demo;
2
3 public class ParamterDemo {
4
5 public static void main(String[] args) {
6 // TODO Auto-generated method stub
7 // int sum = add(4, 5);
8 // System.out.println("sum="+sum);
9 // int sum1 = add(4, 5, 6);
10 // System.out.println("sum1="+sum1);
11
12 // int[] arr = {5,1,4,7,3};
13 // int sum =add(arr);
14 // System.out.println("sum="+sum);
15
16 int sum = newAdd(5,1,4,7,3);
17 System.out.println("sum="+sum);
18 }
19 /*
20 * 函数的可变参数。
21 * 其实就是一个数组,但是接收的是数组中的元素。
22 * 自动将这些元素封装成数组。简化了调用者的书写。
23 *
24 *
25 */
26 public static int newAdd(int.../*...代表省略*/ arr/*,int a*/) {//可变参数
27
28 int sum = 0;
29 for (int i = 0; i < arr.length; i++) {
30 sum+=arr[i];
31 }
32 return sum;
33 // System.out.println(arr);
34 //
35 // return 0;
36 }
37 //注意:可变参数类型,必须定义在参数列表的结尾
38 public static int newAdd(int a,int.../*...代表省略*/ arr) {//可变参数
39
40 int sum = 0;
41 for (int i = 0; i < arr.length; i++) {
42 sum+=arr[i];
43 }
44 return sum;
45 // System.out.println(arr);
46 //
47 // return 0;
48 }
49
50 public static int add(int[] arr) {
51
52 int sum = 0;
53 for (int i = 0; i < arr.length; i++) {
54 sum+=arr[i];
55 }
56 return sum;
57 }
58
59 public static int add(int a,int b) {
60 return a+b;
61 }
62
63 public static int add(int a,int b, int c) {
64 return a+b+c;
65 }
66
67 }
ParamterDemo
集合框架-工具类-JDK5.0特性-函数可变参数的更多相关文章
- 集合框架-工具类-JDK5.0特性-ForEach循环
1 package cn.itcast.p4.news.demo; 2 3 import java.util.ArrayList; 4 import java.util.HashMap; 5 impo ...
- 集合框架-工具类-JDK5.0特性-静态导入
1 package cn.itcast.p4.news.demo; 2 3 import java.util.ArrayList; 4 //import java.util.Collections; ...
- JAVA基础学习之 Map集合、集合框架工具类Collections,Arrays、可变参数、List和Set集合框架什么时候使用等(4)
package com.itcast.test20140113; import java.util.ArrayList; import java.util.Arrays; import java.ut ...
- java基础37 集合框架工具类Collections和数组操作工具类Arrays
一.集合框架工具类:Collections 1.1.Collections类的特点 该工具类中所有的方法都是静态的 1.2.Collections类的常用方法 binarySearch(List< ...
- JDK5新特性之 可变参数的方法
可变参数的方法:不知道这个方法该定义多少个参数 注意: > 参数实际上是数组 > 必须写在参数列表最后一个 package cn.itcast.day24.varparam; import ...
- Java基础---泛型、集合框架工具类:collections和Arrays
第一讲 泛型(Generic) 一.概述 1.JDK1.5版本以后出现的新特性.用于解决安全问题,是一个类型安全机制. 2.JDK1.5的集合类希望在定义集合时,明确表明你要向集合中装入那种类 ...
- Java 集合框架工具类
Collections Arrays Collections 集合框架的工具类, 里面的方法都是静态的, 可以直接使用类名调用 常用方法 1. sort(List<T> list); // ...
- java集合框架——工具类
一.概述 JAVA集合框架中有两个很重要的工具类,一个是Collections,另一个是Arrays.分别封装了对集合的操作方法和对数组的操作方法,这些操作方法使得程序员的开发更加高效. public ...
- 集合框架-工具类-Arrays-asList方法
1 package cn.itcast.p3.toolclass.arrays.demo; 2 3 import java.util.ArrayList; 4 import java.util.Arr ...
随机推荐
- Intellij IDEA中Springboot启动报Command line is too long错误
启动报错:Error running 'CmsFrontApplication': Command line is too long. Shorten command line for CmsFron ...
- 鱼书_第一章_Python入门
Python版本 Python有Python 2.x和Python 3.x两个版本.两个版本不兼容,可能出现用Python 3.x编的代码不能被Python 2.x执行的情况. Python安装 An ...
- 【九度OJ】题目1137:浮点数加法 解题报告
[九度OJ]题目1137:浮点数加法 解题报告 标签(空格分隔): 九度OJ 原题地址:http://ac.jobdu.com/problem.php?pid=1137 题目描述: 求2个浮点数相加的 ...
- 【九度OJ】题目1203:IP地址 解题报告
[九度OJ]题目1203:IP地址 解题报告 标签(空格分隔): 九度OJ http://ac.jobdu.com/problem.php?pid=1203 题目描述: 输入一个ip地址串,判断是否合 ...
- 【LeetCode】85. Maximal Rectangle 解题报告(Python)
作者: 负雪明烛 id: fuxuemingzhu 个人博客: http://fuxuemingzhu.cn/ 题目地址: https://leetcode.com/problems/maximal- ...
- 【LeetCode】643. 子数组最大平均数 I Maximum Average Subarray I (Python)
作者: 负雪明烛 id: fuxuemingzhu 公众号:每日算法题 目录 题目描述 题目大意 解题方法 方法一:preSum 方法二:滑动窗口 刷题心得 日期 题目地址:https://leetc ...
- hdu-5568SUM (dp)
sequence2 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total S ...
- 【jvm】05-为什么你写的代码有时候和预期不一致
[jvm]05-为什么你写的代码有时候和预期不一致 欢迎关注b站账号/公众号[六边形战士夏宁],一个要把各项指标拉满的男人.该文章已在github目录收录. 屏幕前的大帅比和大漂亮如果有帮助到你的话请 ...
- 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具
查看本章节 查看作业目录 需求说明: 使用 history 对象和 location 对象中的属性和方法制作一个简易的网页浏览工具 实现思路: 使用history对象中的 forward() 方法和 ...
- Java面向对象笔记 • 【第5章 异常处理】
全部章节 >>>> 本章目录 5.1 异常概述 5.1.1 程序中的异常 5.1.2 异常分类 5.1.3 实践练习 5.2 try-catch处理异常 5.2.2 使用f ...