Java study notes (19)-collection

Collection

可以存储多个数据,而且长度可以改变的容器。
	Collection<E>是集合的顶级接口。
	<E>---泛型
		用于指定我们集合中元素的类型
		由于泛型的指定集合中元素类型只能是引用数据类型
		集合只能存储对象
//创建集合
  Collection collection = new ArrayList();
  //添加元素
  collection.add("苹果");
  collection.add("西瓜");
  collection.add("榴莲");
  System.out.println("元素个数"+collection.size());
  System.out.println(collection);
  //删除元素
  collection.remove("榴莲");
  System.out.println(collection);
  //清空元素
  collection.clear();
  System.out.println("元素个数"+collection.size());
  //
  collection.add("苹果");
  collection.add("西瓜");
  collection.add("榴莲");
  //遍历元素
   //方式一 增强for循环
   for (Object object : collection) {
    
    
    System.out.println(object);
   }
   //方式二 迭代器 专门用来遍历集合
   Iterator it = collection.iterator();
   //hasNext 有没有下一个元素
   //next 获取下一个元素
   //remove 删除当前元素
   while(it.hasNext()) {
    
    
   String object = (String)it.next();
    System.out.println(object);
   }
  //判断元素
   System.out.println(collection.contains("西瓜"));
   System.out.println(collection.contains("枣子"));
   System.out.println(collection.isEmpty());
//对象存集合
    Collection collection = new ArrayList();
    StudentDemo stu1 = new StudentDemo("张三",16);
    StudentDemo stu2 = new StudentDemo("张四",15);
    StudentDemo stu3 = new StudentDemo("张五",17);
   //添加对象
    collection.add(stu1);
    collection.add(stu2);
    collection.add(stu3);
    System.out.println(collection.size());
    System.out.println(collection.toString());
   //删除对象
    collection.remove(stu1);
    System.out.println(collection.size());
public class StudentDemo {
    
    
 private String name;
 private int age;
 public StudentDemo(){
    
    
  
 }
 public StudentDemo(String name, int age) {
    
    
  super();
  this.name = name;
  this.age = age;
 }
 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 "StudentDemo [name=" + name + ", age=" + age + "]";
 }
 //
}
//先创建集合
  List list = new ArrayList();
  //添加元素
  list.add("苹果");
  list.add("小米");
  //按下标添加元素
  list.add(0,"华为");
  System.out.println(list.size());
  System.out.println(list.toString());
  //删除元素
  list.remove(0);
  System.out.println(list.size());
  System.out.println(list.toString());
  //遍历
   // 使用for进行遍历
   for (int i = 0; i < list.size(); i++) {
    
    
    System.out.println(list.get(i));
   }
   //增强for
   for (Object object:list) {
    
    
    System.out.println(object);
   }
   //迭代器
   Iterator it = list.iterator();
   while(it.hasNext()) {
    
    
    System.out.println(it.next());
   }
   System.out.println("--------列表迭代器---------");
   //列表迭代器
   //功能更强大 可以向前 向后 可以删除 添加 修改元素
   System.out.println("--------列表迭代器-从前往后--------");
   ListIterator it2 = list.listIterator();
   while(it2.hasNext()) {
    
    
    System.out.print(it2.nextIndex());
    System.out.println(it2.next());
    //
   }
   //
   System.out.println("--------列表迭代器-从后往前--------");
   while(it2.hasPrevious()) {
    
    
    System.out.print(it2.previousIndex());
    System.out.println(it2.previous());
    //
   }
  //获取位置
   System.out.println(list.indexOf("小米"));
   //返回子集合 左包含 右不包含
   System.out.println(list.subList(0, 1));

Guess you like

Origin blog.csdn.net/qq_42351519/article/details/111406606