---
title: "8_（JAVA基础300集最全教程 学习）Collectioin各容器实现方式"
author: "up-up-programmer"
date: "2014-05-15T23:01:38Z"
tags: []
categories: ["编程语言"]
slug: "8-java-300-collectioin"
draft: false
lastmod: "2014-05-16T00:12:05Z"
id: 2067124
url: "https://up-up-programmer.iteye.com/blog/2067124"
---

1.ArrayList :
实现方式（非可序化Object数组）：private transient Object elementData;
扩容方式：历史值的3倍与2取商再加1（(oldCapacity * 3)/2 + 1）；

```java
public void ensureCapacity(int minCapacity) {
	modCount++;
	int oldCapacity = elementData.length;
	if (minCapacity > oldCapacity) {
	    Object oldData[] = elementData;
	    int newCapacity = (oldCapacity * 3)/2 + 1;
    	    if (newCapacity < minCapacity)
		newCapacity = minCapacity;
            // minCapacity is usually close to size, so this is a win:
            elementData = Arrays.copyOf(elementData, newCapacity);
	}
    }
```


注意到代码中有这么一处：elementData = null;后面给出的注释是： Let gc do its work(让GC回收)，这告诉我们在代码开发中也可以在对于不再使用的空间，将引用置为Null,让gc更好的回收空间。


```java

  public void clear() {
	modCount++;

	// Let gc do its work
	for (int i = 0; i < size; i++)
	    elementData[i] = null;

	size = 0;
    }

```

2.