增强的for循环合集

  1. ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml");
  2. Student student = (Student) ac.getBean("student");
  3. System.out.println("name "+student.getName());
  4. //从 数组中循环取值;
  5. System.out.println("*******************从 数组中循环取值********************");
  6. for (Object crouse : student.getCarry()){
  7. System.out.println("课程有"+crouse.toString());
  8. }
  9. //从 Set集合中循环取值;
  10. System.out.println("*******************从 Set集合中循环取值********************");
  11. for (String crouse : student.getCset()){
  12. System.out.println("课程有"+crouse);
  13. }
  14. //从 List集合中循环取对象;
  15. System.out.println("*******************从 List集合中循环取对象********************");
  16. for (Crouse crouse : student.getClist()){
  17. System.out.println("课程有"+crouse.getName());
  18. }
  19. //从 Map集合中循环取键值对;
  20. System.out.println("*******************从 Map集合中循环取键值对********************");
  21. for (Entry<String, Crouse> entry : student.getCmap().entrySet()){
  22. System.out.println("课程有"+entry.getKey()+" "+entry.getValue().getName());
  23. }
  24. //从 Properties中循环取键值对 与map不同,Properties中的键值对都为字符串;
  25. System.out.println("*******************从Properties中循环取键值对********************");
  26. //方法1;
  27. Properties cpp = student.getCpp();
  28. Enumeration<Object> en = cpp.keys();
  29. while(en.hasMoreElements()){
  30. String key = (String) en.nextElement();
  31. System.out.println("课程有"+key+" "+cpp.getProperty(key));
  32. }
  33. //方法2;
  34. for (Entry<Object, Object> entry : cpp.entrySet()){
  35. System.out.println("课程有"+entry.getKey()+" "+entry.getValue());
  36. }





原文地址:https://www.cnblogs.com/Jxiaobai/p/6622121.html