当前位置:  首页>> 技术小册>> Java语言基础10-Java中的集合

示例:

  1. package com.github.collection1.demo5;
  2. /**
  3. * @author maxiaoke.com
  4. * @version 1.0
  5. */
  6. public class Person {
  7. private String name;
  8. private Integer age;
  9. public Person(String name, Integer age) {
  10. this.name = name;
  11. this.age = age;
  12. }
  13. @Override
  14. public String toString() {
  15. return "Person{" + "name='" + this.name + '\'' + ", age=" + this.age + '}';
  16. }
  17. }

示例:

  1. package com.github.collection1.demo5;
  2. import java.util.ArrayList;
  3. import java.util.Collection;
  4. import java.util.Iterator;
  5. /**
  6. * @author maxiaoke.com
  7. * @version 1.0
  8. */
  9. public class Test {
  10. public static void main(String[] args) {
  11. Collection<Person> collection = new ArrayList<>();
  12. collection.add(new Person("张三", 11));
  13. collection.add(new Person("李四", 59));
  14. collection.add(new Person("王五", 19));
  15. collection.add(new Person("赵六", 42));
  16. collection.add(new Person("田七", 8));
  17. collection.add(new Person("王八", 2));
  18. for (Iterator<Person> iterator = collection.iterator(); iterator.hasNext();) {
  19. Person next = iterator.next();
  20. System.out.println(next);
  21. }
  22. }
  23. }

该分类下的相关小册推荐: