Java Program to Access elements from a LinkedList

What is LinkedList ?
A linked list is a linear data structure in which each element (called a node) stores a reference to the next element in the list. Linked lists are dynamic data structures, which means that their size can change during the execution of a program. In contrast, arrays have a fixed size, which means that you need to specify the size of the array when you create it and you cannot change it later.
There are several ways to access elements from a LinkedList in Java:
- Using the getFirst() and getLast() methods: These methods return the first and last element in the list, respectively. If the list is empty, they throw a NoSuchElementException.
- Using the get(int index) method: This method returns the element at the specified index in the list. If the index is out of bounds (less than 0 or greater than or equal to the size of the list), it throws an IndexOutOfBoundsException.
- Using an iterator: You can use the iterator() method of the LinkedList class to obtain an Iterator for the list, and then use the next() method of the Iterator to retrieve the elements in the list.
- Using a for loop: You can use a for loop to iterate through the elements in the list and access them one by one.
- Using the forEach() method: You can use the forEach() method of the LinkedList class to iterate through the elements in the list and perform an operation on each element.
- Using a ListIterator: You can use the listIterator() method of the LinkedList class to obtain a ListIterator for the list, and then use the next() method of the ListIterator to retrieve the elements in the list. The ListIterator interface provides additional methods for iterating through the list in either direction, as well as methods for adding, removing, and setting elements in the list.
Example 1 :
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList <String>list = new LinkedList<>(); list.add("apple"); list.add("banana"); list.add("orange"); // Access the first element String first = list.getFirst(); System.out.println(first); // apple // Access the last element String last = list.getLast(); System.out.println(last); // orange // Access the element at a particular index String element = list.get(1); System.out.println(element); // banana } }
Output :
apple orange banana
Explanation :
In this example, we create a LinkedList of strings and add three elements to it. We then use the getFirst() method to access the first element in the list, the getLast() method to access the last element in the list, and the get(int index) method to access the element at a particular index in the list.
Example 2:
import java.util.LinkedList; public class Main { public static void main(String[] args) { LinkedList <Integer>linkedList = new LinkedList<>(); linkedList.add(1); linkedList.add(2); linkedList.add(3); int element = linkedList.get(0); // gets the element at index 0 System.out.println(element); // prints 1 } }
Output :
1
Explanation :
In this example, we create a LinkedList object and add some elements to it using the add method. Then, we use the get method to retrieve the element at a specific index.
Prime Course Trailer
Related Banners
Get PrepInsta Prime & get Access to all 200+ courses offered by PrepInsta in One Subscription
Get over 200+ course One Subscription
Courses like AI/ML, Cloud Computing, Ethical Hacking, C, C++, Java, Python, DSA (All Languages), Competitive Coding (All Languages), TCS, Infosys, Wipro, Amazon, DBMS, SQL and others
Login/Signup to comment