集合
提示
数组,集合都是对多个数据进行存储操作的,简称为容器。
这里的存储指的是内存层面的存储,而不是持久化存储
数组:特点
一旦指定了参数,那么长度就被确定了,就不可以更改
int[] arr = new int[6]; // 长度就直接确定了
1对于数组来说,它一旦声明了类型以后,数组中只能存放该类型的数据。
数组:缺点
- 不能随意修改长度
- 删除一个元素、增加一个元素效率很低
- 数组中实际元素的数量是没有办法获取的,没有提供对应的方法或者属性来获取
- 数组存储:有序、可重复;对于无序的,不可重复的就不能满足要求
正因为上面的缺点,引入了一个新的存储数据的结构 -> 集合
# Collection
常用方法:
- 增加:
add(E e)、addAll(Collection<? extends E> c )
- 删除:
clear()、remove(Object o)
- 查看:
iterator()、size()
- 判断:
contains(Object o)、equals(Object o)、isEmpty()
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Test01 {
public static void main(String[] args) {
// Collection 接口
// 创建对象 利用实现类创建对象
Collection col = new ArrayList();
// 调用方法
// 集合有一个特点,只能存放引用数据类型的,不能是基本数据类型
// 基本数据类型自动装箱,对应包装类:Integer
col.add(19);
col.add(18);
System.out.println(col); // 默认调用 toString()
List<Integer> list = Arrays.asList(new Integer[]{11, 21, 23, 34});
// 将另一个集合添加入col中
col.addAll(list);
System.out.println(col);
// 清空
// col.clear();
System.out.println(col);
// 集合中元素个数
System.out.println(col.size());
// 集合是否为空
System.out.println(col.isEmpty());
boolean isRemove = col.remove(19);
System.out.println(isRemove);
}
}
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
简写方式:
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
public class Test01 {
public static void main(String[] args) {
// Collection 接口
// 创建对象 利用实现类创建对象
Collection<Integer> col = new ArrayList<>();
// 调用方法
// 集合有一个特点,只能存放引用数据类型的,不能是基本数据类型
// 基本数据类型自动装箱,对应包装类:Integer
col.add(19);
col.add(18);
System.out.println(col); // 默认调用 toString()
List<Integer> list = Arrays.asList(11, 21, 23, 34);
// 将另一个集合添加入col中
col.addAll(list);
System.out.println(col);
// 清空
// col.clear();
System.out.println(col);
// 集合中元素个数
System.out.println(col.size());
// 集合是否为空
System.out.println(col.isEmpty());
boolean isRemove = col.remove(19);
System.out.println(isRemove);
}
}
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# 对集合遍历
package com.collect;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;
public class Test02 {
public static void main(String[] args) {
Collection<Integer> col = new ArrayList<>();
col.add(18);
col.add(19);
col.add(12);
col.add(111);
// 对集合遍历
// 普通for循环
for (int i = 0; i < col.size(); i++) {
// 没有根据索引获取元素的方法
}
// 增强 for 循环
for (Integer integer : col) {
System.out.println(integer);
}
// 方式3:迭代器
Iterator<Integer> integerIterator = col.iterator();
while (integerIterator.hasNext()) {
System.out.println(integerIterator.next());
}
}
}
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
27
28
29
30
31
32
33
34
35
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
27
28
29
30
31
32
33
34
35
# List 接口
List
接口继承了Collection
,所以上述有的方法基本都有
- 增加:
add(int index, E element)
- 删除:
remove(int index)
- 修改:
set(int index, E element)
- 查看:
get(int index)
案例
package com.collect;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
public class Test03 {
public static void main(String[] args) {
List<Integer> list = new ArrayList<>();
list.add(1);
list.add(2);
list.add(3);
list.add(4);
list.add(5);
list.add(6);
System.out.println(list);
// 在下标为3的位置上添加一个 66
list.add(3, 66);
System.out.println(list);
// 将下标为3 的位置的值改成 77
list.set(3, 77);
System.out.println(list);
// 在集合中存储的是 Integer类型的时候,调用remove的时候: remove(int index)
list.remove(5);
// 获取下标为0的值
System.out.println(list.get(0));
// List 集合遍历
// 普通for循环
for (int i = 0; i < list.size(); i++) {
System.out.println(list.get(i));
}
// 增强for循环
for (Integer integer : list) {
System.out.println(integer);
}
// 迭代器遍历
Iterator<Integer> integerIterator = list.iterator();
while (integerIterator.hasNext()) {
System.out.println(integerIterator.next());
}
}
}
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# List 的 ArrayList 实现类
JDK1.8 底层依旧是
Object
类型的数组,size
:数组中有效长度transient Object[] elementData; private int size;
1
2
3ArrayList al = new ArrayList();
调用空构造器public ArrayList() { this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA; } private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
1
2
3
4
5在 JDK1.8 中在调用空构造器的时候,底层
elementData
的数组的初始化为一个空的数组{}
。add()
方法调用了
add()
第一次添加元素的时候进行确定数组的长度/** * Appends the specified element to the end of this list. * * @param e element to be appended to this list * @return <tt>true</tt> (as specified by {@link Collection#add}) */ public boolean add(E e) { ensureCapacityInternal(size + 1); // Increments modCount!! elementData[size++] = e; return true; } private void ensureCapacityInternal(int minCapacity) { ensureExplicitCapacity(calculateCapacity(elementData, minCapacity)); } private void ensureExplicitCapacity(int minCapacity) { modCount++; // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } // 扩容方法 /** * Increases the capacity to ensure that it can hold at least the * number of elements specified by the minimum capacity argument. * * @param minCapacity the desired minimum capacity */ private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + (oldCapacity >> 1); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); // minCapacity is usually close to size, so this is a win: elementData = Arrays.copyOf(elementData, newCapacity); }
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
提示
底层数组,在调用构造器的时候,底层数组为{}
,在调用add()
方法以后底层数组才重新赋值为新数组,新数组的长度为 10(=> 节省了内存),在add()
后才创建长度为 10 的数组。
底层扩容长度为原数组的 1.5 倍
# List 的 Vector 实现类
Vector v = new Vector();
1
底层 Object 数组,int 类型的属性表述数组的有效长度
/** * The array buffer into which the components of the vector are * stored. The capacity of the vector is the length of this array buffer, * and is at least large enough to contain all the vector's elements. * * <p>Any array elements following the last element in the Vector are null. * * @serial */ protected Object[] elementData; /** * The number of valid components in this {@code Vector} object. * Components {@code elementData[0]} through * {@code elementData[elementCount-1]} are the actual items. * * @serial */ protected int elementCount;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19调用空构造器的时候
public Vector(int initialCapacity, int capacityIncrement) { super(); if (initialCapacity < 0) throw new IllegalArgumentException("Illegal Capacity: "+ initialCapacity); this.elementData = new Object[initialCapacity]; this.capacityIncrement = capacityIncrement; } /** * Constructs an empty vector so that its internal data array * has size {@code 10} and its standard capacity increment is * zero. */ public Vector() { this(10); // 底层数组长度为10 }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17add()
方法public synchronized boolean add(E e) { modCount++; ensureCapacityHelper(elementCount + 1); elementData[elementCount++] = e; return true; } private void ensureCapacityHelper(int minCapacity) { // overflow-conscious code if (minCapacity - elementData.length > 0) grow(minCapacity); } private void grow(int minCapacity) { // overflow-conscious code int oldCapacity = elementData.length; int newCapacity = oldCapacity + ((capacityIncrement > 0) ? capacityIncrement : oldCapacity); if (newCapacity - minCapacity < 0) newCapacity = minCapacity; if (newCapacity - MAX_ARRAY_SIZE > 0) newCapacity = hugeCapacity(minCapacity); elementData = Arrays.copyOf(elementData, newCapacity); }
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24底层数组扩容长度为 2 倍
synchronized
代表线程安全,伴随着效率就低,因为效率低,Vector
现在被淘汰了。
# Collection 系列集合总结
底层都是数组
数组优点:查询效率高
数组缺点:删除,增加元素效率低
数组特点:数据可重复
编辑 (opens new window)
上次更新: 2021/11/16, 21:40:50