Introduction
The size()
method in Java's LinkedList
class is a simple yet important tool. It allows you to determine the number of elements currently stored within your linked list. Knowing the size can be crucial for various operations, like iterating through the list or checking if it meets certain criteria before processing.
Syntax
public int size()
Parameters
The size()
method does not accept any parameters. It operates on the existing linked list to retrieve its size.
Parameter | Description |
---|---|
None | The method doesn't require any input. |
Return Value
The size()
method returns an integer representing the number of elements present in the linked list.
Examples
Example 1: Basic Usage
This example demonstrates how to create a LinkedList
, add some elements, and then use the size()
method to find out how many elements are in it.
import java.util.LinkedList;
public class LinkedListSizeExample1 {
public static void main(String[] args) {
LinkedList<String> myList = new LinkedList<>();
myList.add("Apple");
myList.add("Banana");
myList.add("Cherry");
int listSize = myList.size();
System.out.println("The size of the linked list is: " + listSize);
}
The size of the linked list is: 3
Here, we create a LinkedList
named myList
and add three strings. Then, we call size()
to get the number of elements (which is 3) and print it to the console.
Example 2: Checking Size Before Iteration
This example shows how you might use the size of a linked list to control whether or not you iterate over its elements. This avoids unnecessary iterations when the list is empty, potentially saving processing time.
import java.util.LinkedList;
public class LinkedListSizeExample2 {
public static void main(String[] args) {
LinkedList<Integer> numbers = new LinkedList<>();
if (numbers.size() > 0) {
for (int number : numbers) {
System.out.println(number);
}
} else {
System.out.println("The list is empty, no elements to print.");
}
}
The list is empty, no elements to print.
In this example, we create an empty linked list numbers
. The condition `numbers.size() > 0` evaluates to false because the list is empty. Therefore, the code inside the `else` block executes, printing a message indicating that there are no elements to print.
Example 3: Size After Removal
This demonstrates how the size of the linked list changes after you remove an element using methods like removeFirst()
or removeLast()
. This is useful for tracking list modifications and ensuring correct operations afterwards.
import java.util.LinkedList;
public class LinkedListSizeExample3 {
public static void main(String[] args) {
LinkedList<Double> values = new LinkedList<>();
values.add(10.5);
values.add(20.7);
values.add(30.9);
System.out.println("Initial size: " + values.size());
values.removeFirst(); // Removes the first element (10.5)
System.out.println("Size after removing first element: " + values.size());
}
Initial size: 3
Size after removing first element: 2
Initially, the list has a size of 3. After calling removeFirst()
to remove the first element (10.5), the size is reduced to 2.