JDK5.0新特性系列---2.新的for循环
2012-12-03 14:00
507 查看
import java.util.ArrayList;
import java.util.List;
/**
* 新的for循环,格式为for(type x:type y)
* 表示遍历数组或集合y的元素,把元素值赋给x
*/
public class ForEach {
/**对整数数组求和*/
public static long getSum(int[]
nums) throws Exception{
if(nums == null)
throw new Exception("错误的参数输入,不能为null!");
long sum = 0;
//依次取得nums元素的值并累加
for(int x
: nums){
sum += x;
}
return sum;
}
/**对整数列表求和*/
public static long getSum(List<Integer>
nums) throws Exception{
if(nums == null)
throw new Exception("错误的参数输入,不能为null!");
long sum = 0;
//可以与遍历数组一样的方式遍历列表
for(int x:nums){
sum += x;
}
return sum;
}
/**求多维数组的平均值*/
public static int getAvg(int[][]
nums) throws Exception{
if(nums == null)
throw new Exception("错误的参数输入,不能为null!");
long sum = 0;
long size = 0;
//对于二维数组,每个数组元素都是一维数组
for(int[]
x : nums){
//一维数组中的元素才是数字
for(int y
: x){
sum += y;
size ++;
}
}
return (int)(sum/size);
}
public static void main(String[]
args)throws Exception{
int[] nums = {456,23,-739,163,390};
List<Integer> list_I = new ArrayList<Integer>();
for(int i
= 0; i < 5; i++){
list_I.add(nums[i]);
}
System.out.println(getSum(nums));
System.out.println(getSum(list_I));
int[][] numss = {{1,2,3},{4,5,6},{7,8,9,10}};
System.out.println(getAvg(numss));
}
}
import java.util.List;
/**
* 新的for循环,格式为for(type x:type y)
* 表示遍历数组或集合y的元素,把元素值赋给x
*/
public class ForEach {
/**对整数数组求和*/
public static long getSum(int[]
nums) throws Exception{
if(nums == null)
throw new Exception("错误的参数输入,不能为null!");
long sum = 0;
//依次取得nums元素的值并累加
for(int x
: nums){
sum += x;
}
return sum;
}
/**对整数列表求和*/
public static long getSum(List<Integer>
nums) throws Exception{
if(nums == null)
throw new Exception("错误的参数输入,不能为null!");
long sum = 0;
//可以与遍历数组一样的方式遍历列表
for(int x:nums){
sum += x;
}
return sum;
}
/**求多维数组的平均值*/
public static int getAvg(int[][]
nums) throws Exception{
if(nums == null)
throw new Exception("错误的参数输入,不能为null!");
long sum = 0;
long size = 0;
//对于二维数组,每个数组元素都是一维数组
for(int[]
x : nums){
//一维数组中的元素才是数字
for(int y
: x){
sum += y;
size ++;
}
}
return (int)(sum/size);
}
public static void main(String[]
args)throws Exception{
int[] nums = {456,23,-739,163,390};
List<Integer> list_I = new ArrayList<Integer>();
for(int i
= 0; i < 5; i++){
list_I.add(nums[i]);
}
System.out.println(getSum(nums));
System.out.println(getSum(list_I));
int[][] numss = {{1,2,3},{4,5,6},{7,8,9,10}};
System.out.println(getAvg(numss));
}
}
相关文章推荐
- 黑马程序员--基础--第一篇--JDK5.0新特性泛型 静态倒入 自动拆装箱 不定项参数 增强for如循环 枚举
- JDK5.0新特性系列---11.5.1线程 同步装置之Semaphore
- day9 JDK 新特性--注解、新 for 循环、静态导入、自动装箱拆箱、泛型、可变长参数
- JDK5.0新特性系列---1.自动装箱和拆箱
- Java5.0新特性03-增强for循环
- JDK5.0新特性系列---2.新的for循环
- JDK5.0新特性系列---11.5.4线程 同步装置之Exchanger
- JDK5.0新特性系列---3.枚举类型
- (黑马程序员)JDK5.0新特性,高级for的使用
- Day 07 debug调试 junit单元测试 jdk5.0新特性 泛型,枚举,静态导入,拆装箱,增强for,可变参数 反射
- JDK5.0新特性系列---7.使用ProcessBuilder执行本地命令
- 20-常用对象API(集合框架-JDK5.0特性-ForEach循环) 21-常用对象API(集合框架-JDK5.0特性-函数可变参数). 1 2
- JDK5.0新特性系列---9.注释功能Annotation
- JDK5.0新特性系列---11.2线程 任务执行架构
- 19-集合框架工具类-20-常用对象API(集合框架-JDK5.0特性-ForEach循环)
- JDK5.0新特性系列---11.3线程 锁Lock
- jdk 1.5新特性--for 循环加强
- 无法转向JDK 5.0?学习一款开放源代码工具如何帮助在旧版 JVM 上使用这些特性
- 黑马程序员——JDK5.0新特性(泛型)
- jdk5.0新特性