Java Program to Access elements from a LinkedList

Configurare noua (How To)

Situatie

In this example, we will learn to access elements from a LinkedList in Java using various methods.

Backup

import java.util.LinkedList;

class Main {
  public static void main(String[] args) {
    LinkedList<String> languages = new LinkedList<>();

    // add elements in the LinkedList
    languages.add("Python");
    languages.add("Java");
    languages.add("JavaScript");
    System.out.println("LinkedList: " + languages);

    // get the element from the LinkedList
    String str = languages.get(1);
    System.out.print("Element at index 1: " + str);
  }
}

Output

LinkedList: [Python, Java, JavaScript]
Element at index 1: Java

In the above example, we have used the get() method with parameter 1. Here, the method returns the element at index 1.

Solutie

Tip solutie

Permanent

Voteaza

(12 din 19 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?