一、基本概念
数组:同一数据类型,创建之初必须给予长度,一旦长度确定,就不能改变。
实际运行中,数据的数量是不断变化的
需要一些能够自己调整长度的一些容器
1.数据长度可变
2.数据的保存方式不同,数组(查询最快),链表(查询慢,删除添加元素快),树
MyArray
object[] objs;
MyArray(){
objs = new Object[capcity];
}
add(object obj){
//判断数组中是否可以放得下,如果放不下了,就对数组进行扩容(创建一个新数组)。
objs[i] = obj
}
remove(int index){
}
单列集合
数组就是单列的,存储一个值进去
Collection 接口 定义了单列集合中共有的功能
List:可以存储重复元素,就可以在Collection的基础上添加自己特有的方法(通过所引操作的方法)
ArrayList:底层实现是数组
LinkedList底层实现是链表结构
Vector:底层实现也是数组,是线程安全的
Set:不可以存储重复元素
HashSet:底层使用哈希表 无序的
TreeSet:底层使用树结构 可以排序的.
text(Collection collection){
}
双列集合:键;值
Map
HashMap
HashTable
TreeMap
二、ArrayList
java中的集合类中默认可以存储任意数据类型
java中的集合提供泛型机制,在定义时,最好为集合类提供一个明确的类型
ArrayList底层是一个数组实现,提供一系列对数组操作的方法
ArrayList();在第一次添加元素时创建底层数组,容量默认是10
ArrayList(10);在创建ArrayList对象时,就会创建底层数组,容量是指定容量
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);//数组复制,创建一个新数组
}
public static void main(String[] args) {
ArrayList<Integer> arrayList = new ArrayList();
// arrayList.add("a");
arrayList.add(10);//向末尾添加元素,数组添加满自动扩容,扩容为原来的1.5倍
arrayList.add(0,100);//向指定的索引处添加元素
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
arrayList.add(10);
// arrayList.clear();清空集合
System.out.println(arrayList.contains(100));//是否包含指定元素
/**
E elementData(int index) {
return (E) elementData[index];
}
*/
System.out.println(arrayList.get(3));//查找索引为3处的元素
System.out.println(arrayList.indexOf(20));//查找20首次出现的位置
System.out.println(arrayList.isEmpty());//判断是否为空
System.out.println(arrayList.remove(10));//删除索引为10的元素
System.out.println(arrayList.remove((Object)10));//删除内容为10的元素
System.out.println(arrayList.set(2,666));//把制定索引处元素替换
System.out.println(arrayList.size());//实际存储元素的个数
Object obj = arrayList.toArray();
// System.out.println(arrayList.toString();
System.out.println(arrayList);
}
三、LinkedList
LinkedList 底层实现是双向链表,一个一个的Node对象(data next(指针) prev(指针))
Node<E> node(int index) {
// assert isElementIndex(index);
if (index < (size >> 1)) {
Node<E> x = first;
for (int i = 0; i < index; i++)
x = x.next;
return x;
} else {
Node<E> x = last;
for (int i = size - 1; i > index; i--)
x = x.prev;
return x;
}
}
ArrayList和LinkList中方法名相同,但是地层实现不同
LinkList中,提供一些用于像队列,栈结构的操作方法
public static void main(String[] args) {
LinkedList<String> linkedList = new LinkedList<>();
linkedList.add("a");
linkedList.add("b");
linkedList.add("c");
linkedList.add("d");
linkedList.add(1,"2");
linkedList.addFirst("s");
linkedList.addLast("e");
System.out.println(linkedList.pollFirst());
System.out.println(linkedList.remove());
}
四、Vector
Vector底层实现是数组,但是是线程安全的
public synchronized boolean add(E e) {
modCount++;
ensureCapacityHelper(elementCount + 1);
elementData[elementCount++] = e;
return true;
}
public static void main(String[] args) {
Vector<String> vector = new Vector<>();
vector.add("A");
}