
- 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 Convert the LinkedList into an Array and vice versa
In this article, we will understand how to convert the linked list into an array and vice versa. 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 −
The list is defined as: [Java, Python, Scala, Mysql]
The desired output would be −
The result array is: Java Python Scala Mysql
Algorithm
Step 1 - START Step 2 - Declare namely Step 3 - Define the values. Step 4 - Create a list and add elements to it using the ‘add’ method. Step 5 - Display the list on the console. Step 6 - Create another empty list of previous list size. Step 7 - Convert it into array using the ‘toArray’ method. Step 8 - Iterate over the array and display the elements on the console. Step 9 - Stop
Example 1
Here, we convert the list to an array.
import java.util.LinkedList; public class Demo { public static void main(String[] args) { System.out.println("The required packages have been imported"); LinkedList<String> input_list= new LinkedList<>(); input_list.add("Java"); input_list.add("Python"); input_list.add("Scala"); input_list.add("Mysql"); System.out.println("The list is defined as: " + input_list); String[] result_array = new String[input_list.size()]; input_list.toArray(result_array); System.out.print("\nThe result array is: "); for(String elements:result_array) { System.out.print(elements+" "); } } }
Output
The required packages have been imported The list is defined as: [Java, Python, Scala, Mysql] The result array is: Java Python Scala Mysql
Example 2
Here, we convert an array to a list.
import java.util.Arrays; import java.util.LinkedList; public class Demo { public static void main(String[] args) { System.out.println("The required packages have been imported"); String[] result_array = {"Java", "Python", "Scala", "Mysql"}; System.out.println("The elements of the result_array are defined as: " + Arrays.toString(result_array)); LinkedList<String> result_list= new LinkedList<>(Arrays.asList(result_array)); System.out.println("\nThe elements of the result list are: " + result_list); } }
Output
The required packages have been imported The elements of the result_array are defined as: [Java, Python, Scala, Mysql] The elements of the result list are: [Java, Python, Scala, Mysql]
- Related Articles
- Golang program to convert a linked list into an array and vice-versa
- Java Program to Convert the ArrayList into a string and vice versa
- How to convert an array to Set and vice versa in Java?
- Golang program to convert the arraylist into string and vice-versa
- Java Program to Convert contents of a file to byte array and Vice-Versa
- Converting A Linked List Into An Array And Vice Versa
- How to convert String to StringBuilder and vice versa Java?
- C++ Program to Convert Binary Number to Octal and vice-versa
- C++ Program to Convert Binary Number to Decimal and vice-versa
- C++ Program to convert Octal Number to Decimal and vice-versa
- Convert LinkedList to an array in Java
- C++ Program to Convert Km/hr to miles/hr and vice versa
- How to convert an integer to hexadecimal and vice versa in C#?
- How to Convert a String to Hexadecimal and vice versa format in java?
- How can you convert the saturated solution into unsaturated or vice-versa?

Advertisements