ArrayList 類似於 Array 對象,但是當列表中的對象數量增加時,它提供了動態空間分配的功能。在 Array 對象中,我們需要在初始化時提供數組的大小,但這並不是 ArrayList 所必需的。實際上,當您初始化ArrayList時,它將自動將其容量分配爲10。
這裏我用 Array 對象實現ArrayList,並提供 get(index),add(object) 和 remove(index) 等基本函數。
import java.util.ArrayList;
/**
* <p></p>
*
* @author 三產
* @version 1.0
* @date 2017-03-24
* @QQGroup 213732117
* @website http://www.coderknock.com
* @copyright Copyright 2017 拿客 coderknock.com All rights reserved.
* @since JDK 1.8
*/
public class MyArrayList {
private static final int SIZE_FACTOR=5;
private Object data[];
private int index;
private int size;
public MyArrayList(){
this.data=new Object[SIZE_FACTOR];
this.size=SIZE_FACTOR;
}
public void add(Object obj){
System.out.println("index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
if(this.index==this.size-1){
//我們需要加大data[]的大小
increaseSizeAndReallocate();
}
data[this.index]=obj;
this.index++;
}
private void increaseSizeAndReallocate() {
this.size=this.size+SIZE_FACTOR;
Object newData[]=new Object[this.size];
for(int i=0; i<data.length;i++){
newData[i]=data[i];
}
this.data=newData;
System.out.println("***index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
}
public Object get(int i) throws Exception{
if(i>this.index-1){
throw new Exception("ArrayIndexOutOfBound");
}
if(i<0){
throw new Exception("Negative Value");
}
return this.data[i];
}
public void remove(int i) throws Exception{
if(i>this.index-1){
throw new Exception("ArrayIndexOutOfBound");
}
if(i<0){
throw new Exception("Negative Value");
}
System.out.println("Object getting removed:"+this.data[i]);
for(int x=i; x<this.data.length-1;x++){
data[x]=data[x+1];
}
this.index--;
}
public static void main(String[] args) throws Exception {
MyArrayList mal = new MyArrayList();
mal.add("0");
mal.add("1");
mal.add("2");
mal.add("3");
mal.add("4");
mal.add("5");
mal.add("6");
mal.add("7");
mal.add("8");
mal.add("9");
mal.remove(5);
System.out.println(mal.get(7));
}
}
輸出如下:
index:0size:5data size:5
index:1size:5data size:5
index:2size:5data size:5
index:3size:5data size:5
index:4size:5data size:5
***index:4size:10data size:10
index:5size:10data size:10
index:6size:10data size:10
index:7size:10data size:10
index:8size:10data size:10
index:9size:10data size:10
***index:9size:15data size:15
Object getting removed:5
8
這只是使用 Array 基本實現 ArrayList 的功能,只爲了解其原理。還請使用 JDK 中的 ArrayList。
原文中併爲實現泛型,我們就來補充下:
import java.util.ArrayList;
/**
* <p></p>
*
* @author 三產
* @version 1.0
* @date 2017-03-24
* @QQGroup 213732117
* @website http://www.coderknock.com
* @copyright Copyright 2017 拿客 coderknock.com All rights reserved.
* @since JDK 1.8
*/
public class MyArrayList<T> {
private static final int SIZE_FACTOR=5;
private Object data[];
private int index;
private int size;
public MyArrayList(){
this.data=new Object[SIZE_FACTOR];
this.size=SIZE_FACTOR;
}
public void add(T obj){
System.out.println("index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
if(this.index==this.size-1){
//我們需要加大data[]的大小
increaseSizeAndReallocate();
}
data[this.index]=obj;
this.index++;
}
private void increaseSizeAndReallocate() {
this.size=this.size+SIZE_FACTOR;
Object newData[]=new Object[this.size];
for(int i=0; i<data.length;i++){
newData[i]=data[i];
}
this.data=newData;
System.out.println("***index:"+this.index+"size:"+this.size+"data size:"+this.data.length);
}
public T get(int i) throws Exception{
if(i>this.index-1){
throw new Exception("ArrayIndexOutOfBound");
}
if(i<0){
throw new Exception("Negative Value");
}
return (T) this.data[i];
}
public void remove(int i) throws Exception{
if(i>this.index-1){
throw new Exception("ArrayIndexOutOfBound");
}
if(i<0){
throw new Exception("Negative Value");
}
System.out.println("Object getting removed:"+this.data[i]);
for(int x=i; x<this.data.length-1;x++){
data[x]=data[x+1];
}
this.index--;
}
public static void main(String[] args) throws Exception {
MyArrayList<Integer> mal = new MyArrayList<Integer>();
mal.add(0);
mal.add(1);
mal.add(2);
mal.add(3);
mal.add(4);
mal.add(5);
mal.add(6);
mal.add(7);
mal.add(8);
mal.add(9);
mal.remove(5);
System.out.println(mal.get(7));
}
}
這裏其實只是修改了 add(T)、get(index) 方法。