ArrayList is the most popular implementation of List in java.
ArrayList is very similar to Array but provides the feature of dynamic space allocation when the number of objects in the list grows.
ArrayList与Array非常相似,但是当列表中的对象数量增加时,它提供了动态空间分配的功能。
In Array, we have to provide the size at the time of initialization but that is not required for ArrayList.
在Array中,我们必须在初始化时提供大小,但这不是ArrayList所必需的。
Actually, when you initialize ArrayList, it automatically assigns its initial capacity to 10.
实际上,初始化ArrayList时,它会自动将其初始容量分配给10。
使用数组实现ArrayList (Implement ArrayList using Array)
ArrayList is implemented on top of array. Here I am trying to implement custom ArrayList with an Array and provide basic functions such as get(index)
, add(object)
and remove(index)
.
ArrayList在数组顶部实现。 在这里,我尝试使用Array实现自定义ArrayList并提供基本功能,例如get(index)
, add(object)
和remove(index)
。
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){
//we need to increase the size of 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));
}
}
This is the basic implementation of ArrayList using an Array. The idea is to understand how an ArrayList is implemented. For development purposes, use the ArrayList class from the Collections API.
这是使用Array的ArrayList的基本实现。 这个想法是要了解如何实现ArrayList。 出于开发目的,请使用Collections API中的ArrayList类。
Below is the output produced when we execute the above program.
下面是执行上述程序时产生的输出。
$ javac MyArrayList.java
$ java MyArrayList
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
$
References:
参考文献:
翻译自: https://www.journaldev.com/110/how-to-implement-arraylist-with-array-in-java