Situatie
In this example, we will learn to remove elements from the Java LinkedList using remove()
Solutie
Pasi de urmat
import java.util.LinkedList;
class Main {
public static void main(String[] args) {
LinkedList<String> tests = new LinkedList<>();
// add elements in LinkedList
tests.add(“test1”);
tests.add(“test2”);
tests.add(“test3”);
tests.add(“test4”);
System.out.println(“LinkedList: ” + tests);
// remove elements from index 1
String str = tests.remove(3);
System.out.println(“Removed Element: ” + str);
System.out.println(“Updated LinkedList: ” + tests);
}
}
Output:
LinkedList: [test1, test2, test3, test4]
Removed Element: test4
Updated LinkedList: [test1, test2, test3]
Leave A Comment?