Array | How to add new elements to an array?

Now we will learn How to add new elements to an array? we can do so simply by giving swapping the elements of the existing array to the right to add in between and for adding at the end we can simply by giving index number and against giving the value which you want to add. Let’s see all the limitations and drawbacks which we can face during adding new elements.

Array and elements

Adding New Element In An Array

As we know we can add the elements in an array at different places like at the end, at the beginning, and even at the last also.

Note:-

There must be sufficient space available to add new elements in the array as the size of an array needs to be declared during initialization so keep in mind during adding elements otherwise it will give an error and to handle an exception that we call Array Out Pf Bound Exceptions. It occurs when we try to access an element that does not even exist.

Let’s add elements at different locations in an array

Adding New Element In Between Or Starting

AS we discussed above we can add the new element at any location of the array so now here we are going to add a new element either at Starting or in between. For doing so we need to shift all the elements in one index toward the right so that we can accommodate the new element in between.

Now let’s follow the Code for executing

importjava.util.*; 
class Main {   
public static void main(String[] args) 
    {   
        // Original array with size 5
        Integer odd_Array[] = { 1,3,7,9,11 }; 
 
        // display the original array 
        System.out.println("Original Array:" + Arrays.toString(odd_Array)); 
 
        // element to be added at index
        int val = 5; 
        int index = 2;
        //dest array with size more than 1 of the original array
        int[] dest_Array = new int[odd_Array.length+1];
        int j = 0;
        //Iterate dest_array and insert new element as well as shift other elements to the right
       for(int i = 0; i <dest_Array.length; i++) {
         if(i == index) {
               dest_Array[i] = val;
             }
             else {
                   dest_Array[i] = odd_Array[j];
                   j++;
             }
        }
        // display the updated array 
        System.out.println("\nArray after adding element " + val + " at index " + index +":"
                            + Arrays.toString(dest_Array)); 
    } 
}

Adding At The End Of The Array

To add at the end we do not need to swap all the elements from right to right by one index.

To do so Just Follow the code given below.

class Main {   
public static void main(String[] args)     {  
        // Original array with size 5
        Integer odd_Array[] = { 1,3,5,7,9 }; 
 
        // display the original array 
        System.out.println("Original Array:" + Arrays.toString(odd_Array)); 
 
        // element to be added 
        int val = 11; 
 
        // convert array to Arraylist
        List<Integer>oddlist = new ArrayList<Integer>(Arrays.asList(odd_Array)); 
 
        // Add the new element 
        oddlist.add(val); 
 
        // Convert the Arraylist back to array 
        odd_Array = oddlist.toArray(odd_Array); 
 
        // display the updated array 
        System.out.println("\nArray after adding element " + val + ":" + Arrays.toString(odd_Array)); 
    } 
}

Learn More about Array | How to add new elements to an array? At: New elements in array

And also Visit Java Tutorials  To know more about solutions to java problems also visit to Python’s list: What is the difference Between methods Append and Extend? to know more about Python and other programming Languages.

Leave a Comment

%d bloggers like this: