5:37 pm - Thursday August 17, 2017

Removes the element at the specified position in the Array list(int index).

CANDID INDUSTRIAL TRAINING -- CHENNAI
Corporate Training for Experienced Candidate
Struts | Hibernate | Spring | Java / J2EE
SOAP | RestFull | Design Pattern | more...
Ph: +91 72000 69003
137b, 2nd st, shanthi nagar, Chrompet, Chennai -600044

BACK

This method is used to remove the element at the specified position in Array List.

Example Program


import java.util.*;

public class ArrayListRemove {
public static void main(String args[]) {
// create an arraylist
ArrayList<Integer> arrlist = new ArrayList<Integer>(5);
// add elements using add method
arrlist.add(21);
arrlist.add(33);
arrlist.add(29);
arrlist.add(30);
arrlist.add(33);
// size of arraylist
System.out.println("size of the list is:" + arrlist.size());
for (Integer num : arrlist) {
System.out.println("value is:" + num);
}
// remove the elemnt at 2nd position
arrlist.remove(1);
System.out.println("new array list size is:" + arrlist.size());

for (Integer num : arrlist) {
// print new arraylist
// System.out.println("size of new arraylist:" +arrlist.size());
System.out.println("value=" + num);
}
}
}

output:

size of the list is:5

value is:21

value is:33

value is:29

value is:30

value is:33

new array list size is:4

value=21

value=29

value=30

value=33

 

EXPLANATION:

public E remove(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Specified by:

remove in interface List<E>

Overrides:

remove in class AbstractList<E>

Parameters:

index – the index of the element to be removed

Returns:

the element that was removed from the list

Throws:

IndexOutOfBoundsException – if the index is out of range (index < 0 || index >= size())

BACK

 

 

Filed in: Java Examples

Comments are closed.