ArrayList- How to insert data at a particular location?

Posted on Updated on

When working with arrays, a developer is restricted with my limitation. It becomes very difficult to use array in the program, but ArrayList debars all the limitations and let a programmer explore all the possibility with ease.

It is painful task to if we’ve to insert a data in middle of an array, this will require shifting of elements to next index and simultaneously would need to keep a watch on size of array, but this has become very simple with ArrayList.

ArrayList which dynamically grows upon addition of elements to it and it is very easy to add, insert or delete element at any location.

Below example demonstrate how to add and insert a data at any location.

OutPut:

Value in ArrayList before insertion at location 0 is 10
Value in ArrayList before insertion at location 1 is 20
Value in ArrayList before insertion at location 2 is 30
Value before insertion at index '1' is 20
Value after insertion at index '1' is 40
Value in ArrayList after insertion at location 0 is 10
Value in ArrayList after insertion at location 1 is 40
Value in ArrayList after insertion at location 2 is 20
Value in ArrayList after insertion at location 3 is 30

One thought on “ArrayList- How to insert data at a particular location?

    Rob said:
    February 13, 2016 at 4:12 pm

    Very good example.

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.