Java基础笔记 – 增强的for循环For each循环 自动装箱拆箱 可变参数
生活随笔
收集整理的這篇文章主要介紹了
Java基础笔记 – 增强的for循环For each循环 自动装箱拆箱 可变参数
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
1、For each循環:1.1、語法:1.2、For each循環的使用:1.3、嵌套For each循環:1.4、三種循環遍歷集合的列舉:1.5、增強的for循環的缺點:2、自動裝箱/拆箱(Autoboxing/unboxing):2.1、自動裝箱:2.2、自動拆箱:2.3、Integer類相關的源代碼:2.3.1、valueOf方法:3、可變參數:3.1、包含可變參數的函數:3.2、接收參數:3.3、說明:
1、For each循環:
JDK5.0中提供的新特性For each循環的加入簡化了集合的遍歷。
1.1、語法: for(type element : array){… System.out.println(element);… } 1.2、For each循環的使用: public static void main(String[] args) {List<String> arrayList = new ArrayList<String>();arrayList.add("a");arrayList.add("b");arrayList.add("c");//普通的循環方式for(int i=0; i<arrayList.size(); i++){System.out.println(arrayList.get(i));}//使用For each循環for(String element : arrayList){System.out.println(element);} } 1.3、嵌套For each循環: int[][] array = {{1,2,3},{1,2}}; for(int[] row : array){for(int element : row){System.out.println(element);} } 1.4、三種循環遍歷集合的列舉: //普通的循環方式 for(int i=0; i<arrayList.size(); i++){System.out.println(arrayList.get(i)); }//迭代循環 for(Iterator<String> iter = arrayList.iterator(); iter.hasNext();){System.out.println(iter.next()); }//使用For each循環 for(String element : arrayList){System.out.println(element); } 1.5、增強的for循環的缺點: 失去了索引信息,不能獲取到索引值,只能這樣獲取: //使用For each循環 int i = 0; for(String element : arrayList){System.out.println(i + element);i++; }因為增強的for循環會丟失下標信息,所以當遍歷集合或數組時,如果需要方法集合或數組的下標,最好使用舊式的方式來實現循環或遍歷。
2、自動裝箱/拆箱(Autoboxing/unboxing):自動裝箱/拆箱打打方便類基本類型數據和它們包裝類的使用。
自動裝箱:把基本數據類型轉換為包裝類。
自動拆箱:把包轉類型自動轉換為基本數據類型。
2.1、自動裝箱: //自動裝箱 Collection<Integer> c = new ArrayList<Integer>(); c.add(1); c.add(2); c.add(3); 2.2、自動拆箱: //自動拆箱 for(Integer integer : c){System.out.println(integer); } 2.3、Integer類相關的源代碼: 2.3.1、valueOf方法: public static Integer valueOf(int i) {if(i >= -128 && i <= IntegerCache.high)return IntegerCache.cache[i + 128];elsereturn new Integer(i);} 這里對應-128~127的整數調用了IntegerCache.cache[i + 128];詳細代碼如下: private static class IntegerCache {static final int high;static final Integer cache[];static {final int low = -128;// high value may be configured by propertyint h = 127;if (integerCacheHighPropValue != null) {// Use Long.decode here to avoid invoking methods that// require Integer's autoboxing cache to be initializedint i = Long.decode(integerCacheHighPropValue).intValue();i = Math.max(i, 127);// Maximum array size is Integer.MAX_VALUEh = Math.min(i, Integer.MAX_VALUE - -low);}high = h;cache = new Integer[(high - low) + 1];int j = low;for(int k = 0; k < cache.length; k++)cache[k] = new Integer(j++);}private IntegerCache() {}}其主要的功能就是把-128~127的整數緩存起來,當藥使用這些數字時,就從這些緩存中獲取。
由此可知下面代碼運行的結果為true:
Integer m = 127; Integer n = 127; System.out.println(m == n);而下面的代碼則為false:
Integer m = 128; Integer n = 128; System.out.println(m == n); 3、可變參數:可變參數使得可以聲明一個接受一個可變數目參數的方法。
可變參數必須是方法聲明中的最后一個參數。
3.1、包含可變參數的函數: public static int add(int... arrays){int sum = 0;for(Integer integer : arrays){sum += integer;}return sum; }可變參數部分相當于數組。
3.2、接收參數: 可以為多個參數或數組: //傳遞數組 add(new int[]{1,2,3}); //傳遞參數 add(1,2,3); 3.3、說明:可變參數本質上是一個數組,聲明了可變參數的方法,既可以傳遞離散的值,也可以傳遞一個數組對象。
可變參數必須作為方法參數的最后一個參數
。
總結
以上是生活随笔為你收集整理的Java基础笔记 – 增强的for循环For each循环 自动装箱拆箱 可变参数的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 停止Java线程,小心interrupt
- 下一篇: Android开发中的多线程编程技术