Java-foreach分析總結


今天晚上被定積分搞得頭昏,拿煙的手,微微顫抖。

 

foreach總結:

  foreach是java5的新特性,有人說是抄襲C#的,我也不太清楚,反正會用就行了,主要增強了for循環,在遍歷數組和集合上面很方便。

foreach是for循環的簡化版本,但是foreach不能完全取代for循環,然而任何foreach都可以改寫為for循環,foreach不是一個關鍵字,主要就是這個格式,每一個for的意思。

 

foreach語句格式:

  for(元素類型type  元素變量value : 遍歷對象obj) { 

    引用x的java語句;  

}

 

下面看幾段代碼,看看foeeach的強大和簡便:

 

  1 package cn.zpoor;
  2 
  3 import java.util.List;
  4 import java.util.ArrayList;
  5 import java.util.Arrays;
  6 
  7 /**
  8  * @author 薛定諤的貓
  9  * foreach遍歷數組和集合*/
 10 public class TestArray {
 11 
 12     public void initArray() {
 13         //定義並且初始化一個數組
 14         int arr[] = {2,5,3};
 15         System.out.println("-----排序前的一維數組:");
 16         for(int x:arr) {
 17             System.out.print(x + " ");//foreach循環逐個輸入數組元素的值
 18         }
 19         System.out.println();
 20         
 21         //數組冒泡排序
 22         Arrays.sort(arr);
 23         //foreach輸出排序之后的數組元素
 24         System.out.println("-----排序后的一維數組:");
 25         for(int x:arr) {
 26             System.out.print(x+" ");
 27         }
 28         System.out.println();
 29     }
 30     
 31     //集合轉換成數組
 32     public void listToArray() {
 33         //創建list並且添加元素
 34         List<String> list = new ArrayList<String>();
 35         list.add("1");
 36         list.add("2");
 37         list.add("5");
 38         
 39         //foreach循環輸出集合元素
 40         System.out.println("-----foreach輸出集合元素");
 41         for(String x:list) {
 42             System.out.println(x+ " ");
 43         }
 44         System.out.println();
 45         
 46         //把ArrayList轉換成數組
 47         Object[] obj = list.toArray();
 48         System.out.println("-----foreach輸出集合轉換而來的數組元素");
 49         for(Object x:obj) {
 50             System.out.print(x.toString()+ " ");//逐個輸出數字元素的值
 51         }
 52         System.out.println();
 53     }
 54     
 55     //foreach輸出二維數組
 56     public void twoDimensionArray() {
 57         int arr[][] = {{1,2},{3,4}};
 58         System.out.println("-----foreach輸出二維數組");
 59         for(int[] x:arr) {
 60             for(int e:x) {
 61                 System.out.print(e+ " ");//逐個輸出數組元素,多維數組的本質就是沒有多維數組
 62             }
 63         }
 64         System.out.println();
 65     }
 66     
 67     //foreach輸出三維數組
 68     public void threeDimensionArray() {
 69         int arr[][][] = {
 70                 {{1,2},{3,4}},
 71                 {{5,6},{7,8}}
 72         };
 73         
 74         System.out.println("-----foreach輸出三維數組");
 75         for(int[][] x:arr) {
 76             for(int[] y:x) {
 77                 for(int z:y) {
 78                     System.out.print(z + " ");//三重循環,有點作死
 79                 }
 80             }
 81         }
 82     }
 83     
 84     public static void main(String[] args) {
 85         TestArray test = new TestArray();
 86         test.initArray();
 87         test.listToArray();
 88         test.twoDimensionArray();
 89         test.threeDimensionArray();
 90     }
 91 }
 92 
 93 /*-----排序前的一維數組:
 94 2 5 3 
 95 -----排序后的一維數組:
 96 2 3 5 
 97 -----foreach輸出集合元素
 98 1 
 99 2 
100 5 
101 
102 -----foreach輸出集合轉換而來的數組元素
103 1 2 5 
104 -----foreach輸出二維數組
105 1 2 3 4 
106 -----foreach輸出三維數組
107 1 2 3 4 5 6 7 8 
108 */

 

foreach的局限性:

  如果要是引用數組或者集合的索引,foreach就沒法做到了,因為foreach只是遍歷一邊數組或者集合。

 

舉個栗子:

 1 package cn.zpoor;
 2 /**
 3  * @author 薛定諤的貓
 4  * foreach的局限性*/
 5 public class TestForeach {
 6     public static void main(String[] args) {
 7         //定義一個數組
 8         int[] arr = new int[4];
 9         System.out.println("輸出賦值前定義的數組");
10         
11         for(int x:arr) {
12             System.out.println(x);
13         }
14         
15         //通過索引賦值
16         System.out.println("給定義的數組賦值");
17         for(int i = 3;i>0;i--) {
18             arr[i] = i;
19         }
20         
21         //輸出賦值之后的數組
22         System.out.println("foreach輸出賦值之后的一維數組");
23         for(int x:arr) {
24             System.out.println(x);
25         }
26     }
27 }
28 
29 /*輸出賦值前定義的數組
30 0
31 0
32 0
33 0
34 給定義的數組賦值
35 foreach輸出賦值之后的一維數組
36 0
37 1
38 2
39 3
40 */

 

 

Conclusion:

  foreach是for循環的增強版本,簡化了編程,提高了代碼的可讀性和安全性(數組越界)。在遇到數組或者集合索引的情況下,foreach就不好用了,。而且foreach一般結合泛型使用。

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM