YouTip LogoYouTip

Java Linkedlist Clear

# Java LinkedList clear() Method [![Image 3: Java LinkedList](#) Java LinkedList](#) * * * The `clear()` method is a commonly used method provided by the `LinkedList` class in Java, used to remove all elements from the linked list. After calling this method, the linked list becomes an empty list. ### Method Syntax public void clear() ### Method Functionality The main functions of the `clear()` method are: 1. Remove all elements from the `LinkedList` 2. Reset the size of the linked list to 0 3. Does not affect the capacity of the linked list, as `LinkedList` has no capacity limit * * * ## Usage Examples ### Basic Usage ## Example import java.util.LinkedList; public class ClearExample { public static void main(String[] args){ // Create a LinkedList LinkedList fruits =new LinkedList(); // Add elements fruits.add("Apple"); fruits.add("Banana"); fruits.add("Cherry"); System.out.println("LinkedList before clearing: "+ fruits); System.out.println("Size before clearing: "+ fruits.size()); // Use the clear() method to empty the linked list fruits.clear(); System.out.println("LinkedList after clearing: "+ fruits); System.out.prin
← Java Linkedlist RemovelastJava Linkedlist Offerfirst β†’