Collection接口
发布日期:2021-04-30 21:06:11 浏览次数:96 分类:精选文章

本文共 3716 字,大约阅读时间需要 12 分钟。

Java ????

?????

??????????????????????????????

  • ?????????????
  • ??????????????
  • ???????????????

???Java????????????????????????????????

???????

??????????????????

  • ?????boolean add(Object obj)
    ???????????
  • ???????boolean addAll(Collection c)
    ???????????????????
  • ?????void clear()
    ??????????
  • ???????boolean contains(Object o)
    ?????????????
  • ?????boolean equals(Object o)
    ??????????????
  • ????boolean isEmpty()
    ????????
  • ?????boolean remove(Object o)
    ??????????
  • ???????int size()
    ??????????
  • ??????Object[] toArray()
    ????????

Java???????

???????

// ????
Collection collection = new ArrayList();
// ????
collection.add(new Student("??", 20));
collection.add(new Student("??", 21));
collection.add(new Student("??", 22));
// ??????
System.out.println("?????" + collection.size()); // ???3

???????

???????????????

  • ??for???
  • // ????????
    for (Object obj : collection) {
    Student stu = (Student) obj;
    System.out.println(stu.toString());
    }
    1. ????
    2. // ?????
      Iterator it = collection.iterator();
      // ????
      while (it.hasNext()) {
      Student s = (Student) it.next();
      System.out.println(s.toString());
      }

      ??????????

      // ??????????
      System.out.println(collection.contains(new Student("??", 21))); // ???true
      System.out.println(collection.isEmpty()); // ???false

      ???????

      // ??????
      collection.remove(new Student("??", 21));
      System.out.println("?????" + collection.size()); // ???1

      ??????????

      ?????????????????????????????????

      ???Student?

      public class Student {
      private String name;
      private int age;
      public Student(String name, int age) {
      super();
      this.name = name;
      this.age = age;
      }
      public Student() {
      }
      public String getName() {
      return name;
      }
      public void setName(String name) {
      this.name = name;
      }
      public int getAge() {
      return age;
      }
      public void setAge(int age) {
      this.age = age;
      }
      @Override
      public String toString() {
      return "Student [name=" + name + ", age=" + age + "]";
      }
      @Override
      public boolean equals(Object obj) {
      // ??????????
      if (this == obj) {
      return true;
      }
      // ????????
      if (obj == null) {
      return false;
      }
      // ??????
      if (obj instanceof Student) {
      Student s = (Student) obj;
      // ????
      return this.name.equals(s.getName()) && this.age == s.getAge();
      }
      return false;
      }
      }

      ???????

      public class Test {
      public static void main(String[] args) {
      // ????
      Collection collection = new ArrayList();
      // ??????
      Student s1 = new Student("??", 20);
      Student s2 = new Student("??", 21);
      Student s3 = new Student("??", 22);
      collection.add(s1);
      collection.add(s2);
      collection.add(s3);
      System.out.println("?????" + collection.size()); // ???3
      // ????????
      for (Object obj : collection) {
      Student stu = (Student) obj;
      System.out.println(stu.toString());
      }
      // ???????
      Iterator it = collection.iterator();
      while (it.hasNext()) {
      Student s = (Student) it.next();
      System.out.println(s.toString());
      }
      // ????????????
      System.out.println(collection.contains(s1)); // ???true
      System.out.println(collection.contains(new Student("??", 21))); // ???true
      // ????????
      System.out.println(collection.isEmpty()); // ???false
      // ????
      collection.remove(s1);
      System.out.println("?????" + collection.size()); // ???2
      collection.remove(new Student("??", 21));
      System.out.println("?????" + collection.size()); // ???1
      }
      }
    上一篇:centos7完全离线安装mysql5.7
    下一篇:插件化框架解读之android系统服务实现原理,算法太TM重要了

    发表评论

    最新留言

    做的很好,不错不错
    [***.243.131.199]2026年06月10日 04时19分22秒