JAVA集合框架 1 :Collection体系集合

集合概述:

  • 概念:对象的容器,定义了对多个对象进项操作的的常用方法。可实现数组的功能。

  • 和数组的区别:

    • 数组长度固定,集合长度不固定。

    • 数组可以存储基本类型和引用类型,集合只能存储引用类型。

  • 位置: java.util.*;

一、Collection体系集合

在这里插入图片描述

Collection父接口

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

    • Collection父接口有list和set两个子接口,list是 有序 、 有下标、可重复的,但set 无序、无下标,不能重复。
    • 所以整体而言,我们将Collection视为无序、无下标、不能重复;但实际上,在api当中,认为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() //姜此集合转换成数组。
  • Collection接口方法的使用:

(一)Collection集合保存 string字符串

/**
 * Collection接口的使用(一)
 * 1.添加元素
 * 2.删除元素
 * 3.遍历元素
 * 4.判断
 */
public class Demo1{
    
    
    pubic static void main(String[] args){
    
    
        //创建集合
        Collection collection=new ArrayList();        
//      * 1.添加元素
        Collection.add("苹果");
        Collection.add("西瓜");
        Collection.add("榴莲");
        System.out.println("元素个数:"+collection.size()); // 3
        System.out.println(collection); // 【苹果,西瓜,榴莲】

//      * 2.删除元素
        collection.remove("榴莲");
        System.out.println("删除之后:"+collection.size()); // 2
        collection.clear(); 
         System.out.println("清空之后:"+collection.size()); // 0

//      * 3.遍历元素
        //3.1 使用增强for (foreach 回车)
        for(Object object : collection){
    
    
            System.out.println(object); // 苹果  西瓜  榴莲
        }
        
        //3.2 使用迭代器(迭代器:专门用来遍历集合的一种方式)
        //hasnext(); 判断是否有下一个元素
        //next(); 获取下一个元素
        //remove(); 删除当前元素(必须使用迭代器的删除方法,不能使用collection.remove() ,否则会引发错误)
        Iterator iterator=collection.Itertor();
        while(iterator.hasnext()){
    
    
            String s=(String)iterator.next();
            System.out.println(s); // 苹果  西瓜  榴莲
            //删除操作
            //collection.remove(s);引发错误:并发修改异常 java.util.ConcurrentModificationException
            //iterator.remove();应使用迭代器的删除方法
        }


//      * 4.判断
        System.out.println(collection.contains("西瓜")); //true
        System.out.println(collection.isEmpty()); //false
        }
    }
}

(二)Collection集合保存 学生信息(学生类)

/**
 * Collection接口的使用(二)
 * 1.添加元素
 * 2.删除元素
 * 3.遍历元素
 * 4.判断
 */
public class Demo2 {
    
    
	public static void main(String[] args) {
    
    
		//新建Collection对象
		Collection collection=new ArrayList();
		//实例化,创建学生对象
		Student s1=new Student("张三",18);
		Student s2=new Student("李四", 20);
		Student s3=new Student("王五", 19);
		//1.添加数据
		collection.add(s1);
		collection.add(s2);
		collection.add(s3);
		//collection.add(s3);可重复添加相同对象
		System.out.println("元素个数:"+collection.size()); // 3
		System.out.println(collection.toString());
		
		//2.删除数据
		collection.remove(s1);
		System.out.println("删除之后:"+collection.size()); // 2
		
		//3.遍历数据
		//3.1 增强for
		for(Object object:collection) {
    
    
			Student student=(Student) object;
			System.out.println(student.toString());
		}
		//3.2迭代器
		//迭代过程中不能使用collection的删除方法
		Iterator iterator=collection.iterator();
		while (iterator.hasNext()) {
    
    
			Student student=(Student) iterator.next();
			System.out.println(student.toString());
		}
		
		//4.判断和上一块代码类似。
	}
}

Guess you like

Origin blog.csdn.net/lixingecho/article/details/118483896