日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java语法糖之foreach

發布時間:2024/4/13 java 29 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Java语法糖之foreach 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
語法糖是一種幾乎每種語言或多或少都提供過的一些方便程序員開發代碼的語法,它只是編譯器實現的一些小把戲罷了,編譯期間以特定的字節碼或者特定的方式對這些語法做一些處理,開發者就可以直接方便地使用了。這些語法糖雖然不會提供實質性的功能改進,但是它們或能提高性能、或能提升語法的嚴謹性、或能減少編碼出錯的機會。Java提供給了用戶大量的語法糖,比如泛型、自動裝箱、自動拆箱、foreach循環、變長參數、內部類、枚舉類、斷言(assert)等。 package com.learn.foreach;import java.util.ArrayList; import java.util.List;public class ForeachTest {public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("s1");list.add("s2");for (String s : list) {System.out.println(s);}} } 反編譯出來的內容很多,看不懂也沒關系,關鍵看到Iterator這個標志,其實在對有實現Iterable接口的對象采用foreach語法糖的話,編譯器會將這個for關鍵字轉化為對目標的迭代器使用。上面的代碼會被轉化為如下的代碼:package com.learn.foreach;import java.util.ArrayList; import java.util.List;import com.learn.iterator.Iterator;public class ForeachTest {/*** 編譯器會對沒有顯示構造函數的類添加一個默認的構造函數,* 這個階段是在語義分析階段完成的*/public ForeachTest() {super();}public static void main(String[] args) {List<String> list = new ArrayList<String>();list.add("s1");list.add("s2");for(Iterator it = (Iterator) list.iterator();it.hasNext();) {String s = (String)it.next();{System.out.println(s);}}} } 注:如果要想使自己自定義的類可以采用foreach語法糖就必須實現Iterable接口。 java中的數組也可以采用foreach的語法糖,但是數組并沒有實現Iterable接口呀。 package com.learn.foreach;public class ForeachTest2 {public static void main(String[] args) {String arr[] = {"s1","s2"};for (String s : arr) {System.out.println(s);}}} 數組的foreach語法糖并沒有采用Iterable實現轉換。真正的解析結果如下所示:package com.learn.foreach;public class ForeachTest2 {public ForeachTest2() {super();}public static void main(String[] args) {int arr[] = {1,2};int [] arr$ = arr;for(int len$ = arr$.length; i$=0; i$<len$; ++i$) {int i = arr$[i$];{System.out.println(i);}}}} 可以看到對于數組而言,其實就是轉換為普通的遍歷而已;關于foreach語法糖的信息就這樣結束了嚒? 顯然沒有!對于實現RandomAccess接口的集合比如ArrayList,應當使用最普通的for循環而不是foreach循環來遍歷,所以第一個例子中有欠妥之處。 首先看一下jdk1.7中對RandomAccess接口的定義:java.util Interface RandomAccessAll Known Implementing Classes: ArrayList, AttributeList, CopyOnWriteArrayList, RoleList, RoleUnresolvedList, Stack, Vectorpublic interface RandomAccess Marker interface used by List implementations to indicate that they support fast (generally constant time) random access. The primary purpose of this interface is to allow generic algorithms to alter their behavior to provide good performance when applied to either random or sequential access lists. The best algorithms for manipulating random access lists (such as ArrayList) can produce quadratic behavior when applied to sequential access lists (such as LinkedList). Generic list algorithms are encouraged to check whether the given list is an instanceof this interface before applying an algorithm that would provide poor performance if it were applied to a sequential access list, and to alter their behavior if necessary to guarantee acceptable performance. It is recognized that the distinction between random and sequential access is often fuzzy. For example, some List implementations provide asymptotically linear access times if they get huge, but constant access times in practice. Such a List implementation should generally implement this interface. As a rule of thumb, a List implementation should implement this interface if, for typical instances of the class, this loop: for (int i=0, n=list.size(); i < n; i++) list.get(i);runs faster than this loop: for (Iterator i=list.iterator(); i.hasNext(); ) i.next();This interface is a member of the Java Collections Framework.

?

總結

以上是生活随笔為你收集整理的Java语法糖之foreach的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。