3:13 pm - Tuesday December 17, 3129

Java Program used to to remove the specified element in the list using remove() method

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

Remove Index program to remove the specified element in the list

Example Program


import java.util.ArrayList;

public class RemoveIndex {
public static void main(String[] args) {
ArrayList al = new ArrayList();
al.add(“0″);
al.add(“1″);
al.add(“2″);
al.add(“3″);
System.out.println(“Arraylist is” + al);
Object obj = al.remove(3);
System.out.println(“After using remove : ” + obj);
for (int index = 0; index < al.size(); index++) System.out.println(al.get(index)); } } [/java] Output

Arraylist is[0, 1, 2, 3]

After using remove : 3

0

1

2

Description

The java.util.ArrayList.remove(int index) method removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

Declaration

Following is the declaration for java.util.ArrayList.remove() method

public E remove(int index)

Parameters

     index — The index of the element to be removed .

Return Value

    This method returns the element that was removed from the list .

Exception

    IndexOutOfBoundsException — if the index is out of range.

 

BACK

 

 

Filed in: Z-A All Java Codes

Comments are closed.