Member-only story
Java Programming Language Tutorial: How to Traverse a LinkedList
Hi everyone, welcome back. In this tutorial, we will be going over how to traverse LinkedLists in Java. LinkedLists are objects that can be used to store a collection of data, similar to that of an ArrayList. The LinkedList is a great option when wanting to modify a collection of data. With this introduction out of the way, let’s get into it.
Creating LinkedList
Let’s start by creating a LinkedList. Before we can create the LinkedList, we need to make sure we have our LinkedList import, which is included within the java.util package:
import java.util.LinkedList;
Now, we can successfully create our LinkedList:
LinkedList<Integer> numbers = new LinkedList<Integer>();
So now, we have created a LinkedList variable of the integer type named numbers. When we create our LinkedList, we need to specify the data type. As we can see in the example above, we have used the ‘Integer’ data type. We can use other data types as well, such as the ‘String’ data type.
Adding Elements Into The LinkedList
Now, let’s try to add elements into our LinkedList. We can do this by using the built in add() function that is provided. Let’s see an…