Join
Blogs
Questions
Videos
Tags
Members
Search
 
 
Create your own blog, earn points and get popular!

Get Elements, Remove Elements, Replace Elements from Java ArrayList

Replace specified element from Java ArrayList

1) Object set(int index, Object element)

Replaces the element at specified index with the object passed as a parameter. Returns the previous element that has been replaced.

Get element from the ArrayList

1) Object get(int index)

Returns the element at the specified index in the ArrayList. Note : We need to downcast the returned object to the desired type.


Remove elements from the ArrayList

1) Object remove(int index)

Removes the elements at the specified index in the list and shifts the subsequent element of the list by one. This method returns the element that has been removed from the list. This method can throw IndexOutOfBoundException if the index specified is out of range.

2) void removeRange(int startIndex, int endIndex)

Removes the elements from list whose index is between startIndex inclusive and endIndex exclusive and shifts the subsequent elements of the list.

3) void clear()

Removes all elements of the ArrayList.

Convert ArrayList in to an Object array

1) Object[] toarray()

Returns array of Object containing all elements of the ArrayList in the correct order.

This brings us end of Java ArrayList Tutorial. Please let us know your views by leaving comment in comments section below.


<< Search Elements in ArrayList
    Java ArrayList Example >>
Your rating: None Average: 5 (1 vote)
Share this

Post new comment