Collection接口常用操作【数据结构】

Collection 接口概念:

collection 即单列集合
用来存储管理一组对象 objects,这些对象一般被称为元素 elements,统一定义了一套单列集合的接口

Collection 常用操作:


1.创建 Collection对象:

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

public class TestCollection {
    public static void main(String[] args) {
        Collection<String> collection = new ArrayList<>();
    }
}

上述代码:

  1. 向上转型:实际 new 的对象是 ArrayList,但使用的是 Collection 类型的引用来保存该对象
  2. 泛型:写一个方法 / 类,让这个方法和类可以同时支持多种不同类型的数据
    泛型参数必须是引用类型

在这里插入图片描述
2.使用 size 方法:

即看集合里有多少个元素,此处则为:有多少个String 对象

Collection<String> collection = new ArrayList<>();

System.out.println(collection.size());

输出结果:0

此处只能为 size 不能是 length

在这里插入图片描述
size 和 length

数组元素个数:.length
此处 length 后不带 ( ),表示的是一个属性
.
字符串获取字符个数:.length( )
此处 length 后带 ( ),表示调用一个方法
.
集合获取元素个数:.size( )

3.使用 isEmpty 方法:

判断集合是否为空

Collection<String> collection = new ArrayList<>();

System.out.println(collection.isEmpty());

输出结果:true

4.使用 add 方法插入元素:

Collection<String> collection = new ArrayList<>();

collection.add("Hello");
collection.add("Bite!");

此处注意:add 的参数类型必须和泛型参数类型匹配

再次使用 size 和 isEmpty :

Collection<String> collection = new ArrayList<>();

collection.add("Hello");
collection.add("Bite!");

System.out.println(collection.size());
System.out.println(collection.isEmpty());

输出结果:2
         false

5.使用 toArray 把 collection 转换成数组:

Collection<String> collection = new ArrayList<>();

collection.add("Hello");
collection.add("Bite!");

Object[] array = collection.toArray();
System.out.println(Arrays.toString(array));

输出结果:

在这里插入图片描述
6.使用 for 循环 遍历集合中的元素:

Collection<String> collection = new ArrayList<>();

collection.add("Hello");
collection.add("Bite!");

for (String s : collection) {
    // s 就分别指向collection中的每一个元素
    System.out.println(s);
}

输出结果:

在这里插入图片描述
7.使用 contains 方法判定元素是否存在:

Collection<String> collection = new ArrayList<>();

collection.add("Hello");
collection.add("Bite!");

System.out.println(collection.contains("Bite!"));

输出结果:true

比较字符串的时候,是按照equals方式来判定的,所以比较的是对象的值,而不是

8.使用 remove 来删除元素:

Collection<String> collection = new ArrayList<>();

collection.add("Hello");
collection.add("Bite!");

collection.remove("Hello");

System.out.println("====删除Hello====");
Object[] array2 = collection.toArray();
System.out.println(Arrays.toString(array2));

输出结果:

在这里插入图片描述
9.使用 clear 方法 清空所有元素:

Collection<String> collection = new ArrayList<>();

collection.add("Hello");
collection.add("Bite!");

collection.clear();
System.out.println("====清空所有元素====");
System.out.println(collection.isEmpty());
System.out.println(collection.size());

输出结果:

在这里插入图片描述
以上常用操作总结:

方法说明
int size( )返回集合中的元素个数
boolean isEmpty( )判断集合是否没有任何元素,俗称空集合
boolean add(E e)将元素 e 放入集合中
Object[ ] toArray( )返回一个装有所有集合中元素的数组
boolean contains(Object o)判定元素o是否存在于集合中
boolean remove(Object o)如果元素 e 出现在集合中,删除其中一个
void clear( )清空集合中所有元素

在这里插入图片描述

评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

一朵花花

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值