Last Updated : 30 Oct, 2018
The
set()method of
Java.util.AbstractSequentialListis used to replace any particular element in the sequential list created using the LinkedList 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:AbstractSequentialList.set(int index, Object element)Parameters:
This function accepts two parameters as shown in the above syntax and described below.
The method returns the previous value from the sequential list that is replaced with the new value. Below programs illustrate the Java.util.AbstractSequentialList.set() method:
Example 1: Java
// Java code to illustrate set()
import java.util.*;
import java.util.AbstractSequentialList;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<String>
absqlist = new LinkedList<String>();
// Use add() method to add elements in the list
absqlist.add("Geeks");
absqlist.add("for");
absqlist.add("Geeks");
absqlist.add("10");
absqlist.add("20");
// Displaying the AbstractSequentialList
System.out.println("AbstractSequentialList:"
+ absqlist);
// Using set() method to replace Geeks with GFG
System.out.println("The Object that is replaced is: "
+ absqlist.set(2, "GFG"));
// Using set() method to replace 20 with 50
System.out.println("The Object that is replaced is: "
+ absqlist.set(4, "50"));
// Displaying the modified linkedlist
System.out.println("The new List is:" + absqlist);
}
}
Output:
AbstractSequentialList:[Geeks, for, Geeks, 10, 20] The Object that is replaced is: Geeks The Object that is replaced is: 20 The new List is:[Geeks, for, GFG, 10, 50]Example 2: Java
// Java code to illustrate set()
import java.util.*;
import java.util.AbstractSequentialList;
public class AbstractSequentialListDemo {
public static void main(String args[])
{
// Creating an empty AbstractSequentialList
AbstractSequentialList<Integer>
absqlist = new LinkedList<Integer>();
// Use add() method to add elements in the list
absqlist.add(50);
absqlist.add(40);
absqlist.add(30);
absqlist.add(20);
absqlist.add(10);
// Displaying the AbstractSequentialList
System.out.println("AbstractSequentialList:"
+ absqlist);
// Using set() method to replace 40 with 400
System.out.println("The Object that is replaced is: "
+ absqlist.set(1, 400));
// Using set() method to replace 10 with 100
System.out.println("The Object that is replaced is: "
+ absqlist.set(4, 100));
// Displaying the modified linkedlist
System.out.println("The new List is:" + absqlist);
}
}
Output:
AbstractSequentialList:[50, 40, 30, 20, 10] The Object that is replaced is: 40 The Object that is replaced is: 10 The new List is:[50, 400, 30, 20, 100]
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4