ArrayList to LinkedList
Problem statement – Add all the elements of ArrayList to the linked list
Logic
- Create an ArrayList and add elements to it.
- Create a Linked list to store all the elements of the ArrayList
- Now add all the elements of a linked list to the array list
- Print all the elements of the LinkedList
- That’s it
Code
package LinkedList; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class ArrayListToLinkedList { public static void main(String[] args) { // Create an ArrayList List<Integer> arrayList = new ArrayList<>(); arrayList.add(1); arrayList.add(2); arrayList.add(3); // Create a LinkedList LinkedList<Integer> linkedList = new LinkedList<>(); // Add elements from ArrayList to LinkedList linkedList.addAll(arrayList); // Print the LinkedList for (int i : linkedList) { System.out.println(i); } } }