/* * 学好程序的方法: * * 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.