Inserting an element into a Java array
Here’s an example of how you can insert an element into a Java array:
First, let’s say we have an array of integers named originalArray
:
int[] originalArray = {1, 2, 3, 4, 5};
To insert an element, let’s say the value 6
, at a specific index, let’s say index 2
, we can follow these steps:
- Create a new array,
newArray
, with a length one greater than the original array:
int[] newArray = new int[originalArray.length + 1];
- Loop through the original array up to the desired index and copy each element to the new array:
for (int i = 0; i < index; i++) {
newArray[i] = originalArray[i];
}
- Insert the desired element at the desired index:
newArray[index] = element;
- Loop through the remaining elements in the original array, starting from the index, and copy them to the new array, starting from the next index:
for (int i = index; i < originalArray.length; i++) {
newArray[i + 1] = originalArray[i];
}
After executing these steps, the newArray
will contain the inserted element at the specified index.
Here’s the complete code for inserting an element into a Java array:
int[] originalArray = {1, 2, 3, 4, 5};
int index = 2;
int element = 6;
int[] newArray = new int[originalArray.length + 1];
for (int i = 0; i < index; i++) {
newArray[i] = originalArray[i];
}
newArray[index] = element;
for (int i = index; i < originalArray.length; i++) {
newArray[i + 1] = originalArray[i];
}
// newArray now contains the inserted element
Remember to modify the index
variable and the element
variable according to your requirement.
#java #array