查看ArrayList的方法们(挑选几个)
源码
package dataStructure;
import java.lang.reflect.Constructor;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.regex.Pattern;
/**
* Created by donal on 2017/2/12.
*/
/**
* 输出方法体
* Pattern p = Pattern.compile("\\w+\\.");
try{
Class<?> c = Class.forName("java.util.ArrayList");
Method[] methods = c.getMethods();
Constructor[] constructors = c.getConstructors();
for(Method method : methods){
System.out.println(
p.matcher(method.toString()).replaceAll("") //去掉 包名.类名.
);
}
System.out.println("----------------------------------------------");
for (Constructor constructor : constructors){
System.out.println(p.matcher(constructor.toString()).replaceAll(""));
}
}catch (ClassNotFoundException e){
System.out.println("No such class: " + e);
}
*/
public class MyArrayList
private static final int DEFAULT_CAPACITY = 10;
private static final Object[] EMPTY_ELEMENTDATA = {};
private static final Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = {};
transient Object[] elementData;
private int size;
public MyArrayList() {
this.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;
}
public MyArrayList(int initialCapacity){
if (initialCapacity > 0) {
this.elementData = new Object[initialCapacity];
} else if (initialCapacity == 0) {
this.elementData = EMPTY_ELEMENTDATA;
} else {
throw new IllegalArgumentException("Illegal Capacity: "+
initialCapacity);
}
}
private void makeSure(int capacitySize){
if (elementData == DEFAULTCAPACITY_EMPTY_ELEMENTDATA ){
elementData = new Object[DEFAULT_CAPACITY];
}else if (capacitySize > DEFAULT_CAPACITY){
throw new RuntimeException();
}
}
public boolean add(Object o){
makeSure(size + 1);
elementData[size++] = o;
return true;
}
public void add(int index, Object o){
if (index >= DEFAULT_CAPACITY)
throw new RuntimeException();
makeSure(size + 1);
System.arraycopy(elementData, index, elementData, index + 1,
size - index);
elementData[index] = o;
size++;
}
public boolean remove(Object o){
int index = -1;
for (int i = 0; i < size(); ++i){
if (elementData[i] == o){
index = i;
break;
}
}
if (index == -1)
return false;
else
remove(index);
return true;
}
public Object remove(int index){
if (index >= DEFAULT_CAPACITY)
throw new RuntimeException();
Object oldElem = elementData[index];
System.arraycopy(elementData, index, elementData, index,
size - index);
elementData[size--] = null;
return oldElem;
}
public Object get(int n){
if (n > size())
throw new RuntimeException();
return elementData[n];
}
public int indexOf(Object o){
for (int i = 0; i < size(); ++i){
if (elementData[i] == o)
return i;
}
return -1;
}
public void clear(){
for (int i = 0 ; i < size(); ++i)
elementData[i] = null;
}
public boolean contains(Object o){
for (int i = 0; i < size(); ++i){
if (elementData[i] == o)
return true;
}
return false;
}
public boolean isEmpty(){
if (size() == 0)
return true;
return false;
}
public int size(){
return size;
}
public String toString(){
StringBuilder sb = new StringBuilder();
for (int i = 0 ; i < size(); ++i){
sb.append("index : " + i + ", element : " + elementData[i] + "\n");
}
return sb.toString();
}
public static void main(String[] args) {
MyArrayList myArrayList = new MyArrayList(5);
myArrayList.add("123");
myArrayList.add("456");
myArrayList.add("789");
System.out.println(myArrayList.get(1));
System.out.println(myArrayList.size());
myArrayList.remove(2);
System.out.println(myArrayList);
}
}