Java Program to Convert a LinkedList Collection to an Array

You are currently viewing Java Program to Convert a LinkedList Collection to an Array

Java program to convert a LinkedList collection to an array

// Java program to convert a LinkedList 
// collection to an array

import java.util.*;

public class Main {
  public static void main(String[] args) {
    LinkedList < Integer > list = new LinkedList < Integer > ();

    list.add(1);
    list.add(2);
    list.add(3);
    list.add(4);
    list.add(5);

    Object[] arr = list.toArray();
    System.out.println("Array: \n" + Arrays.toString(arr));
  }
}

Output

Array: 
[1, 2, 3, 4, 5]

Ranjith

Hi, I'm Manoj a full-time Blogger, YouTuber, Affiliate Marketer, & founder of Coding Diksha. Here, I post about programming to help developers.

Leave a Reply