一、前言
对于arraylist集合,其是List接口下的一种数据存储结构,底层由数组组成。本身特点为:查找快,增删慢(原因:因为底层是数组,而数组是需要一个连续的空间,每次的增删就相当于数组的移动和复制,从而效率大大降低。但是在查询上,效率极高。
二、核心
1. 在翻阅jdk源码之后,其大致可以总结为:
进行add方法添加之后,默认值首先为0,其次有一个判断,从而使默认值为10;
扩容机制为1.5倍的扩容,采用的是位运算(位运算在java中的效率高);
核心代码为
System.arraycopy (objects,0,temp,0,objects.length);
其作用为数组的复制,参数依次为:源对象,源对象起始位置,目标对象,目标对象起始位置,拷贝元素个数。
2. ArrayList非线程安全,如果多个线程同时对同一个arraylist更改数据的话,会导致数据不一致或者数据污染,如果出现线程不安全操作的时候,arraylist尽可能的抛出concurrentModifycationException防止数据异常。如果想保证线程安全,可以使用vector...
package com.liqiang.jdk;
import java.util.ArrayList;
import java.util.LinkedList;
/**
* 1.ArrayList的简单实现(手写)
* 2.包括以下方法:
* int size();
* MyArrayList();
* MyArrayList(int initialCapacity);
* boolean isEmpty();
* Object get(int index);
* boolean add(Object obj);
* void add(int index,Object obj)
* Object remove(int index)
* boolean remove(Object obj)
* Object set(int index,Object obj)
* void rangeCheck(int index)
* void ensureCapacity()
*/
public class MyArrayList<T> {
private Object[] elementData;
private int size;
/*
* return the size of array
* */
public int size(){
return size;
}
public MyArrayList(){
this(10);
}
/*
* we need to check the legality of parameter
* and the initial of array
* */
public MyArrayList(int initalCapacity){
if(initalCapacity < 0){
try{
throw new Exception("the size of array cant't < 0");
}catch(Exception e){
e.printStackTrace();
}
}
elementData = new Object[initalCapacity];
}
public boolean isEmpty(){
return size == 0;
}
/*
* First,the parameter should be checked in order to throw exception
* */
public Object get(int index){
rangeCheck(index);
return elementData[index];
}
/*
* First,we should ensure the size of array is more than the capacity of array
* if less,you should ready to extend the capacity of array
* */
public boolean add(Object obj) {
ensureCapacity();
elementData[size] = obj;
size++;
return true;
}
/*
* First,the legality of index need to checked
* Second,about the capacity of array
* Third,copy array to a new array
* */
public void add(int index,Object obj){
rangeCheck(index);
ensureCapacity();
System.arraycopy(elementData,index,elementData,index+1,size-index);
elementData[index] = obj;
}
/*
* First,check the legality of index
* Second,according to the index,we should delete the object
* Third,the whole of array should make a forward step in position
* */
public Object remove(int index){
rangeCheck(index);
int arrnums = size-index-1;
Object oldValue = elementData[index];
if(arrnums > 0)
System.arraycopy(elementData,index+1,elementData,index,arrnums);
elementData[--size] = null;
return oldValue;
}
/*
* First,delete the concreate object
* Second,we can get the index of concreate obj in array
* Thrid,delete the object by using remove(int index)
* */
public boolean remove(Object obj){
for(int i = 0;i < size;++i){
/*
* equals只是比较值,而==还会比较两个对象是否是同一个对象
* */
if(get(i).equals(obj)) {
remove(i);
break;
}
}
return true;
}
/*
* First,change the object in specified position
* Second,the legality of special index
* */
public Object set(int index,Object obj){
rangeCheck(index);
Object oldValue = elementData[index];
elementData[index] = obj;
return oldValue;
}
private void rangeCheck(int index){
/*
* 对下标的检查
*/
if(index<0||index>=size){
try {
throw new Exception();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
public void ensureCapacity(){
if(size == elementData.length){
Object[] newArray = new Object[size*2+1];
System.arraycopy(elementData,0,newArray,0,elementData.length);
elementData = newArray;
}
}
public static void main(String[] args){
MyArrayList list = new MyArrayList();
MyArrayList<Integer> list3;
ArrayList list1 = new ArrayList();
LinkedList<Integer> linked1;
ArrayList<Integer> list2 = new ArrayList<Integer>();
list.add("123");
list.add("哦豁");
list.add("wsad");
list.add("555");
System.out.println(list.remove(2));
}
}