remove (Object o) method of List Interface in java.

remove (Object o) method of List Interface in java.


Posted in : Core Java Posted on : January 11, 2011 at 6:16 PM Comments : [ 0 ]

In the following example we will discuss how can you remove specified element from a list.

remove (Object o) method of List Interface in java.

In the following example we will discuss how can you remove specified element from a list.

Syntax 

public boolean remove (Object o) 

This method returns a boolean value True/False.

In this method you can remove an element from a list, it returns either true or false. It returns 'true' when an element is removed or returns 'false' when an element is not removed or does not exist in list. Through this method you can remove a first occurrence of specified element from a list. After deletion of an element the lists index is reduced by (-1). All the rest elements which are positioned right from previously removed element are shifted left by subtracting (-1) in their indexes in the order they are previously arranged. 

Parameter Description

Object o : It takes an argument as element to be removed from the list. If element is present in list then remove (Object o) method remove the element and if not present then list is remain same. 

Example of remove (Object o) method 

In this example we will show you how does remove (Object o) method works in List interface. This example will help you to understand how can you remove the first occurrence of specified element of a list. Through this example we will show you adding of element to the list, show the element with their position, size of list,  and deletion of specified element of a list.

Example:-

package Devmanuals.com;
import java.util.List;
import java.util.ArrayList;
public class ListRemoveObject {
public static void main(String args[]){
ArrayList al = new ArrayList ();
al.add("3");
al.add("1");
al.add("2");
al.add("3");
al.add("4");
System.out.println("Initial size of list is : "+al.size());
for(int i=0;i<al.size();i++)
System.out.println("Element at the position " +i+"=" +al.get(i));
boolean b=al.remove("3");
System.out.println("Element is removed : "+b);
System.out.println("The new list with their index values are : " );
for(int i=0;i<al.size();i++)
System.out.println("At position " +i + " element is " +al.get(i));
System.out.println("Size of list after removing element is : "+al.size());
boolean bol=al.remove("5");
System.out.println("Element is removed : "+bol);
System.out.println("List is : "+al);
}
}

Output :

Initial size of list is : 5

Element at the position 0=3

Element at the position 1=1

Element at the position 2=2

Element at the position 3=3

Element at the position 4=4

Element is removed : true

The new list with their index values are :

At position 0 element is 1

At position 1 element is 2

At position 2 element is 3

At position 3 element is 4

Size of list after removing element is : 4

Element is removed : false

List is : [1, 2, 3, 4]

Download Source Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics