????????????????--"?????":
??????????????????????????????????????飬?????????????????????????ж???????????????洢???????????.
??????????????
??????0?????????????????????????.???????????????????????????????????????????????????????????????????????????????????????.
????ArrayList??LinkedList
????ArrayList??LinkedList?????洢????????洢???????java?????е????.
????ArrayList????????????????????????ArrayList??????????????????????????????????????get??set?????????????????.?????????????????????????????.?????????????o?????????????????????????????????????.
???????????????????????????????????в??????????????????鯔????????????????????????????????????LinkedList????????????????????????????????????????????????????????С????????????????????.????????????????????get????????????????????????????в????????get????????????????????.
???????????????????????????????????????????????????????????arrayList??LinkedList.
???????ArrayList
??????????java??ArrayList?????????????????????ArrayList??????????????????????д???????????ArrayList??????.
????MyArrayList:
package com.wang.list;
import java.util.Iterator;
public class MyArrayList<T> implements Iterable<T> {
//??????????????
private static final int DEFAULT_CAPACITY=10;
//??????????
private int theSize;
//??????????????????List
private T [] theItems;
public MyArrayList(){
clear();
}
//???????????????????
public void clear(){
theSize=0;
ensureCapacity(DEFAULT_CAPACITY);
}
//????????int???С?????????????伯?????С
public void ensureCapacity(int newCapacity) {
if(newCapacity<theSize)
return;
T[] old=theItems;
theItems=(T[]) new Object[newCapacity];
for(int i=0;i<size();i++){
theItems[i]=old[i];
}
}
//?????????????????
public int size() {
return theSize;
}
//????????????????
public boolean isEmpty(){
return size()==0;
}
public void trimToSize(){
ensureCapacity(size());
}
//???????????μ?????
public T get(int index){
if(index<0||index>=size())
throw new ArrayIndexOutOfBoundsException("???????");
return theItems[index];
}
//?????????????????
public T set(int index??T value){
if(index<0||index>=size())
throw new ArrayIndexOutOfBoundsException("???????");
T old=theItems[index];
theItems[index]=value;
return old;
}
//??????????????????????????????β
public boolean add(T value){
add(size()??value);
return true;
}
//?????????????λ?ú?????
public void add(int index?? T value) {
//?????????????????????size();???????????飬????????????
if(theItems.length==size()){
ensureCapacity(size()*2+1);
}
//???β????????????????index?????index????????????????????λ
for(int i=theSize;i>index;i--){
theItems[i]=theItems[i-1];
}
theItems[index]=value;
//???????????????????????
theSize++;
}
//????????????????????
public T remove(int index){
T val=theItems[index];
for(int i=index;i<size()-1;i++){
theItems[i]=theItems[i+1];
}
theSize--;
return val;
}
//??д???????????????????????????????????static???????иù?????????????????????????
@Override
public Iterator<T> iterator() {
return new ArrayListIterator();
}
private class ArrayListIterator implements java.util.Iterator<T>{
private int current=0;
@Override
public boolean hasNext() {
return current<size();
}
@Override
public T next() {
return theItems[current++];
}
@Override
public void remove() {
MyArrayList.this.remove(--current);
}
}
}