
- Java Tutorial
- Java - Home
- Java - Overview
- Java - Environment Setup
- Java - Basic Syntax
- Java - Object & Classes
- Java - Constructors
- Java - Basic Datatypes
- Java - Variable Types
- Java - Modifier Types
- Java - Basic Operators
- Java - Loop Control
- Java - Decision Making
- Java - Numbers
- Java - Characters
- Java - Strings
- Java - Arrays
- Java - Date & Time
- Java - Regular Expressions
- Java - Methods
- Java - Files and I/O
- Java - Exceptions
- Java - Inner classes
- Java Object Oriented
- Java - Inheritance
- Java - Overriding
- Java - Polymorphism
- Java - Abstraction
- Java - Encapsulation
- Java - Interfaces
- Java - Packages
- Java Advanced
- Java - Data Structures
- Java - Collections
- Java - Generics
- Java - Serialization
- Java - Networking
- Java - Sending Email
- Java - Multithreading
- Java - Applet Basics
- Java - Documentation
- Java Useful Resources
- Java - Questions and Answers
- Java - Quick Guide
- Java - Useful Resources
- Java - Discussion
- Java - Examples
Java Program to Access elements from a LinkedList
In this article, we will understand how to access elements from a linked-list. The java.util.LinkedList class operations perform we can expect for a doubly-linked list. Operations that index into the list will traverse the list from the beginning or the end, whichever is closer to the specified index.
Below is a demonstration of the same −
Suppose our input is −
Input list: [Python, Java, Scala, Java, JavaScript]
The desired output would be −
The element at index 3 is: Java
Algorithm
Step 1 - START Step 2 - Declare a linked list namely input_list. Step 3 - Define the values. Step 4 - Using the built-in function get(), we can access any specific element of the linked list by passing the index value to the function. Step 5 - Display the result Step 6 - Stop
Example 1
Here, we bind all the operations together under the ‘main’ function.
import java.util.LinkedList; public class Demo { public static void main(String[] args) { LinkedList<String> input_list = new LinkedList<>(); input_list.add("Python"); input_list.add("Java"); input_list.add("Scala"); input_list.add("Java"); input_list.add("JavaScript"); System.out.println("The list is defined as: " + input_list); String result_string = input_list.get(3); System.out.print("The element at index 3 is: " + result_string); } }
Output
The list is defined as: [Python, Java, Scala, Java, JavaScript] The element at index 3 is: Java
Example 2
Here, we encapsulate the operations into functions exhibiting object oriented programming.
import java.util.LinkedList; public class Demo { static void get_element(LinkedList<String> input_list, int index){ String result_string = input_list.get(index); System.out.print("The element at index 3 is: " + result_string); } public static void main(String[] args) { LinkedList<String> input_list = new LinkedList<>(); input_list.add("Python"); input_list.add("Java"); input_list.add("Scala"); input_list.add("Java"); input_list.add("JavaScript"); System.out.println("The list is defined as: " + input_list); int index = 3; get_element(input_list, index); } }
Output
The list is defined as: [Python, Java, Scala, Java, JavaScript] The element at index 3 is: Java
- Related Articles
- Java Program to Remove elements from the LinkedList
- Java Program to Add elements to a LinkedList
- Remove a range of elements from a LinkedList in Java
- Golang program to access elements from a linked list
- Get first and last elements from Java LinkedList
- Golang program to add elements to a linkedlist
- Java Program to Implement LinkedList
- Create an object array from elements of LinkedList in Java
- Java Program to Detect loop in a LinkedList
- C# Program to access tuple elements
- How to create a Queue from LinkedList in Java?
- Implement a stack from a LinkedList in Java
- Program to convert ArrayList to LinkedList in Java
- Java program to remove duplicates elements from a List
- Iterate through elements of a LinkedList using a ListIterator in Java

Advertisements