/*
* 学好程序的方法:
*
* 1 分析清楚任何一个程序、函数、类和对象和变量在内存中清楚的内存分配和运行调用情况
* 2 分析清楚任何一个Java类的实现情况
* 3 多看看源代码
4 每一个知识点网上都有详细说明,除了学书本的,也要补充网上的知识
*
* */
清单 1
// 使用 for-each 类型的循环.
class ForEach {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
int sum = 0;
// 使用for-each 类型的循环求和
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
}
System.out.println("Summation: " + sum);
}
}
清单 2
// 在 for-each 循环中使用break,下面程序仅计算nums的前五个元素之和.
class ForEach2 {
public static void main(String args[]) {
int sum = 0;
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
// 求和.
for(int x : nums) {
System.out.println("Value is: " + x);
sum += x;
if(x == 5) break; // 当X是5时跳出循环
}
System.out.println("Summation of first 5 elements: " + sum);
}}
清单 3
// for-each循环中的迭代变量是"只读的",因为它与数组的下标相关,
//对迭代变量付值时,对数组下标是没有影响的.
class NoChange {
public static void main(String args[]) {
int nums[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
for(int x : nums) {
System.out.print(x + " ");
x = x * 10; // 不会影响 nums
}
System.out.println();
for(int x : nums)
System.out.print(x + " ");
System.out.println();
}
}
程序的输出:
1 2 3 4 5 6 7 8 9 10
1 2 3 4 5 6 7 8 9 10
清单 4
// 在多维数组上使用for-each
//在JAVA中,多维数组是数组的数组, 每次迭代时要取下一个数组而不是单个元素,
//如二维数组的情况,迭代变量是对一维数组的引用.
class ForEach3 {
public static void main(String args[]) {
int sum = 0;
int nums[][] = new int[3][5];
// 给 nums 赋值
for(int i = 0; i < 3; i++)
for(int j=0; j < 5; j++)
nums[i][j] = (i+1)*(j+1);
// 用 for-each 显示数组中的元素并求和
for(int x[] : nums) {
for(int y : x) {
System.out.println("Value is: " + y);
sum += y;
}
}
System.out.println("Summation: " + sum);
}
}
清单 5
// 用for-each循环搜索数组
class Search {
public static void main(String args[]) {
int nums[] = { 6, 8, 3, 7, 5, 6, 1, 4 };
int val = 5;
boolean found = false;
// 用 for-each 在数组中搜索一个值
for(int x : nums) {
if(x == val) {
found = true;
break;
}
}
if(found)
System.out.println("Value found!");
}
}
清单 6
// 在collection集合的元素中进行for each循环
import java.util.*;
class AvgCollection {
static double getAvg(ArrayList nums) {
double sum = 0.0;
for(double itr : nums)
sum = sum + itr;
return sum / nums.size();
}
public static void main(String args[]) {
ArrayList list = new ArrayList();
list.add(10.14);
list.add(20.22);
list.add(30.78);
list.add(40.46);
double avg = getAvg(list);
System.out.println("List average is " + avg);
}
}
清单 7
// 在一个 Iterable 对象上使用for each循环.
import java.util.*;
// 这个类支持字符串中字符的迭代
class StrIterable implements Iterable,
Iterator {
private String str;
private int count = 0;
StrIterable(String s) {
str = s;
}
public boolean hasNext() {
if(count < str.length()) return true;
return false;
}
public Character next() {
if(count == str.length())
throw new NoSuchElementException();
count++;
return str.charAt(count-1);
}
public void remove() {
throw new UnsupportedOperationException();
}
// 这个方法实现Iterable.
public Iterator iterator() {
return this;
}
}
class ForEachIterable {
public static void main(String args[]) {
StrIterable x = new StrIterable("This is a test.");
// 显示每一个字符
for(char ch : x)
System.out.print(ch);
System.out.println();
}
}
运行结果:
C:\java>java ForEachIterable
This is a test.
分享到:
相关推荐
下面是用一个传统的for循环遍历集合的例子:用传统的for循环遍历数组的做法如下:这些做法逗比while循环更好,但是它们并不完美。如果真的那么不幸,并且外部集
MATLAB工具箱大全-将多重循环转为单重循环-For-Each
从Java5起,在Java中有了for-each循环,可以用来循环遍历collection和array。For each循环允许你在无需保持传统for循环中的索引,或在使用iterator /ListIterator时无需调用while循环中的hasNext()方法就能遍历...
本文详解了JAVA中的for-each循环与迭代,是JS入门学习中的基础知识,也是java中的难点知识,需要的朋友可以参考下 。
VB For each循环实例 VB For each循环实例
22 For-Each 23 PlotPub 24 Sheffield大学最新遗传算法工具箱 25 Camera Calibration 像机标定工具箱 26 Qhull(二维三维三角分解、泰森图)凸包工具箱 2019版 27 jplv7 28 MatlabFns 29 张量工具箱Tensor Toolbox 30 ...
1. Xcode的使用: ...(2.)for-each循环 (3.)GC机制 (4.)协议的必选与可选方法 3. Objective-C 2.0的运行时编程: (1.)isa指针 (2.)SEL 与IMP (3.)objc_msgSend函数 (4.)动态方法解析 (5.)消息转发
它提供了对于自前一版以来添加的所有新特性的完整描述,包括泛型、注释、断言、自动装臬、枚举、for-each循环、可变元数方法和静态导入子句。 本书试图详细说明Java语言的语法和语义规范。编者试图在每一种语言结构...
它提供了对于自前一版以来添加的所有新特性的完整描述,包括泛型、注释、断言、自动装臬、枚举、for-each循环、可变元数方法和静态导入子句。, 本书试图详细说明Java语言的语法和语义规范。编者试图在每一种语言结构...
Collection接口继承自Iterable接口,这意味着可以通过for-each循环遍历集合元素。 List接口是有序的集合,可以包含重复的元素。List接口提供了按照索引位置访问元素的方法,以及添加、删除和修改元素的操作。List...
它提供了对于自前一版以来添加的所有新特性的完整描述,包括泛型、注释、断言、自动装臬、枚举、for-each循环、可变元数方法和静态导入子句。, 本书试图详细说明Java语言的语法和语义规范。编者试图在每一种语言结构...
for_each 返回值实例详解;
java学习资源,斗地主发牌,构造函数 对象数组 for-each循环等
无序:HashMap中的元素没有固定的顺序,可以通过迭代器或for-each循环遍历元素。 总之,HashMap是一种非常实用的数据结构,可以用于存储键值对,并且具有较高的性能和灵活性。但是,在使用HashMap时需要注意线程...
还有就是java5 的一些新特性,如参数化类型,for-each 循环。其实我主要是看有关java编程方面的,而测试驱动就没那么关注。从这本书中学到了很多编程技巧和规范,以及加深了对面向对象基本原理的理解。看完才知道...
// 方法二:jdk1.5支持,用entrySet()和For-Each循环() Java代码 for (Map.Entry, String> m : emails.entrySet()) { logger.info("email-" + m.getKey() + ":" + m.getValue()); } for (Map....
JDK1.5安装包,JDK1.5的一个重要主题就是通过新增一些特性来简化开发,这些特性包括泛型,for-each 循环,自动装包/拆包,枚举,可变参数, 静态导入 。使用这些特性有助于我们编写更加清晰,精悍,安全的代码。
安装包,的一个重要主题就是通过新增一些特性来简化开发,这些特性包括泛型,for-each 循环,自动装包/拆包,枚举,可变参数, 静态导入 。使用这些特性有助于我们编写更加清晰,精悍,安全的代码。
“JDK1.5”(开发代号猛虎)的一个重要主题就是通过新增一些特性来简化开发,这些特性包括泛型,for-each 循环,自动装包/拆包,枚举,可变参数, 静态导入 。使用这些特性有助于我们编写更加清晰,精悍,安全的...
使用“ for-each”循环列出每个名称给定一个主要功能: String [] names = { " Bob " , " Kenn " , " Kevin " , " Erin " };ArrayList< String> studentList = new ArrayList< String> ();studentList . addAll( A