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

x33g5p2x  于2021-09-19 转载在 其他  
字(2.7k)|赞(0)|评价(0)|浏览(527)

Collection 接口概念:

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

Collection 常用操作:


1.创建 Collection对象:

  1. import java.util.ArrayList;
  2. import java.util.Collection;
  3. public class TestCollection {
  4. public static void main(String[] args) {
  5. Collection<String> collection = new ArrayList<>();
  6. }
  7. }

上述代码:

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

2.使用 size 方法:

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

  1. Collection<String> collection = new ArrayList<>();
  2. System.out.println(collection.size());
  3. 输出结果:0

此处只能为 size 不能是 length

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

3.使用 isEmpty 方法:

判断集合是否为空

  1. Collection<String> collection = new ArrayList<>();
  2. System.out.println(collection.isEmpty());
  3. 输出结果:true

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

  1. Collection<String> collection = new ArrayList<>();
  2. collection.add("Hello");
  3. collection.add("Bite!");

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

再次使用 size 和 isEmpty :

  1. Collection<String> collection = new ArrayList<>();
  2. collection.add("Hello");
  3. collection.add("Bite!");
  4. System.out.println(collection.size());
  5. System.out.println(collection.isEmpty());
  6. 输出结果:2
  7. false

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

  1. Collection<String> collection = new ArrayList<>();
  2. collection.add("Hello");
  3. collection.add("Bite!");
  4. Object[] array = collection.toArray();
  5. System.out.println(Arrays.toString(array));

输出结果:

6.使用 for 循环 遍历集合中的元素:

  1. Collection<String> collection = new ArrayList<>();
  2. collection.add("Hello");
  3. collection.add("Bite!");
  4. for (String s : collection) {
  5. // s 就分别指向collection中的每一个元素
  6. System.out.println(s);
  7. }

输出结果:

7.使用 contains 方法判定元素是否存在:

  1. Collection<String> collection = new ArrayList<>();
  2. collection.add("Hello");
  3. collection.add("Bite!");
  4. System.out.println(collection.contains("Bite!"));
  5. 输出结果:true

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

8.使用 remove 来删除元素:

  1. Collection<String> collection = new ArrayList<>();
  2. collection.add("Hello");
  3. collection.add("Bite!");
  4. collection.remove("Hello");
  5. System.out.println("====删除Hello====");
  6. Object[] array2 = collection.toArray();
  7. System.out.println(Arrays.toString(array2));

输出结果:

9.使用 clear 方法 清空所有元素:

  1. Collection<String> collection = new ArrayList<>();
  2. collection.add("Hello");
  3. collection.add("Bite!");
  4. collection.clear();
  5. System.out.println("====清空所有元素====");
  6. System.out.println(collection.isEmpty());
  7. 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( )清空集合中所有元素

相关文章