Skip to content

Java LinkedList Size Example

This example shows how to get the size of the LinkedList (length of LinkedList) object in Java. The example also shows how to iterate the LinkedList using the size method and a for loop.

How to get the size of the LinkedList in Java (length of the LinedList)?

The size method of the LinkedList class returns the number of elements contained in the LinkedList object.

The size method returns an int value which is equal to the number of elements contained in the LinkedList object.

When you initially create an empty LinkedList object, the size method returns 0 as there are no elements in the LinkedList. As and when you add or remove elements from the LinkList object, the size keeps on changing according to the change we made to the list.

Output

How to use the size to iterate the LinkedList using for loop?

You can use the size of the LinkedList returned by the size method to iterate over LinkedList elements using the for loop as given below.

Output

This example is a part of the Java LinkedList tutorial.

Please let me know your views in the comments section below.

About the author

Leave a Reply

Your email address will not be published.