Contact@itspring.com
  • Java Program To Delete And Replace Elements From An Array

    Deleting element from an array :

    Java hasn't provided any direct way to delete element from an array. The reason being Arrays are of fixed size in Java, that means, once you create an array you can't change its size. Which means removing or deleting an item doesn't reduce the size of the array.
     
    First let's do it using loop : 
     
    To delete element from a particular index, you need to shift the elements having higher index to left by one position.
    e.g. Consider that you want to remove element at index 4, so you have to shift element at index 4 to 3, element at index 5 to element at index 4, element at index 6 to element at index 5 and so on.
     
    Note : This is just shifting of elements and it looks like removing the element. But if you check the array length after everything is done, it will be same.
     
    Here is the code :

     

    <p>&nbsp;/*Java program to show how to remove element from Array*/</p>
    <
    div>public class DeleteElemDemo {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">    </span>public static void main(String args[]) {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// array of int type</div>
    <div><span style="white-space:pre">        </span>int[] arr = new int[] { 50100150200250 };</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// index of element to be removed</div>
    <div><span style="white-space:pre">        </span>int remIndex 3;</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// check length of array before deletion</div>
    <div><span style="white-space:pre">        </span>System.out.println(&quot;Array Size Before Deletion : &quot; + arr.length);</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// print array before deletion</div>
    <div><span style="white-space:pre">        </span>for (int i 0&ltarr.lengthi++) {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">            </span>System.out.print(arr[i] + &quot; &quot;);</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>}</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// remove element at index 3</div>
    <div><span style="white-space:pre">        </span>for (int i remIndex&ltarr.length-1i++) {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">            </span>arr[i] = arr[1];</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>}</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// print array after deletion</div>
    <div><span style="white-space:pre">        </span>System.out.println(&quot;\n\Array After Deletion&quot;);</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>for (int i 0&ltarr.length 1i++) {</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">            </span>System.out.print(arr[i] + &quot; &quot;);</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>}</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">        </span>// check length of array before deletion</div>
    <div><span style="white-space:pre">        </span>System.out.println(&quot;\nArray Size After Deletion : &quot; + arr.length);</div>
    <
    div>&nbsp;</div>
    <
    div><span style="white-space:pre">    </span>}</div>
    <
    div>}</div>
    <
    div>&nbsp;</div>
    <
    div>}</div>

Most Liked Posts

Post A Comment

Your email address will not be published. Required fields are marked*

D7T1

0 Comments

No Record found
demo content

The Total Economic Impactâ„¢ of ServiceNow

United States Tue Jun 06 2017

Join us for a webinar where we will reveal the quantified value of the Lightspeed Enterprise for customers that are modernizing IT, eliminating service outages, and optimizing performance with real-time analytics.

Guest speakers Liz Witherspoon of Forrester, and study participant Brad Paubel, VP of Internal Technologies at Maritz, will discuss

  • The value drivers for IT and key quantified economic benefits
  • Key challenges that study customers faced and overcame
  • Focus areas and best practices you can use to drive business impact with ServiceNow

Register

Webinar Registration

Enter the characters visible in the box below

DIA4

Share

Share using

Advertise With Us