MadAlgos Blog
Basic operations on Arrays ep02
A joke to begin with… 😂
How do arrays throw a party?
They insert new elements and delete boring ones, making sure everyone has a good time!
Ah, the fascinating world of arrays in Java, where elements come and go like guests at a lively party.
Back to the topic, In the previous blog (https://madalgos.in/blog-space/20) you'all have understood how the tracesal is done in arrays.
In this series of Basic operations on Arrays, we will learn more about operations on Arrays.
Let's start with inserting elements into an array.
Picture an array as a line for a roller coaster ride. Every element represents a person eagerly waiting their turn. When you want to insert a new person, it's like squeezing them into the line.
To insert an element, you have to make space for them.
It's like politely asking people in the line to "move over, make some room!" Once there's space, you can slide the new person in, making sure to maintain the order and avoid any collisions.
Think of it as performing a delicate dance routine amidst a crowded line.
Now, onto deleting elements from an array.
Imagine the array as a potluck party, where each element represents a delicious dish. Deleting an element is like removing a dish that didn't quite hit the mark.
You simply pluck it out of the array, leaving a noticeable gap. The remaining elements might shift over, closing the gap, much like the guests who quickly fill in the space on their plates with other scrumptious options.
But wait, there's more! Sometimes you don't want to leave an empty space behind, so you need to fill it up.
You can do this by shifting the subsequent elements forward, closing the gap and making the array look neat and tidy again.
It's like rearranging the dishes on the buffet table to maintain a visually pleasing display.
Certainly! Let's dive into the technical aspects of inserting and deleting elements in Java arrays with some code examples.
Inserting elements in arrays
To insert an element into an array, you typically need to create a new array with a larger size, copy the existing elements along with the new element into the new array, and then reassign the reference of the new array to the original array variable.
Here is an example:
// Original arrayint[] originalArray = {1, 2, 3, 4, 5};
// Element to be insertedint newElement = 10;
// Index where the new element should be insertedint insertIndex = 2;
// Create a new array with increased sizeint[] newArray = new int[originalArray.length + 1];
// Copy the elements before the insertion pointfor (int i = 0; i < insertIndex; i++) {newArray[i] = originalArray[i];}
// Insert the new elementnewArray[insertIndex] = newElement;
// Copy the elements after the insertion pointfor (int i = insertIndex; i < originalArray.length; i++) {newArray[i + 1] = originalArray[i];}
// Update the reference to the new arrayoriginalArray = newArray;
In the above code, we have an original array with some elements. We want to insert a new element (10) at a specific position (index 2). We create a new array with increased size, copy the elements before the insertion point, insert the new element, and then copy the remaining elements after the insertion point. Finally, we update the reference to the new array.
Here is the pictorial representation:
Deleting elements in arrays
When deleting an element from an array, you typically shift the elements after the deletion point to fill the gap left by the removed element.
Here's an example:
// Original arrayint[] originalArray = {1, 2, 3, 4, 5};
// Index of the element to be deletedint deleteIndex = 2;
// Create a new array with reduced sizeint[] newArray = new int[originalArray.length - 1];
// Copy the elements before the deletion pointfor (int i = 0; i < deleteIndex; i++) {newArray[i] = originalArray[i];}
// Copy the elements after the deletion pointfor (int i = deleteIndex + 1; i < originalArray.length; i++) {newArray[i - 1] = originalArray[i];}
// Update the reference to the new arrayoriginalArray = newArray;
In the above code, we have an original array with some elements.
We want to delete an element at a specific position (index 2).
For this, we create a new array with reduced size, copy the elements before the deletion point, and then copy the elements after the deletion point, skipping the element we want to delete.
Finally, we update the reference to the new array, effectively removing the desired element from the original array.
Here's the pictorial representation:
So, whether you're playing musical chairs with an array or organizing a feast, Java provides you with the tools to insert and delete elements in a way that's both informative and amusing.
Homework:
Learn about cloning and copying of arrays
Hope you enjoyed reading
Now what?
Follow us on MADAlgos
Happy learning!
Check out the following blogs for reference:
Introduction to arrays:
https://madalgos.in/blog-space/18
https://madalgos.in/blog-space/19
Basic operations on arrays:
https://madalgos.in/blog-space/20