"👨💻 Building a Custom Array Class in Java: A Guide for Interview Preparation 📚"
In programming interviews, understanding fundamental data structures like arrays is crucial. Arrays are a staple in programming, offering a way to store and manipulate a collection of elements. However, Java’s built-in arrays have fixed sizes. In this blog, we’ll create a dynamic array class, CustomArray
, enhancing functionality and offering a deeper understanding of array mechanics.
Understanding the Basics
Arrays in Java store multiple items of the same type efficiently. They’re indexed, allowing for fast access to elements. However, their fixed size is a limitation in many scenarios. This is where our CustomArray
comes into play, offering dynamic resizing and additional functionalities.
Setting Up the Array Class
We start by defining our class with essential member variables: an integer array to hold the elements and a count to keep track of the actual number of elements.
Code Snippet:
public class CustomArray {
private int[] items;
private int count;
public CustomArray(int length) {
items = new int[length];
}
}j
In this code, items
is an array that stores the elements. The count
variable tracks how many items are currently in the array.
Implementing Core Methods
1. Add Method
The add
method includes elements of our CustomArray
. When the underlying array is full, we resize it.
Code Snippet:
public void add(int item) {
if (count == items.length) {
int[] newItems = new int[count * 2];
for (int i = 0; i < count; i++)
newItems[i] = items[i];
items = newItems;
}
items[count++] = item;
}
Here, if the array is full (when count
equals the length of items
), we create a new array double the size, copy the old elements to it, and then add the new item.
The add
method involves two scenarios:
- Adding an item when the array is not full: In this case, the operation is
O(1)
, meaning it has constant time complexity. This is because adding an element to an existing array at a known index (likecount
) is a direct operation. - Adding an item when the array is full: Here, the method must create a new array and copy all elements from the old array to the new one. This operation is
O(n)
, wheren
is the number of elements in the array. This is because each item must be individually copied to the new array.
2. Remove Method
Removing an item requires shifting the elements to fill the gap created by the removed item.
Code Snippet:
public void remove(int index) {
if (index < 0 || index >= count)
throw new IllegalArgumentException();
for (int i = index; i < count; i++)
items[i] = items[i + 1];
count--;
}
This method throws an exception if the index is out of bounds. Otherwise, it shifts each element one position to the left, starting from the given index.
The remove
method has a time complexity of O(n)
. This is because, in the worst case, it may need to shift all the elements after the removed element one position to the left to fill the gap. The number of elements to shift varies based on the index of the element being removed but can be up to n - 1
in the worst-case scenario (removing the first element).
3. Index Of Method
The indexOf
method returns the index of a specified item, or -1 if it's not found.
Code Snippet:
public int indexOf(int item) {
for (int i = 0; i < count; i++)
if (items[i] == item)
return i;
return -1;
}
This method iterates through the array. If it finds the item, it returns its index; otherwise, it returns -1.
The indexOf
method has a time complexity of O(n)
because, in the worst case, it might need to check each element in the array until it finds the target element or reaches the end of the array.
Enhancing the Array Class
To make CustomArray
more robust, consider adding methods like resize
, print
, and others. For instance, a print
method to display the array elements can be handy:
public void print() {
for (int i = 0; i < count; i++)
System.out.println(items[i]);
}
The print
method also has a time complexity of O(n)
. This is straightforward as it involves iterating through all n
elements of the array once to print them.
Testing Your Array Class
It’s important to test your class to ensure it works correctly.
Code Snippet:
public static void main(String[] args) {
CustomArray array = new CustomArray(3);
array.add(10);
array.add(20);
array.add(30);
array.remove(1);
System.out.println(array.indexOf(30));
array.print();
}
This test adds three items, removes one, and then prints the index of an item and all current items.
Conclusion
Building a custom array class in Java helps deepen your understanding of data structures, a key aspect of programming interviews. Through this exercise, you’ve learned about dynamic arrays, element addition and removal, and searching for elements.
Experiment with the code, try adding more functionalities and use this knowledge to enhance your problem-solving skills for interviews.