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

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

集合框架03

發布時間:2025/7/14 编程问答 22 豆豆
生活随笔 收集整理的這篇文章主要介紹了 集合框架03 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

三、增強for循環(foreach)

1 //好處:代碼少,方便遍歷 2 //弊端:沒有索引,不能操作容器里的元素 3 private static void function() { 4 int[] arr = {3,1,4,5,6}; 5 for(int i:arr){ 6 System.out.println(i); 7 } 8 }

四、泛型

/* 2 * JDK1.5 出現新的安全機制,保證程序的安全性 3 * 泛型: 指明了集合中存儲數據的類型 <數據類型> 4 */ 5 6 public class GenericDemo { 7 public static void main(String[] args) { 8 function(); 9 } 10 11 public static void function(){ 12 Collection<String> coll = new ArrayList<String>(); 13 coll.add("abc"); 14 coll.add("rtyg"); 15 coll.add("43rt5yhju"); 16 // coll.add(1); 17 18 Iterator<String> it = coll.iterator(); 19 while(it.hasNext()){ 20 String s = it.next(); 21 System.out.println(s.length()); 22 } 23 } 24 } /** 帶有泛型的接口* * public interface List <E>{* abstract boolean add(E e);* }* 實現類,先實現接口,不理會泛型* public class ArrayList<E> implements List<E>{* }* 調用者 : new ArrayList<String>() 后期創建集合對象的時候,指定數據類型* * 實現類,實現接口的同時,也指定了數據類型* public class XXX implements List<String>{* }* new XXX()*/ 1 /* 2 * 泛型的通配符 3 */ 4 public class GenericDemo { 5 public static void main(String[] args) { 6 ArrayList<String> array = new ArrayList<String>(); 7 8 HashSet<Integer> set = new HashSet<Integer>(); 9 10 array.add("123"); 11 array.add("456"); 12 13 set.add(789); 14 set.add(890); 15 16 iterator(array); 17 iterator(set); 18 } 19 /* 20 * 定義方法,可以同時迭代2個集合 21 * 參數: 怎么實現 , 不能寫ArrayList,也不能寫HashSet 22 * 參數: 或者共同實現的接口 23 * 泛型的通配,匹配所有的數據類型 ? 24 */ 25 public static void iterator(Collection<?> coll){ 26 Iterator<?> it = coll.iterator(); 27 while(it.hasNext()){ 28 //it.next()獲取的對象,什么類型 29 System.out.println(it.next()); 30 } 31 } 32 }?

轉載于:https://www.cnblogs.com/Nelsoner/p/6677685.html

總結

以上是生活随笔為你收集整理的集合框架03的全部內容,希望文章能夠幫你解決所遇到的問題。

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