bugl
bugl
HomeLearnPatternsPathsSearch
HomeLearnPatternsPathsSearch

Loading lesson path

Learn/Java/Java Reference
Java•Java Reference

Java LinkedList set() Method

Flash cards

Review the key moves

1/4
Core idea

What is the main idea behind Java LinkedList set() Method?

Lesson checks

Practice each idea before moving on

Short Mimo-style checks built from this lesson's code, terms, and sequence.

1Quick choice

Which statement best captures the main point of this lesson?

2Fill blank

Complete the missing token from the example code.

___ java.util.LinkedList;
3Order

Put the learning moves in the order that makes the concept easiest to apply.

The set() method replaces an item at a specified position in the list and returns the item that was previously at that position.
Definition and Usage
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