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

RahimV

RahimV

My name is RahimV and I have over 16 years of experience in designing and developing Java applications. Over the years I have worked with many fortune 500 companies as an eCommerce Architect. My goal is to provide high quality but simple to understand Java tutorials and examples for free. If you like my website, follow me on Facebook and Twitter.

Add Comment

Your email address will not be published. Required fields are marked *