4:46 am - Tuesday April 25, 2017

Remove the first occurrence of the specified element from Array list remove(Object o)

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.ArrayList;
import java.util.List;
public class ArrayListSet {
public static void main(String args[])
{
// create an arraylist
ArrayList arrlist = new ArrayList(6);
// add elements using add method
arrlist.add("a");
arrlist.add("b");
arrlist.add("e");
arrlist.add("c");
arrlist.add("d");
arrlist.add("e");
// size of arraylist
System.out.println("size of the list is:" + arrlist.size());
for (String strng : arrlist) {
System.out.println("String is:" + strng);
}
// remove first occurrence of e
arrlist.remove("e");
System.out.println("new array list size is:" + arrlist.size());
for (String strng : arrlist) {
// print new arraylist
// System.out.println("size of new arraylist:" +arrlist.size());
System.out.println("String=" + strng);
}
}
}

output:

size of the list is:6

String is:a

String is:b

String is:e

String is:c

String is:d

String is:e

new array list size is:5

String=a

String=b

String=c

String=d

String=e

Explanation:

public boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged. More formally, removes the element with the lowest index i such that(o==null ? get(i)==null : o.equals(get(i))) (if such an element exists). Returns true if this list contained the specified element (or equivalently, if this list changed as a result of the call).

Specified by:

remove in interface Collection<E>

Specified by:

remove in interface List<E>

Overrides:

remove in class AbstractCollection<E>

Parameters:

o – element to be removed from this list, if present

Returns:

true if this list contained the specified element

 

 

BACK

Filed in: Java Examples

Comments are closed.