The set() method of Java AbstractSequentialList is used to replace any particular element in the list created using the AbstractSequentialList class with another element. This can be done by specifying the position of the element to be replaced and the new element in the parameter of the set() method.
Syntax:
public E set(int index, Object element)
Parameters: This function accepts two parameters as shown in the above syntax and described below.
- index: This is of integer type and refers to the position of the element that is to be replaced from the list.
- element: It is the new element by which the existing element will be replaced and is of the same object type as the list.
Return Value: The method returns the previous value from the list that is replaced with the new value.
Exception: This method throws following exceptions:
- UnsupportedOperationException: if the set operation is not supported by this list
- ClassCastException: if the class of the specified element prevents it from being added to this list
- NullPointerException: if the specified element is null and this list does not permit null elements
- IllegalArgumentException: if some property of the specified element prevents it from being added to this list
- IndexOutOfBoundsException: if the index is out of range (index = size())
Below program illustrate the Java.util.AbstractSequentialList.set() method:
Example 1:
import java.io.*;
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
AbstractSequentialList<String> list
= new LinkedList<String>();
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
list.add( "10" );
list.add( "20" );
System.out.println( "AbstractSequentialList:"
+ list);
System.out.println( "The Object that is replaced is: "
+ list.set( 2 , "GFG" ));
System.out.println( "The Object that is replaced is: "
+ list.set( 4 , "50" ));
System.out.println( "The new AbstractSequentialList is:"
+ list);
}
}
|
Output:
AbstractSequentialList:[Geeks, for, Geeks, 10, 20]
The Object that is replaced is: Geeks
The Object that is replaced is: 20
The new AbstractSequentialList is:[Geeks, for, GFG, 10, 50]
Example 2: To demonstrate IndexOutOfBoundException
import java.io.*;
import java.util.*;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
AbstractSequentialList<String> list
= new LinkedList<String>();
list.add( "Geeks" );
list.add( "for" );
list.add( "Geeks" );
list.add( "10" );
list.add( "20" );
System.out.println( "AbstractSequentialList:"
+ list);
System.out.println( "Trying to replace 10th "
+ "element with GFG" );
try {
list.set( 10 , "GFG" );
}
catch (Exception e) {
System.out.println(e);
}
}
}
|
Output:
AbstractSequentialList:[Geeks, for, Geeks, 10, 20]
Trying to replace 10th element with GFG
java.lang.IndexOutOfBoundsException: Index: 10, Size: 5
Feeling lost in the vast world of Backend Development? It's time for a change! Join our
Java Backend Development - Live Course and embark on an exciting journey to master backend development efficiently and on schedule.
What We Offer:
- Comprehensive Course
- Expert Guidance for Efficient Learning
- Hands-on Experience with Real-world Projects
- Proven Track Record with 100,000+ Successful Geeks
Last Updated :
24 Dec, 2018
Like Article
Save Article