We all know about the basic data structure, which is Array pretty well. And in java they are static. It means we have to allocate memory for the array ahead of time. The memory will define the number of elements that the array can hold. But what if we have know idea about the exact number of elements that we are going to insert before hand. Then the best thing is to have a dynamic array.

我们都知道基本的数据结构,即数组 。 在Java中,它们是静态的。 这意味着我们必须提前为阵列分配内存。 内存将定义数组可以容纳的元素数。 但是,如果我们事先知道要插入的元素的确切数目该怎么办。 最好的是拥有一个动态数组。

A dynamic array automatically grows when we try to make an insertion and there is no space left for the new item. A simple dynamic array can be constructed, by using an array of fixed-size. The elements of the array are stored contiguously, for an instance, if it is an integer type array it will take 4 bytes of space per integer element. The remaining positions towards the end are reserved and unused. New elements can be added to the end of the array until the reserved space is fully utilized. If you still need to add up more elements even after that, the array needs to be resized which is a very expensive task. Usually it doubles its size.

当我们尝试插入时,动态数组会自动增长,并且新项目没有剩余空间。 通过使用固定大小的数组,可以构造一个简单的动态数组。 数组的元素是连续存储的,例如,如果它是整数类型的数组,则每个整数元素将占用4个字节的空间。 剩余的剩余位置将保留并未使用。 可以将新元素添加到数组的末尾,直到完全利用了保留的空间。 如果之后仍然需要添加更多元素,则需要调整数组大小,这是一项非常昂贵的任务。 通常,它的大小会增加一倍。

Following is the Figure 1, where you can find the Array class, to model a dynamic behavior by using a static array in Java. Basic array operations are implemented below.

以下是图1 ,您可以在其中找到Array类,以通过使用Java中的静态数组来对动态行为进行建模。 基本数组操作在下面实现。

public class Array {
	
	
	private int[] items;
	private int count;
	
	public Array(int length) {
		
		items = new int[length];
	}
	
	public void print() {
		
		for(int i=0;i<count;i++) {
			System.out.println(items[i]);
		}
	}
	
	public void insert(int number) {
		
		//If an item is added to the end
		items[count]=number;
		count++;
		
		//If the array is full, resize it
		if(items.length == count) {
			
			int[] newItems = new int[count*2];
			
			for(int i=0;i<count;i++) {
				newItems[i]=items[i];
			}
			
			items=newItems;
		}
		
	}
	
	//Delete
	public void removeAt(int index) {
		
		if(index<0 || index>=count ) {
			throw new IllegalArgumentException();
		}	
		
		for(int i=index;i<count;i++) {
			items[i]=items[i+1];
		}
		
		count--;
	}
	
	//Search
	public int indexOf(int number) {
		
		for(int i=0;i<count;i++) {
			if(number == items[i]) {
				return i;
			}
		}
		
		return -1;
		
	}
}

First initialize the array inside the parameterized constructor with the length provided. print() method will display all the elements of the array. In here, you have to concern about having a new variable named count, instead using items.length, otherwise it will end up displaying three zeros when no number is inserted.

首先使用提供的长度在参数化构造函数中初始化数组。 print()方法将显示数组的所有元素。 在这里,您必须考虑拥有一个名为count的新变量 使用items.length,否则当没有插入数字时,它将最终显示三个零。

Variable count has to be incremented by one whenever new number is inserted. And yes, we are talking about the insert() method. Then most cruical point comes into play. When the array is full what we should do? Simply create a new array making it doubled the size. Then copy all the existing elements to the new array. Then point new array to the existing array. This will make it recursive. Big O Notation for the insertion would be O(n), since we have to copy all the elemnts to the new array which is very expensive.

每当插入新数字时,变量计数就必须增加一。 是的,我们正在谈论insert()方法。 然后最关键的一点开始发挥作用。 当阵列已满时,我们应该怎么做? 只需创建一个新数组,使其大小增加一倍即可。 然后将所有现有元素复制到新数组。 然后将新阵列指向现有阵列。 这将使其递归。 插入的大O表示法将是O(n),因为我们必须将所有元素复制到新数组中,这非常昂贵。

Next comes the delete method which is removeAt(). This deletes an item based on their index. First make sure the provided index is within the range ( 0 - count ). Then the element based on the index, will be removed and to fill that hole, all the other elements from the right side, should be shifted left accordingly. Next decrement the count by one. Big O Notation for the best case of deleting an element would be O(1), which would be the last element. For the worst case it is O(n), which will be the element at 0th index.

接下来是delete方法,它是removeAt() 。 这将根据其索引删除项目。 首先,请确保提供的索引在(0-count)范围内。 然后,将删除基于索引的元素,并填充该Kong,所有其他右侧元素应相应向左移动。 接下来将计数减一。 对于删除元素的最佳情况Big O表示法O(1) ,它是最后一个元素。 最坏的情况O(n) ,它将是索引为0的元素。

Last implemented method is searching the index by value, indexOf() method. Searching the perticular value through a for loop will give the relevant index. Resulting -1 if not found.

最后实现的方法是按索引搜索indexOf()方法。 通过for循环搜索垂直值将给出相关索引。 如果未找到,则结果为-1。

And finally here's the Main test class in Figure 2 to run the code and check the output.

最后是图2中的Main测试类,用于运行代码并检查输出。

public class Main {


	public static void main(String[] args) {
		
		Array numbers = new Array(3);
		numbers.insert(10);
		numbers.insert(20);
		numbers.insert(30);
		numbers.insert(40);
		numbers.insert(50);
		numbers.insert(60);
		numbers.removeAt(0);
		System.out.println("index of the number : " +numbers.indexOf(60));
		numbers.print();
	
	}


}

This is all about a brief explanation how you can come up with a dynamic array by using static array features.

这就是对简短说明的简要说明,您将如何使用静态数组功能创建动态数组。