java集合框架02
阿新 • • 發佈:2017-06-15
array set map集合 操作 不能 highlight ash listt next()
public class ListTest { public static void main(String[] args) { //創建幾個新聞對象 News news1=new News(1, "新聞1"); News news2=new News(2, "新聞2"); News news3=new News(3, "新聞3"); //創建一個List集合 List list=new ArrayList(); //現在 集合中存放的Object list.add(1); list.add("abc"); list.add(news1); //遍歷集合 for (Object object : list) { System.out.println(object); } System.out.println("**************************"); /* * 現在 我只想存放 News 新聞 * 泛型集合 * 在創建集合的時候,就已經規定了 集合中允許存放的類型! * 如果後續進行新增操作時候,插入了類型不一致的數據,會編譯報錯! */ List<News> news=new ArrayList<News>(); //new ArrayList(); //news.add(1); 編譯報錯 //news.add(null); 可以存放 Null news.add(news1); news.add(news2); news.add(news3); System.out.println("集合的大小:"+news.size()); //4 //遍歷 for (News n : news) { System.out.println(n.getTitle()); //不需要強制類型轉化 直接輸出 } System.out.println("***********************"); //List <int> i=new ArrayList(); 不能使用基本數據類型 List<Integer> i=new ArrayList(); //對應類型的封裝類 List<String> s=new ArrayList(); //只能存放String類型 } } Map的泛型集合 public class Student { private Integer studentNo; //學生的編號 是唯一的 private String name; private Integer age; private String sex; @Override public String toString() { return "Student [studentNo=" + studentNo + ", name=" + name + ", age=" + age + ", sex=" + sex + "]"; } public Student() { super(); } public Student(Integer studentNo, String name, Integer age, String sex) { super(); this.studentNo = studentNo; this.name = name; this.age = age; this.sex = sex; } public Integer getStudentNo() { return studentNo; } public void setStudentNo(Integer studentNo) { this.studentNo = studentNo; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } } public class MapTest { public static void main(String[] args) { /* * 創建一個Map的泛型集合 * 之前 * Map map=new HashMap(); * key和value全都是 Object類型 */ Map m=new HashMap(); m.put(1, 2); m.put("1","2"); m.put(1.0, 2.0); System.out.println("map集合的大小:"+m.size()); //3 System.out.println("************************************"); //創建幾個新聞對象 Student stu1=new Student(1, "奧黑驢1", 1000, "女"); Student stu2=new Student(2, "奧黑驢2", 10000, "女"); Student stu3=new Student(3, "奧黑驢3", 1000, "女"); //泛型集合 key只能是Integer Map<Integer,Student> map=new HashMap<Integer, Student>(); //map.put(1, 1); 默認裝箱操作 int基本數據類型轉換成Interge封裝類 map.put(stu1.getStudentNo(), stu1); map.put(stu2.getStudentNo(), stu2); map.put(stu3.getStudentNo(), stu3); //遍歷之前獲取keySet Set<Integer> keySet = map.keySet(); //01. for加強 for (Integer key : keySet) { //根據key取得對應的Student對象 System.out.println(map.get(key)); } System.out.println("***************************"); //02. iterator Iterator<Integer> it = keySet.iterator(); while (it.hasNext()) { /*Integer key=it.next(); map.get(key);*/ System.out.println(map.get(it.next())); } System.out.println("***************************"); /* * 03. entrySet() 同時可以獲取 key和value * 之前都是先獲取key 之後拿key取得value */ Iterator iterator = map.entrySet().iterator(); while (iterator.hasNext()) { Entry entry = (Entry) iterator.next(); System.out.println("map的key===>"+entry.getKey()); System.out.println("map的value===>"+entry.getValue()); } } }
java集合框架02