ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

Collection

2021-11-19 12:00:52  阅读:121  来源: 互联网

标签:name System collection Collection Student println out


ollection体系集合

image-20211117222521504

  • 集合:该体系结构的根接口,代表一组对象,

  • List接口的特点:有序、有下标、元素可重复

  • Set接口的特点:无序、无下标、元素不能重复

     

Collection父接口

  • 特点:代表一组任意类型的对象,无序、无下标、不能重复

  • 方法:

    • boolean add(Object obj)//添加一个对象

    • boolean addAll(Collection c)//将一个集合中的所有对象添加到此集合中。

    • void clear()//清空此集合中的所有对象

    • boolean contains(Object o)//检查此集合中是否包含o对象

    • boolean equals (Object o)//比较此集合是否与指定对象相等

    • boolean isEmpty()//判断此集合是否为空

    • boolean remove(Object o)//在此集合中移除o对象

    • int size()//返回此集合中的元素个数

    • Object[] toArray()//将此集合转换成数组

package chapter01;
//需要导入一些包哦
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

public class Demo01 {
  public static void main(String[] args) {
      Collection collection= new ArrayList();
//         添加元素
          collection.add("A");
          collection.add("B");
          collection.add("C");
      System.out.println("元素个数"+collection.size());
      System.out.println(collection);
          //删除元素
      //collection.remove("B");
      System.out.println(collection);
      //collection.clear();//清理干净
          //遍历元素
      //3.1增强for
      for (Object object:collection){
          System.out.println(object);
      }
      //3.2使用迭代器,专门用来迭代集合 有hasNext,nest,remove三个
      Iterator iterator= collection.iterator();//迭代器
      System.out.println(iterator.hasNext());//true
      while (iterator.hasNext()){
          String s1=(String)iterator.next();
          //collection.remove(s1);
          iterator.remove();
          System.out.println(s1);
      }
      System.out.println(collection.size());
      //判断
      System.out.println(collection.contains("D"));//是否有C
      System.out.println(collection.isEmpty());//是否是空
  }
}

Collection 的使用,保存学生信息

public class Demo02 {
  public static void main(String[] args) {
      //先新建collrctipn对象
      Collection collection = new ArrayList();
      Student s1=new Student("阿娆",20);
      Student s2=new Student("麦麦",21);
      Student s3=new Student("娟娟",22);
      Student s4=new Student("小卢",24);
      //1.添加数据
      collection.add(s1);
      collection.add(s2);
      collection.add(s3);
      System.out.println(collection.size());
      //2.删除对象
//       collection.remove(s1);
//       System.out.println(collection.size());
//       collection.clear();//只是从集合里移除,但是并没有删除
      //3.遍历对象
      //3.1增强for
      for(Object object:collection){
          Student s=(Student)object;
          System.out.println(s.toString());
      }
      //3.2迭代器hasNext       next()     remove() ;迭代过程中不能使用collection的删除方法
      Iterator iterator= collection.iterator();
      while (iterator.hasNext()){
          Student s=(Student)iterator.next();
          System.out.println(s.toString());
      }
      //判断
      System.out.println(collection.contains(s1));
      System.out.println(collection.contains(s4));
  }
}
package chapter01;

public class Student {
  private String name;
  private int age;
  public Student(){

  }
  public Student(String name,int age){
      super();
      this.age=age;
      this.name=name;
  }

  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;
  }
  public String toString(){
      return "Student[name="+name+", age ="+age+"]";
  }
}

Student[name=麦麦, age =21] Student[name=娟娟, age =22] Student[name=阿娆, age =20] Student[name=麦麦, age =21] Student[name=娟娟, age =22] true false

 

 

标签:name,System,collection,Collection,Student,println,out
来源: https://www.cnblogs.com/ar000517/p/15576524.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有