Java: Convert a linked list to array list
Java Collection, LinkedList Exercises: Exercise-23 with Solution
Write a Java program to convert a linked list to an array list.
Sample Solution:-
Java Code:
import java.util.*;
public class Exercise23 {
public static void main(String[] args) {
// create an empty linked list
LinkedList <String> linked_list = new LinkedList <String> ();
linked_list.add("Red");
linked_list.add("Green");
linked_list.add("Black");
linked_list.add("White");
linked_list.add("Pink");
System.out.println("Original linked list: " + linked_list);
//Convert a linked list to array list
List<String> list = new ArrayList<String>(linked_list);
for (String str : list){
System.out.println(str);
}
}
}
Sample Output:
Original linked list: [Red, Green, Black, White, Pink] Red Green Black White Pink
Pictorial Presentation:
Flowchart:
Java Code Editor:
Contribute your code and comments through Disqus.
Previous: Check if a particular element exists in a linked list.
Next: Compare two linked lists.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
It will be nice if you may share this link in any developer community or anywhere else, from where other developers may find this content. Thanks.
https://w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-23.php
- Weekly Trends and Language Statistics
- Weekly Trends and Language Statistics