如何在Java中使用Array实现ArrayList

ArrayList is the most popular implementation of List in java.

ArrayListjavaList的最受欢迎的实现。

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
$
GitHub Repository. GitHub Repository中查看更多Java代码示例。

References:

参考文献:

翻译自: https://www.journaldev.com/110/how-to-implement-arraylist-with-array-in-java

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值