Java program to add element at the specified position

Configurare noua (How To)

Situatie

In this example, we will learn to insert elements to the Java LinkedList at the specified position.

Solutie

Pasi de urmat

import java.util.LinkedList;

class Main {
public static void main(String[] args){

// create a linkedlist
LinkedList<String> tests = new LinkedList<>();

// add elements to LinkedList
tests.add(“test2”);
tests.add(“test3”);
System.out.println(“LinkedList: ” + tests);

// add element at the specified position
languages.add(0, “test1”);
System.out.println(“Updated LinkedList: ” + tests);
}
}

Output:

LinkedList: [test2, test3]
Updated LinkedList: [test1, test2, test3]

Tip solutie

Permanent

Voteaza

(9 din 19 persoane apreciaza acest articol)

Despre Autor

Leave A Comment?