how to replace a LinkedList element at specified index using set method of LinkedList class. - Java Collection Framework

Java examples for Collection Framework:LinkedList

Description

how to replace a LinkedList element at specified index using set method of LinkedList class.

Demo Code


import java.util.LinkedList;
 
public class Main {
 
  public static void main(String[] args) {
 
    //create LinkedList object
    LinkedList lList = new LinkedList();
   /*www.  j  a  va  2  s  .  co  m*/
    //add elements to LinkedList
    lList.add("1");
    lList.add("2");
    lList.add("3");
    lList.add("4");
    lList.add("5");
   
    System.out.println("LinkedList contains : " + lList);
   
     lList.set(3, "Replaced");
     System.out.println("After replacing 4, LinkedList contains : " + lList);
  }
}

Result


Related Tutorials