小白想当程序员

导航

Collection集合

集合

Collection接口包括两个接口{List set}

  • List包括三个类(有序有下标可重复){ArrayList LinkedList Vector}

  • Set包括两个类(无序无下标不可重复){Hastset Sortedset}

Collection 接口的使用

  • 添加元素

  • 删除元素

  • 遍历元素

  • 判断

第一步创建集合:因为接口不能实例化,我们借用第一类的ArrayList

//创建集合
Collection collection = new ArrayList();//接口不能实例化

 

(1)add添加元素

//添加元素
collection.add("苹果");
collection.add("香蕉");
collection.add("西瓜");
System.out.println("元素个数:"+collection.size());//3个
System.out.println(collection);//[苹果, 香蕉, 西瓜]

 

(2)remove删除元素 clear清空元素

collection.remove("西瓜");
System.out.println(collection);//[苹果, 香蕉]
collection.clear();

 

(3)遍历元素:(2种方法)

增强FOR循环

for (Object object:collection) {
   System.out.println(object);
}

collection的方法

iterator()是集合中的一种方法,需要用Iterator创建;it.hasNext()返回一个boolearn值,如果有元素返回ture;其中it.next()作用是将it中的元素提取;it.remove()是将it中元素删除

Iterator it  =  collection.iterator();
while (it.hasNext()){
    Object object2 = it.next();
    System.out.println(object2);
    it.remove();}
System.out.println(collection.size());//0,因为删除了

 

(4)判断

//判断是否存在一个元素
System.out.println(collection.contains("西瓜"));//true

//判断是否为空
System.out.println(collection.isEmpty());//flase

 

给collection添加学生类

package com.Long.lessonTest;
//创建一个学生类
public class Student {
   private String name;
   private int age;
   //无参构造
   public Student() {
  }

   //有参构造
   public Student(String name, int age){
       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;
  }

   @Override//创建一个打印方法
   public String toString() {
       return "Student{" +
               "name='" + name + '\'' +
               ", age=" + age +
               '}';
  }
}
package com.Long.lessonTest;


import java.util.ArrayList;
import java.util.Collection;

public class main {
   public static void main(String[] args) {

       Collection collection = new ArrayList();
        Student s1 = new Student("张三",15);
        Student s2 = new Student("张三",15);
        Student s3 = new Student("张三",15);
        //给集合添加元素
        collection.add(s1);
        collection.add(s2);
        collection.add(s3);
       System.out.println(collection.size());//元素的个数
       System.out.println(collection.toString());//打印里面的元素


  }
}

 

List集合

  • 可以实现指定位置添加元素

    List list = new ArrayList();
    list.add("苹果");
    list.add(0,"华为");//指定位置添加元素
  • 输出元素可以用for循环

  • 可以采用get获取元素

    //for循环
    for (int i = 0; i < list.size(); i++) {
       System.out.println(list.get(i));//获取list中的元素
    }
  • 输出元素可以用增强for循环

    //增强for循环
    for (Object object : list) {
       System.out.println(object);
    }
  • 输出元素可以用迭代器

    Iterator it = list.iterator();
    while (it.hasNext()){
       System.out.println(it.next());
    }
  • 输出元素可以用ListIterator

    ListIterator it = list.listIterator();
    while (it.hasNext()){
       //从前往后输出
       System.out.println(it.nextIndex()+":"+it.next());
    }
ListIterator it = list.listIterator();
while (it.hasNext()){
   //从前往后输出
   System.out.println(it.previousIndex()+":"+it.previous());
}

 

posted on 2021-11-10 10:39  小白想当程序员  阅读(42)  评论(0)    收藏  举报