ArrayList 內部用數組存放數據 數組的缺點: 長度不可變; 訪問方式單一,只能用下表索引; 效率表現:訪問任何位置 效率同樣,增刪數據效率下降 內部數組默認的初始容量10; 放滿以後,容量1.5倍增加;減小數組建立提升效率;
package 集合.list.ArrayList; import java.util.Arrays; import java.util.Date; public class MyArryList { //定義一個空的數組 private final static Object[] myArray = {}; //定義一個默認長度10 private final static int default_length = 10; Object[] elementData; //myArryList長度 private int size = 0; //無參數構造方法,默認容量10 public MyArryList() { this.elementData = myArray; } //帶參數構造函數 public MyArryList(int length) { if (length < 0) { throw new IllegalArgumentException("參數不能小於0"); } this.elementData = new Object[length]; size = length; } //獲取長度 public int getSize() { return this.size; } //添加元素 public void add(Object args) { //每次添加元素時須要考慮長度問題 //判斷是否須要擴容 if (size >= elementData.length) { this.grown(); } elementData[size++] = args; } //擴容方法 private void grown() { if (elementData.length <= 1) { elementData = Arrays.copyOf(elementData, elementData.length + 1); } else { elementData = Arrays.copyOf(elementData, elementData.length + (elementData.length >> 1)); } } //刪除元素 public void remove(Object obj) { //先查找第一次出現的索引 int i = indexOf(obj); if (i != -1) { remove(i); } } //刪除指定索引位置元素 public void remove(int index) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("參數越界"); } //遍歷數組 //for(int i = index;i<elementData.length;i++){ // elementData[i]=elementData[i+1]; //} System.arraycopy(elementData, index + 1, elementData, index, size - index - 1); size--; } //指定索引插入元素 public void insert(int index, Object obj) { if (index < 0 || index > size) { throw new IndexOutOfBoundsException("參數越界"); } // if (size >= elementData.length) { this.grown(); } //[1,2,3,4][1,2,5,3,4]第一種方式 // System.arraycopy(elementData,index, elementData,index-1,size-index); //遍歷數組 for (int i = size - 1; i >= index; i--) { elementData[i + 1] = elementData[i]; } //將須要插入元素放入指定索引位置 elementData[index] = obj; size++; } //查找元素第一次索引 public int indexOf(Object obj) { for (int i = 0; i < size; i++) { if (elementData[i] == obj || elementData[i] != null && elementData[i].equals(obj)) { return i; } } return -1; } //更改元素 public void set(int index, Object obj) { elementData[index] = obj; } //清空數據 public void clear() { elementData = new Object[0]; size = 0; } //判斷索引是否越界 public boolean out(int index) { if (index < 0 || index > size) { return false; } else return true; } //判斷是否包含 public boolean contains(Object obj) { return indexOf(obj) != -1; } //判斷是否爲空 public boolean isEmpty() { return size == 0; } //截取子列表 public MyArryList sublist(int fromIndex, int toIdex) { if (fromIndex < 0 || toIdex > size) { throw new IndexOutOfBoundsException("越界"); } if (fromIndex > toIdex) { throw new IllegalArgumentException(); } MyArryList sublist = new MyArryList(toIdex - fromIndex); System.arraycopy(elementData, fromIndex, sublist, 0, toIdex - fromIndex); sublist.size = toIdex - fromIndex; return sublist; } //toString重寫 @Override public String toString() { Object[] newstring =new Object[size]; System.arraycopy(elementData, 0, newstring, 0, size); return Arrays.toString(newstring); } }