bugl
bugl
HomeLearnPatternsPathsSearchPremium
HomeLearnPatternsPaths

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java LinkedList set() Method

❮ LinkedList Methods

Example

import java.util.LinkedList;
public class Main {
  public static void main(String[] args) {
    LinkedList<String> cars = new LinkedList<String>();
    cars.add("Volvo");
    cars.add("BMW");
    cars.add("Ford");
    cars.add("Mazda");
    cars.set(0, "Opel");
    System.out.println(cars);
  }
}

Definition and Usage

The set() method replaces an item at a specified position in the list and returns the item that was previously at that position.

Syntax

public T set(int
index , T
item )

T refers to the data type of items in the list.

Parameter Values

ParameterDescription
indexRequired. The position of the item to be replaced.
itemRequired. The new item to be placed at the specified position.

Technical Details

Returns:The item which was previously at the specified position in the list.
Throws:IndexOutOfBoundsException - If the index is less than zero, equal to the size of the list or greater than the size of the list.

Previous

Java LinkedList retainAll() Method

Next

Java LinkedList size() Method