数组与集合

一、集合与数组存储数据的概述

集合:数组都是对数据进行存储操作的结构,简称 Java 容器

说明:此时的存储,主要指的是内存层面的存储,不涉及到持久化的储存(.text,.jpg,avi,数据库中)

二、数组存储的特点

一旦初始化以后,其长度就确定了。

数组一旦定义好,其元素的类型也就搞定了。我们也就只能操作指定类型的数据了。

比如:String[] arr; int[] arr1; Object[] arr2;

三、数组存储的弊端

  1. 一旦初始化以后,其长度就不可修改。
  2. 数组中提供的方法非常有限,对于添加、插入数据等操作,非常不便,同时效率不高。
  3. 获取数组中实际元素的个数的需求,数组没有规定的属性方法可用
  4. 数组存储的特点:有序、可重复性、对于无序、不可重复的需求,不能满足。

四、集合存储的优点

​ 解决数组存储数据方面的弊端。

Collection 接口

一、单列集合框架结构

  • Collection接口:单列集合,用来存储一个一个的对象
    • list接口:存储有序的、可重复的数据。(动态数组)
      • ArrayList、LinkedList、Vector
    • set接口:存储无序的、不可重复的数据 (集合)
      • HashSet、LinkedHashSet、TreeSet

对应图示

image-20200720163349709

二、Collection 接口常用方法

方法描述
add(Object obj)将元素 obj 添加到集合 coll 中
addAll(Collection coll)将 coll1 集合中的元素添加到当前的集合中
isEmpty()判断当前集合是否为空
clear()清空集合元素
contains(Object obj)判断当前集合中是否包含 obj
containsAll(Collection coll)判断形参 coll1 中的所有元素是否都存在于当前集合中。
remove(Object obj)当前集合中移除 obj 元素
removeAll(Collection coll)从当前集合中移除 coll1 中所有的元素
retainsAll(Collection coll)获取当前集合和 coll1 集合的交集,并返回给当前集合
equals(Object obj)相同返回 true,需要当前集合和形参集合的元素都相同。
hasCode()返回当前对象的哈希值
toArray()返回一个包含此集合中所有元素的数组。
iterator()迭代器遍历
size()获取添加的元素的个数

三、Collection 集合与数组的转换

1
2
3
4
5
//集合 ---> 数组:toArray()
Object[] arr =coll.toArray();
for(int i =0; i < arr.length; i++){
System.out.println(arr[i]);
}
1
2
3
4
5
6
7
8
9
//拓展:数组 --> 集合:调用Arrays类的静态方法asList(T ... t)
List<String> list = Arrays.asList(new String[] {"AA","BB","CC"});
System.out.println(lis.size()); // 3

List arr1 = Arrays.asList(new int[]{123,456});
System.oout.println(arr1.size());//1

List arr2 = Arrays.asList(new Integer[]{123,456});
System.out.println(arr2.size());//2

四 、使用 Collection 集合存储对象,要求所属对象的类吗,满足

​ 向 collection 接口的实现类的对象中添加数据 obj,要求 obj 所在类要重写 equals();

Iteratorj 接口与 foreach 循环

一、遍历 Collection 的两种方式

  1. 使用迭代器 iterator
  2. foreach 循环(或增强 for 循环)

二、说明

Iterator 对象成为迭代器(设计模式的一种),主要用于遍历 collection 集合中的元素。

GOF 给迭代器模式定义为:提供一个方法访问一个容器(container)对象中各个元素,而又不需要暴露该对象的内部细节。迭代器模式,就是为容器而生。

作用:遍历集合 Collection 元素

获取示例: coll.iterator()返回一个迭代器实例

遍历的代码实现:

1
2
3
4
5
6
Iterator iterator = coll.iterator();
// hasNext():判断是否还有下一个元素
while(iterator.hasNext()){
// next();①指针下移 ②将下移的以后的集合位置上的元素返回
System.out.prinln(iterator.next());
}

图示:

image-20200720181109215

remove()使用说明

  1. 测试 Iterator 中的 remove()
  2. 如果还未调用 next()或在上一次调用 next()方法之后已经调用 remove 方法,在调用 remove 报 IllegalStateException。
  3. 内部定义了 remove(),可以在遍历的时候,删除集合中的元素。此方法不同于集合调用 remove()

代码示例:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
@Test
public void test3(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);

//删除集合中"Tom"
Iterator iterator = coll.iterator();
while (iterator.hasNext()){
// iterator.remove();
Object obj = iterator.next();
if("Tom".equals(obj)){
iterator.remove();
// iterator.remove();
}

}
//遍历集合
iterator = coll.iterator();
while (iterator.hasNext()){
System.out.println(iterator.next());
}
}

三、JDK5.0 新特性 – 增强 for 循环:(forech 循环)

  1. 遍历集合举例
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@Test
public void test1(){
Collection coll = new ArrayList();
coll.add(123);
coll.add(456);
coll.add(new Person("Jerry",20));
coll.add(new String("Tom"));
coll.add(false);

//for(集合元素的类型 局部变量 : 集合对象)

for(Object obj : coll){
System.out.println(obj);
}
}

说明:

​ 内部仍调用了迭代器。

  1. 遍历数组举例:
1
2
3
4
5
6
7
8
@Test
public void test2(){
int[] arr = new int[]{1,2,3,4,5,6};
//for(数组元素的类型 局部变量 : 数组对象)
for(int i : arr){
System.out.println(i);
}
}