In this series, I will be discussing some data structures, this post is about the array data structure.
The array is a fundamental data structure, allowing us to store, retrieve and change values of objects of the same type. I'm sure you must have used arrays in one way, shape or form. In this post, we will be implementing our own custom array class, to help us understand and appreciate the underlying concepts of the array data structure.
Attributes of our array
An array must be able to store multiple values, for simplicity's sake, our custom array can only hold integer values. Then our array must have a capacity, that is the maximum number of elements that array can take. We also need the current count of the array, meaning the number of values in the array. Note that the count of the array must be less than the capacity, otherwise, our array cannot hold all the values, if this occurs we would have to resize the array, adding more capacity.
When we create a new array of capacity 5, we'll have an array with default values 0:[0,0,0,0,0]
if after we insert two elements in our array, the count is 2, it would look something like [8,3,0,0,0]
We'll need a getter for our count field, to be able to know the count at any time
Now let's code out the structure of our array class.
public class SuperArray {
private int [] elements;
private int count;
public SuperArray(int capacity){
elements = new int [capacity];
count = 0;
}
public int getCount() {
return count;
}
}
Now that we have our class, as it is now, it can't do anything, so we need to give it some functionality.
Inserting an element
Our array class won't be useful if we are not able to add elements to it.
Let's create a method that inserts an element into our array.
public void insert(int value){
if(count < elements.length){
items[count] = value;
count++;
}else{
//resize array
}
}
Inserting at a specific index
To insert to a specific index, our method accepts the index and the value to be placed at that index. We check the bounds if the index provided is valid, if it is, we move the items after the index one step to the right, then insert the value at the index
public void insertAt(int index, int value){
if(index < 0 || index >= count)
throw new IllegalArgumentException();
for (int i = count - 1; i >= index; i--){
items[i + 1] = items[i];
}
items[index] = value;
count++;
}
Get element by index
We should be able to get elements from our array, we just do a check if the index is within bounds, if it is, we return the value.
public int get(int index) {
if (index < 0 || index >= count) {
throw new IndexOutOfBoundsException("Index out of bounds: " + index);
}
return elements[index];
}
With these we have implemented the basic functionalities of an array, the following methods would be some nice-to-have features for our array.
Get the index of an element
This method allows us to search for the index by using the value, we return -1 if the value is not found in the array.
public int indexOf(int item){
for(int i = 0; i < count; i++){
if(items[i] == item)
return i;
}
return -1;
}
Get the maximum value in the array
Simply returns the maximum number in the array.
public int max(){
int max = 0;
for(int i = 0; i < count; i++){
if(items[i] > max)
max = items[i];
}
return max;
}
Conclusion
Building your own array class can be a great way to deepen your understanding of the array data structure. In this blog post, we've covered the basic steps for creating a simple array class, including defining the class and implementing basic operations. Feel free to explore more advanced features and functionalities to enhance your array class further. Happy coding!